Click here to Skip to main content
14,699,232 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
My web application allows end users to upload and store documents in the database. And they can download that documents from the app, too.

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.

Let's see my code.

This is the function where I receive requests of downloading docs from the client.

Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
    Dim DocumentID As String = getParameter("DocumentID", context)
    DocumentWrapper.Download(DocumentID)
    context.Response.Write("<script language=javascript>window.open('','_self','');window.close();</script>")
End Sub


And this is the function that I query the binary data and response it to the client.

 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=" & docItem.filename.Trim & "." & 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


Now, my users download docx file from my web application.

- On iPad and iPhone: 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.

- 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).

- If we switch to download a doc file (not docx), Safari/Chome on iPad/iPhone will open a new tab and display the content of that doc file (preview mode) and it does not allow us to download that file, just allows to preview the file.

So, my questions here are 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?

Please advice me! Thanks so much!

What I have tried:

I did verify and compare the behavior of browsers (Safari, Chrome) between Android, iOS and Windows as described in "Describe the problem" above
Posted

1 solution

I found the solution for this question.
Just return the correct ContentType for docx, and the issue will be solved for iPhone and iPad
Register the 2007 Office system file format MIME types on servers | Microsoft Docs[^]
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900