'error storing modern office files (xlsx, docx, pptx) in database

I'm building a system that allows adding attachments. I have almost everything working, but any of the "open" formats from newer office files are coming out with the following error:

"We found a problem with some of the content in file.xlsx. Do you want us to recover as much as we can? IF you trust the source of this workbook, click yes."

clicking yes, usually will open the file, but I'd like to get past this entirely.

Files are stored in SQL Server, and uploaded and retrieved by stored procedure.

uploadFunction:

Protected Sub btnUploadAttachment_Click(sender As Object, e As EventArgs) Handles btnUploadAttachment.Click
    Try
        Dim fileCount As Integer = 0
        For Each file As UploadedFile In filFileAttachment.UploadedFiles
            fileCount += 1

            Using conn As New SqlConnection With {.ConnectionString = SadSql},
                cmd As New SqlCommand With {.CommandText = "RAD_ProjectTracker.dbo.spProject_Attachment_Add", .Connection = conn, .CommandType = CommandType.StoredProcedure, .CommandTimeout = SQLCommandTimeout}
                Dim buffer As Byte()
                Using s As Stream = file.InputStream
                    buffer = New Byte(s.Length) {}
                    s.Read(buffer, 0, buffer.Length)
                End Using

                cmd.Parameters.Add("@PROJECT_ID", SqlDbType.Int).Value = hfProjectID.Value
                cmd.Parameters.Add("@VERSION_ID", SqlDbType.Int).Value = hfVersionID.Value
                If Not hfJobID.Value.IsNullOrEmpty() Then cmd.Parameters.Add("@JOB_ID", SqlDbType.Int).Value = hfJobID.Value
                cmd.Parameters.Add("@USER_ID", SqlDbType.Int).Value = Session("uid")
                cmd.Parameters.Add("@ATTACHMENT", SqlDbType.VarBinary).Value = buffer
                cmd.Parameters.Add("@ATTACHMENT_TYPE", SqlDbType.VarChar).Value = "ATTACH_TYPE_DOCUMENT"
                cmd.Parameters.Add("@MIME_TYPE", SqlDbType.VarChar).Value = file.ContentType
                cmd.Parameters.Add("@FILE_NAME", SqlDbType.VarChar).Value = file.FileName
                If Not txtDescription.Text.IsNullOrEmpty() Then cmd.Parameters.Add("@DESCRIPTION", SqlDbType.VarChar).Value = txtDescription.Text

                conn.Open()
                cmd.ExecuteNonQuery()
                conn.Close()
            End Using
        Next
        ReturnToAttachments()
    Catch ex As Exception
        Dim s As String = ex.Message
    End Try
End Sub

and the retrieval code:


Dim tsql As String = "Exec RAD_ProjectTracker.dbo.spProject_Attachments_Get @ATTACHMENT_ID=" & dataItem("ATTACHMENT_ID").Text
    Dim dt As DataTable = GetSQLDataSet(tsql, True).TablesSafe(0)
    If dt.Rows.Count > 0 Then
        Dim dr As DataRow = dt.RowsSafe(0)
        Dim file As Byte() = CType(dr("ATTACHMENT"), Byte())
        Dim fileName As String = If(IsDBNull(dr("FILE_NAME")), "attachment", CStr(dr("FILE_NAME"))) 
        Response.Clear()
        Response.ClearHeaders()
        Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName)
        Response.ClearContent()
        Response.ContentType = dr("MIME_TYPE") 
        Response.BinaryWrite(file)
        Response.End()
    End If

I've tested .xls, .dox, .ppt, .png, .jpg, .txt, and a few others and they all work fine.

It's only the .xlsx, .docx, and .pptx that seem to be having issues.

I've googled around and haven't been able to find much. Is there an extra step to these documents? or perhaps i am doing something in the wrong order?

Any help would be appreciated.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source