上传后 Docx 文件损坏

Docx File Corrupted after Upload

找了很多东西,上网查了很多,还是搞不懂这段代码是怎么回事。我仍然知道我的 docx 文件已损坏,但是当我使用 doc 文件时,一切都很好。

我的上传代码

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

我的下载码:

   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

我试过这些都没有成功:

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

我正在将文件上传到数据库中,然后按超链接下载文件。当我按下超链接并下载文件时。查看它已损坏的文件。

在VB中,当声明一个数组时,你给它数组中元素的数量。这与您指定数组最后一个索引的许多语言不同。

对于您显示的代码,您需要使用

Dim myData(nFileLen - 1) As Byte

确保数组中没有多余的元素。

.doc 格式似乎对此不敏感,但 .docx 是。