Docx, xlsx, pptx files get corrupted when read back from database
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) SaveFileToDatabase(FILE_NAME, CONTENT_TYPE, FILE_CONTENT)
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.