2015-01-29 3 views
1

Я устал много вещей, искал в Интернете, и все же я не могу понять, что происходит с этим кодом. Я до сих пор понимаю, что мои файлы docx повреждены, но когда я делаю это с doc-файлом, все идет отлично.Docx Файл поврежден после загрузки

Мои загрузки Код

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

Я загружаю файл в БД и загружаю файл, нажимая гиперссылку. Когда я нажимаю гиперссылку и загружаю файл. Просмотр в файле поврежден.

+0

Ваш вопрос не совсем ясен. Вы пытаетесь загрузить файл docx, а затем повторно загрузить его? Если да, является ли docx поврежденным после загрузки или после загрузки? –

+0

Возможно, база данных каким-то образом испортила файл. – Nico

ответ

1

В VB при объявлении массива вы даете ему количество элементов в массиве. Это отличается от многих языков, на которых указывается последний индекс массива.

Для кода вы показываете, что вам нужно использовать

Dim myData(nFileLen - 1) As Byte 

, чтобы убедиться, что вы не имеете дополнительный элемент в массиве.

Похоже, что формат .doc не чувствителен к этому, но .docx is.

Смежные вопросы