Skip to content

Docx, xlsx, pptx files get corrupted when read back from database

June 17, 2014

You save an uploaded file of docx (or any MS Office file with trailing x, xlsx, pptx extension) into a SQL database. Then later try to open it from an ASP.NET page after retrieving from database, MS Word says “We’re sorry. We can’t open ….docx because we found a problem with its contents“. After click OK, it shows “Word found unreadable content in …..docx. Do you want to recover the contents of this document?”  

I spent several hours to debug this and tried all possible solutions suggested from others but no good. Then finally, I realized the problem may not be in the reading code but in the writing part. When I changed this part of writing a file into db :

Dim FILE_CONTENT(len) As Byte
File.InputStream.Read(FILE_CONTENT, 0, len)

to :

Dim FILE_CONTENT(0 To len - 1) As Byte

then the reading error disappeared. I just forgot VB actually allocates N+1 elements from 0 to N when you dimension an array without specifying the lower bound. Hope that helps.

No comments yet

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: