问题
I am working on an ASP.NET 1.1 website that stores files in the database. The website has been working great with no problems uploading and downloading any type of documents since 2001.
However, recently the customer noticed that all Microsoft Office 2007 documents DOCX
, XLSX
, ect are corrupted when any user try to download them.
What I have noticed is that SQL Server adds an extra byte to the DocumentContent
(image column). Before uploading the document the content and length are fine. However, after they get stored, SQL Server adds an EXTRA Byte.... WHY?????
Many people on the internet provided a solution on how to download the file, which is great and it worked for me.
I am looking for an answer to this question... WHY DOES SQL SERVER ADD AN EXTRA BYTE TO THE IMAGE COLUMN?
It is an Asp.NET 1.1 and SQL 2000 but we just moved it to 2008 and we still have the same problem.
回答1:
I am not 100% sure if your problem was the same as what I have had but in my case, I found that the problem was actually in the writing part, not in the reading part. For example, my original writing code was like this:
Dim FILE_CONTENT(len) As Byte
File.InputStream.Read(FILE_CONTENT, 0, len)
SaveFileToDatabase(FILE_NAME, CONTENT_TYPE, FILE_CONTENT)
When I changed the first line as the following :
Dim FILE_CONTENT(0 To len - 1) As Byte
the reading error disappeared. I just forgot that VB actually allocates N+1 bytes (O to N) by default when you dimension it without specifying the lower bound. See similar situation described here: Uploaded Docx Files are getting corrupted . Hope that helps.
来源:https://stackoverflow.com/questions/7017294/corrupted-files-microsoft-office-2007-asp-net-1-1-and-sql-server