Thursday, March 29, 2012

Office 2007 documents to database, dynamically generate on demand

Hello, I am currently adding some document management functionality to a .Net 2.0 Web app (VB) where I take a file in, save the file to a SQL 2000 database (image datatype), and then allow users to browse and download these files.

This works great for most files my users use -- with the exception of Office 2k7 files

I am able to upload a file, read the file into a binary array and insert into a new record in the database.

However, when I retrieve the binary array and attempt to output the file, there is a problem.

The user is prompted to open or save the file per-usual, however when Word 2007 tries to open the file, there is an error message that says:

"The Office Open XML file Test.docx cannot be opened because there are problems with the contents"

Under "Details" it says:

"The file is corrupt and cannot be opened"

If I click "OK", I am asked whether I want to have Word recover the contents of the document.

If I click "Yes" my document opens and all of the original content is there.

I have been doing some reading, and I know that the new Office 2007 files are really .zip files with XML files that contain formatting and content information.

What I would like to know is, is there a reason why I can't save these files to the database as I have been, and if so, is there a "right" way to do this?

Hi,

Based on my understanding, the users upload the word 2007 files into database and download them with your asp.net application. When you try to open the file that is downloaded, you get the error message above. If I have misunderstood you, please feel free to let me know.

To better understand your issue, could you please confirm the following information:

When we write the file to the client, we should make sure that file doesn't contain HTML-tag. We can add Response.End() right after writing and stop further processing of the ASP.NET page. For more information, seehttp://blog.benday.com/archive/2005/08/09/2705.aspx.


Yes, you have summarized what I am doing correctly.

I have been looking into this in more detail, and after using Hex Workshop, I compared the original file and the streamed file. The streamed file contained 1 extra byte.

I reviewed my code and found that the size of the file in the database was the same as the original, so I reviewed my code in the download.aspx code-behind (the file that streams the requested document).

I had read about this earlier, so I knew not to have any HTML in the aspx.

However, it seems that the Content-Length of the file is zero-based, so when I was setting Content-Length to equal the value I had in the database, an extra byte was being added.

When I set Content-Length to equal the size of the file (in bytes) - 1, I no longer received the error.

Ex:

Dim File As New EO.Library.File(CInt(Request("file")))
Dim _BinaryLength = (CInt(File.Binary.Length) - 1)

Response.Clear()
Response.Buffer = True
Response.AddHeader("Content-Disposition", "attachment;filename=" & File.Name)
Response.AddHeader("Content-Length", CInt(_BinaryLength))
Response.ContentType = File.ContentType
Response.BinaryWrite(File.Binary)
Response.End()

0 comments:

Post a Comment