title: Docx File Corrupted after Upload
body: <p>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.</p>
  
  <p>My Upload Code</p>
  
  <pre><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}&amp;Item={1}", MyPage.DtoPage.PageID, Me.Item.Id))
  End Sub
  </code></pre>
  
  <p>My download code:</p>
  
  <pre><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
  </code></pre>
  
  <p>I have tired these with no success: </p>
  
  <p><a href="https://stackoverflow.com/questions/2477564/why-are-docx-files-being-corrupted-when-downloading-from-an-asp-net-page">Why are .docx files being corrupted when downloading from an ASP.NET page?</a> </p>
  
  <p><a href="http://www.aspmessageboard.com/showthread.php?230778-Downloaded-docx-files-are-corrupted" rel="nofollow">http://www.aspmessageboard.com/showthread.php?230778-Downloaded-docx-files-are-corrupted</a></p>
  
  <p><a href="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" rel="nofollow">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</a></p>
  
  <p>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. </p>