Uploaded then downloaded same .xlsx file corrupts in SQL
Posted on 2011-05-06
We have an SaaS application that allows users to upload file attachments. We have noticed that when we attach an Excel spreadsheet file that ends in .xlsx - that it uploads fine - but then when its downloaded Excel has a problem with it and says 'Excel found unreadable content in <filename>. Do you want to recover the contents of this workbook?'
If you say yes then the file does recover and open .. but thats not the point.
We are worried that the file has been changed in someway - these are client files - thay are not ours and the integrity of the file must stay the same as it was when it was uploaded by them.
Can anyone tell us what is going on or what we are doing wrong here ?
Relevant code is below.
We are using SQLServer 2005 and VB.Net
CREATE TABLE [dbo].[AttachedDocuments](
[RecNum] [int] IDENTITY(1,1) NOT NULL,
[Document_FileName] [nvarchar](50) NULL,
[Document_File] [image] NULL)
View File Code
Dim strContType As String = GetContentType(strDBFilePath) 'Function where i get file type for e.g. ("doc", "application/msword") , ("docx", "application/msword") , ("pdf", "application/pdf"), ("xls", "application/vnd.ms-excel")
Response.AddHeader("Content-Disposition", "attachment;filename=" & strDBFilePath) ' strDBFilePath value we get from database
Response.Charset = ""
Response.ContentType = strContType
Response.BinaryWrite(CType(objFile, Byte())) ' objFile value we get from database - Document_File field
Save file code
'file_Document is FileUpload control field
Dim intLen As Int32 = file_Document.PostedFile.ContentLength
Dim imgBinaryData(intLen) As Byte
file_Document.PostedFile.InputStream.Read(imgBinaryData, 0, intLen)
strSQL = "Select * From AttachedDocuments"
Dim da As New SqlClient.SqlDataAdapter(strSQL, oconn)
Dim dt As New DataTable, dr As DataRow
dr = dt.NewRow
dr("Document_FileName") = strFileName
dr("Document_File") = imgBinaryData
Dim sqlCmdBld As New SqlClient.SqlCommandBuilder(da)