为什么 iPhone/iPad 上的浏览器不能 download/preview docx 文件?
Why browsers on iPhone/iPad cannot download/preview docx files?
我的 Web 应用程序允许最终用户上传文档并将其存储在数据库中。他们也可以从应用程序下载该文档。
下载文档的行为很简单,用户在GUI上点击一个link "Download this document",我会根据请求的输入documentID参数从数据库中查询文档二进制数据客户端。之后,我会将字节数据响应给客户端。
让我们看看我的代码。
这是我接收客户端下载文档请求的函数。
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
这就是我查询二进制数据并响应给客户端的功能。
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
现在,我的用户从我的 Web 应用程序下载 docx 文件。
在 iPad 和 iPhone 上:当他们点击 link 下载该 docx 时,浏览器 ( Safari,Chrome) 只需打开一个新的空白选项卡。并且没有文件被下载,或者即使我们无法在该新选项卡中预览 docx 内容。
但是,MAC 或 PC 上的浏览器(Safari,Chrome)仍然可以正常工作。 docx 可以按预期下载。 (当我们点击 link 下载 docx 时,会打开一个新标签。然后,会出现一个保存文件对话框,询问您保存文件的位置。保存文件后,新标签在浏览器上会自动关闭)。
如果我们切换到下载 doc 文件(不是 docx),iPad/iPhone 上的 Safari/Chome 将打开一个新选项卡并显示 doc 文件的内容(预览模式),它不允许我们下载该文件,只允许预览文件。
所以,我的问题是为什么 iPad/iPhone 上的浏览器无法下载 docx?有没有办法做到这一点?或者这是 iOS 为 iPad/iPhone 保护最终用户的预期行为?
我找到了这个问题的答案。只是 return docx 的正确 ContentType
https://docs.microsoft.com/en-us/previous-versions/office/office-2007-resource-kit/ee309278(v=office.12)
我的 Web 应用程序允许最终用户上传文档并将其存储在数据库中。他们也可以从应用程序下载该文档。
下载文档的行为很简单,用户在GUI上点击一个link "Download this document",我会根据请求的输入documentID参数从数据库中查询文档二进制数据客户端。之后,我会将字节数据响应给客户端。
让我们看看我的代码。
这是我接收客户端下载文档请求的函数。
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
这就是我查询二进制数据并响应给客户端的功能。
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
现在,我的用户从我的 Web 应用程序下载 docx 文件。
在 iPad 和 iPhone 上:当他们点击 link 下载该 docx 时,浏览器 ( Safari,Chrome) 只需打开一个新的空白选项卡。并且没有文件被下载,或者即使我们无法在该新选项卡中预览 docx 内容。
但是,MAC 或 PC 上的浏览器(Safari,Chrome)仍然可以正常工作。 docx 可以按预期下载。 (当我们点击 link 下载 docx 时,会打开一个新标签。然后,会出现一个保存文件对话框,询问您保存文件的位置。保存文件后,新标签在浏览器上会自动关闭)。
如果我们切换到下载 doc 文件(不是 docx),iPad/iPhone 上的 Safari/Chome 将打开一个新选项卡并显示 doc 文件的内容(预览模式),它不允许我们下载该文件,只允许预览文件。
所以,我的问题是为什么 iPad/iPhone 上的浏览器无法下载 docx?有没有办法做到这一点?或者这是 iOS 为 iPad/iPhone 保护最终用户的预期行为?
我找到了这个问题的答案。只是 return docx 的正确 ContentType https://docs.microsoft.com/en-us/previous-versions/office/office-2007-resource-kit/ee309278(v=office.12)