locked
Trouble with docx files RRS feed

  • Question

  • User1892059496 posted

    Hi all,

    I have already read the following post:  http://forums.asp.net/t/1301978.aspx/2/10, but can't seem to resolve my issue, even though I believe I'm having the same problem.

    My files are stored as binary files in an image field in the db, and there is no problem until I get to .docx files.  The original file opens just fine on my hard drive, but after uploading and retrieving from db, Word 2007 says there are errors that need to be repaired, and Word 2003 won't open them at all - saying that they are corrupt.

    My code is relatively simple, can somebody help me get started?

     

    fn = dr.Item("filename") ext = System.IO.Path.GetExtension(fn).ToLower() MimeType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document"

     

    Response.AddHeader("content-disposition", "attachment; filename=" & fn) Response.ContentType = MimeType Dim bfByte() As Byte = dr.Item("UplFile") Response.OutputStream.Write(dr("UplFile"), 0, bfByte.Length) 'Response.BinaryWrite(dr.Item("Upload"))

    Initially, I used the Response.BinaryWrite line to send the file to the browser, but I have since changed that line to use the Response.OutputStream.Write method - still to no avail. 

    I have also **tried** to shave off the extra byte in the byte array - but that didn't work for me either, which leads me to believe I'm doing it incorrectly.  I can post that code if anyone needs to see it.

    Thanks

    Wednesday, April 27, 2011 9:50 AM

Answers

  • User1892059496 posted

    Well, after many rounds of trial and error I stumbled across a soluction that worked.

    I didn't have to shave a byte off the end, I just had to add a Response.End() after the write line -- see below.

    fn, ext, MimeType are all string variables and dr("UplFile") is the binary field returned from the db

     

                                Response.Clear()
                                Response.Buffer = True
                                fn = dr.Item("filename")
                                ext = System.IO.Path.GetExtension(fn).ToLower()
                                MimeType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
                                Response.AddHeader("content-disposition", "attachment; filename=" & fn)
                                Response.ContentType = MimeType
                                Dim bfByte() As Byte = dr.Item("UplFile")
                                Response.OutputStream.Write(dr("UplFile"), 0, bfByte.Length)
                                Response.End()
    

    Hope this helps somebody - and thank you for the many posts regarding this issue that led me to this solution.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, April 28, 2011 9:29 AM

All replies

  • User1864737465 posted

    I want to say something looks off with MimeType, as in what your putting into Response.ContentType, but I can't be certain.

    I'd try narrowing it down a bit, perhaps just opening a docx file with:

    Response.ContentType = "application/vnd.ms-word.document"  (or is it "application/vnd.ms-word.template"? sorry, just did a quick google and copied and pasted a couple i saw)

    Hope this helps.

    Wednesday, April 27, 2011 1:34 PM
  • User1892059496 posted

    Thank you for your reply.  No, changing the mime type did not prevent the errors from happening.

    I do believe an extra byte is being added to the document either by .net or SQL, but I can't seem to get my code written in a way that eliminates the errors from happening.

    Do you see a way that I could improve my code above to handle the extra byte?  If that is indeed the problem?

    Thanks,
    Carrie

    Wednesday, April 27, 2011 2:11 PM
  • User1864737465 posted

    If you think the bytes are the problem and your situation is the similar to the other thread, you're saying that you already tried the following?:

    Response.OutputStream.Write(dr("UplFile"), 0, bfByte.Length-1)  <--- notice the -1 at the end)

    Also, you've tried retrieving other docx files? what about doc or other text files?

    Wednesday, April 27, 2011 2:27 PM
  • User1892059496 posted

    Hi and thank you,

    Yes, that was one of the first things I tried.

    I created a new document with one line of text in MS 2007, then saved it to my hard drive, uploaded it to db.  WHen I tried to retrieve it, with the following code:

     

                                Response.Clear()
                                Response.Buffer = True
    
                                fn = dr.Item("filename")
                                ext = System.IO.Path.GetExtension(fn).ToLower()
                               
                                MimeType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
                                Response.AddHeader("content-disposition", "attachment; filename=" & fn)
                                Response.ContentType = MimeType
                                Dim bfByte() As Byte = dr.Item("UplFile")
                                Response.OutputStream.Write(dr("UplFile"), 0, bfByte.Length - 1)
    

    I got an error, 'The file is corrupt and can't be opened'

    Now, Word 2007 will repair the file if you click the right buttons, but Word 2003 won't open it at all.

    However, the file that I saved to my hard drive - opens without error or issue.

    Can you think of anything else?

    Thanks,

    Wednesday, April 27, 2011 2:57 PM
  • User1892059496 posted

    Well, after many rounds of trial and error I stumbled across a soluction that worked.

    I didn't have to shave a byte off the end, I just had to add a Response.End() after the write line -- see below.

    fn, ext, MimeType are all string variables and dr("UplFile") is the binary field returned from the db

     

                                Response.Clear()
                                Response.Buffer = True
                                fn = dr.Item("filename")
                                ext = System.IO.Path.GetExtension(fn).ToLower()
                                MimeType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
                                Response.AddHeader("content-disposition", "attachment; filename=" & fn)
                                Response.ContentType = MimeType
                                Dim bfByte() As Byte = dr.Item("UplFile")
                                Response.OutputStream.Write(dr("UplFile"), 0, bfByte.Length)
                                Response.End()
    

    Hope this helps somebody - and thank you for the many posts regarding this issue that led me to this solution.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, April 28, 2011 9:29 AM
  • User1954212465 posted

    When storing a binary file in SQL Server, keep in mind that a file is padded to the nearest word boundry, so you can potentially have an extra byte added to a file. The solution is to store the original file size in the db when you store the file, and use that for the length that needs to be passed to the write function of the Stream object. "Stream.Write(bytes(), 0, length)". This is the ONLY reliable way of getting the correct file size, which is very important for Office 2007 and up files, which do not allow extra characters to be on the end of them (most other file types like jpg's don't care).

    Friday, August 19, 2011 9:27 AM