Someone
Someone

Reputation: 69

Docx File Corrupted after Upload

I have tired many things, searched the internet, and still I cannot figure out what is going on with this code. I still get that my docx files are corrupted, but when I do it with doc file everything is going great.

My Upload Code

Private Sub LbReqUploadAttachment1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles LbReqUploadAttachment1.Click

    If FileUplReqAttachment1.HasFile Then
        'Then save the attachment to the documents table
        Dim type As String = Me.FileUplReqAttachment1.PostedFile.ContentType
        Dim myFile As System.Web.HttpPostedFile = Me.FileUplReqAttachment1.PostedFile
        Dim nFileLen As Integer = myFile.ContentLength
        Dim myData(nFileLen) As Byte
        myFile.InputStream.Read(myData, 0, nFileLen)
        Dim DocDto As New DocumentsDto
        DocDto.Month = Now.ToString("m")
        DocDto.Year = Now.Year
        DocDto.MimeType = type
        DocDto.UploadedById = MyPage.LoggedOnUser.DtoUser.PersonId
        DocDto.DocumentBytes = myData.ToArray
        DocDto = MyPage.DelegateDocument.CreateDocumentsDto(DocDto)

        'Update the order with the new document id
        If Me.TbIntlFlagz.Checked Then
            Item.AssetID = CStr(DocDto.DocumentID)
        Else
            Item.AssetID = "0"
        End If

        ' Save Everything
        SaveItem()

        'Focus after postback
        FileUplReqAttachment1.Focus()
    End If

    'Stay on order screen
    Response.Redirect(String.Format("Default.aspx?i={0}&Item={1}", MyPage.DtoPage.PageID, Me.Item.Id))
End Sub

My download code:

   Sub ProcessRequest(ByVal context As HttpContext) Implements ttpHandler.ProcessRequest

    Dim docba As Byte() = docDto.DocumentBytes

        Dim ext As String = Mime.GetExtensionFromMime(docDto.MimeType)
        context.Response.ContentType = docDto.MimeType

        If String.IsNullOrEmpty(ext) Then
            'We can only use the attachment approach if we found a good extension based on the mime type
        Else
            Dim DispositionHeader As String
            If Not context.Request.QueryString.Item("fn") Is Nothing Then
                DispositionHeader = String.Format("attachment; filename={0}.{1}", AntiXss.UrlEncode(context.Request.QueryString.Item("fn")), ext)
            Else
                DispositionHeader = String.Format("attachment; filename={0}.{1}", AntiXss.UrlEncode("Document"), ext)
            End If
            context.Response.AppendHeader("Content-Disposition", DispositionHeader)
        End If

        context.Response.Expires = (60 * 24 * 1)
        context.Response.OutputStream.Write(docba, 0, docba.Length)
        context.Response.Flush()
        docba = Nothing

    End If
End Sub

I have tired these with no success:

Why are .docx files being corrupted when downloading from an ASP.NET page?

http://www.aspmessageboard.com/showthread.php?230778-Downloaded-docx-files-are-corrupted

https://social.msdn.microsoft.com/Forums/vstudio/en-US/88383fb2-03c6-49f5-afee-ce38497789bd/retrieving-docx-stored-in-sql-server-results-in-there-was-an-error-opening-the-file?forum=vbgeneral

I am uploading the file into a DB, and downloading the file by pressing a hyperlink. When I press the hyperlink and download the file. View at the file it is corrupted.

Upvotes: 1

Views: 1079

Answers (1)

Andrew Morton
Andrew Morton

Reputation: 25013

In VB, when declaring an array you give it the number of elements in the array. This is different from many languages where you specify the last index of the array.

For the code you show, you need to use

Dim myData(nFileLen - 1) As Byte

to make sure that you do not have an extra element in the array.

It appears that the .doc format is not sensitive to this, but .docx is.

Upvotes: 2

Related Questions