title: Why browsers on iPhone/iPad cannot download/preview docx files?
body: <p>My web application allows end users to upload and store documents in the database. And they can download that documents from the app, too.</p>
  
  <p>The behavior of downloading docs is so simple that a user clicks a link "Download this document" on the GUI, I will query the document binary data from the database based on the input documentID parameter requested from the client. After that, I will response the bytes data to the client.</p>
  
  <p>Let's see my code.</p>
  
  <p>This is the function where I receive requests of downloading docs from the client.</p>
  
  <pre><code>Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
      Dim DocumentID As String = getParameter("DocumentID", context)
      DocumentWrapper.Download(DocumentID)
      context.Response.Write("&lt;script language=javascript&gt;window.open('','_self','');window.close();&lt;/script&gt;")
  End Sub
  </code></pre>
  
  <p>And this is the function that I query the binary data and response it to the client.</p>
  
  <pre><code> Public Shared Sub Download(ByVal documentId As String)
          Dim contentType As String = String.Empty
          Dim bytes As Byte() = Nothing
          Dim docItem As DocumentEntity = New DocumentEntity()
  
          If (documentId IsNot Nothing) Then
              docItem = New DocumentDao().SelectByDocumentID(documentId)
  
              If Not String.IsNullOrEmpty(docItem.auto_key.Trim()) Then
                  HttpContext.Current.Response.Clear()
                  HttpContext.Current.Response.ClearContent()
                  HttpContext.Current.Response.ClearHeaders()
                  HttpContext.Current.Response.ContentType = GetContentType(docItem.fileexten)
                  HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment; filename=" &amp; docItem.filename.Trim &amp; "." &amp; docItem.fileexten.Trim)
  
                  If (docItem.document IsNot Nothing) AndAlso (docItem.document IsNot System.DBNull.Value) Then
                      HttpContext.Current.Response.BinaryWrite(docItem.document)
                  End If
  
                  HttpContext.Current.Response.Flush()
                  HttpContext.Current.Response.Close()
                  HttpContext.Current.Response.End()
              End If
          End If
      End Sub
  End Class
  </code></pre>
  
  <p>Now, my users download docx file from my web application.</p>
  
  <ul>
  <li><p><strong>On iPad and iPhone</strong>: When they click on the link to download that docx, browsers (Safari, Chrome) just open a new blank tab. And no files are downloaded, or even though we cannot preview the docx content in that new tab.</p></li>
  <li><p>BUT, browsers (Safari, Chrome) on MAC or PC still work normally. The docx can be able to download as expected. (When we click the link to download the docx, a new tab will be opened. Then, an Save File dialog will show up to ask you the location to save the file. After we save the file, the new tab on the browser will be closed automatically).</p></li>
  <li><p>If we switch to download a <strong>doc</strong> file (not docx), Safari/Chome on iPad/iPhone will open a new tab and display the content of that <strong>doc</strong> file (preview mode) and it does not allow us to download that file, just allows to preview the file.</p></li>
  </ul>
  
  <p>So, my question here is why browsers on iPad/iPhone cannot download the docx? Is there anyway to do that? Or is this an expected behavior of iOS for iPad/iPhone to secure the end user?</p>