在 Android 上通过浏览器下载文件时避免内容类型问题
如果我通过 web 应用程序向浏览器提供了一个文件,我通常只需将 URL 设置为 http://website.com/webapp/download/89347/image.jpg
之类的内容。然后我设置 HTTP 标头 Content-Type: application/octet-stream; filename=image.jpg
和 Content-Disposition:附件
。
然而,在Android上。看来我下载文件的唯一方法是设置 Content-Type: image/jpg 。否则文件名显示
并且出现错误
下载失败
无法下载。此手机不支持该内容
有什么方法可以让 Android 通过浏览器下载并打开文件,而不保留 mime 类型列表?
If I have a file made available to a browser through my webapp, I normally just set the URL to something like http://website.com/webapp/download/89347/image.jpg
. I then set the HTTP headers Content-Type: application/octet-stream; filename=image.jpg
and Content-Disposition: Attachment
.
However, on the Android. It seems the only way I can get the file to download is to set Content-Type: image/jpg
. Otherwise the file name says <Unknown>
and an error comes
Download unsuccessful
Cannot download. The content is not supported on this phone
Is there any way I can get Android to download and open the file through the browser without keeping a list of mime types?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
要使任何下载按预期在所有(尤其是较旧的)Android 版本上运行,您需要...
阅读我的博客文章了解更多详细信息:
http://digiblog.de/2011/04/ 19/android-and-the-download-file-headers/
To make any downloads work on all (and especially older) Android versions as expected, you need to...
Read my blog post for more details:
http://digiblog.de/2011/04/19/android-and-the-download-file-headers/
Dmitriy(或其他正在寻找可能解决方案的人)如果您下载的文件中出现 html 页面,我怀疑这是由于双重 HttpRequest GET 问题造成的。典型的场景是以下 POST、重定向、GET 模型:
Android 浏览器向服务器发出 HttpRequest POST(例如提交按钮或链接以请求下载文件,例如 filename.ext)
服务器将请求的 filename.ext 流式传输到字节,存储在会话变量中,然后发出 Response.Redirect 到 Download.aspx例如,处理响应对象构造
服务器以典型的 Content-Disposition 进行响应:附件; filename="filename.ext" 样式构造,响应对象包含所请求的 filename.ext,即会话变量中的字节。
我相信,Android 下载管理器会向服务器发送另一个 HttpRequest GET 以获取 Download.aspx。我怀疑下载管理器将先前的“附件”响应解释为发送第二个 GET 的触发器。
服务器 (Download.aspx) 再次尝试构造响应对象以发送回浏览器。
Android 下载管理器使用第二个 Download.aspx 中的响应对象内容下载 filename.ext。
在很多情况下这都可以。但是,例如,如果 Download.aspx 代码中的服务器执行一些内务处理并在第一次调用时删除会话变量,那么下一次就没有会话变量了。因此,根据代码的编写方式,响应对象可能不会被显式构造,并且 Response.End 可能不会被调用,因此只有 Download.aspx 的 html 最终被发送。
这是我们使用 Wireshark 发现的,尽管我承认我假设是 Android 下载管理器导致了双重 GET。
我希望这个解释对您有所帮助。
Dmitriy (or others looking for a possible solution) if an html page is appearing in your downloaded file, I suspect this is due to the double HttpRequest GET issue. A typical scenario is the following POST, Redirect, GET model:
Android browser issues a HttpRequest POST to server (e.g. submit button or link to request a download file, filename.ext say)
Server streams the requested filename.ext to bytes, stores in a session variable, and then issues a Response.Redirect to Download.aspx, for example, to handle the response object construction
Android browser correctly sends HttpRequest GET to server for Download.aspx
Server responds with typical Content-Disposition: attachment; filename="filename.ext" style construct with the response object containing the requested filename.ext, being the bytes in the session variable.
Android download manager, I believe, then sends another HttpRequest GET to server for Download.aspx. I suspect that the download manager interprets the previous "attachment" response as a trigger to send this second GET.
Server (Download.aspx) again tries to construct the response object to send back to the browser.
Android download manager downloads filename.ext, using the response object contents from the second Download.aspx.
In many scenarios this would be fine. But if, for example, the server in the Download.aspx code does some housekeeping and removes the session variable the first time it is called, then the next time around there is no session variable. So, depending on how the code is written it is possible that the response object doesn't get explicity constructed and maybe the Response.End doesn't get called and so only the Download.aspx's html ends up being sent.
This is what we discovered using Wireshark, although I admit I am assuming it is the Android download manager that is the cause for the double GET.
I hope this explanation has been of some help.
正如我在 从 android 下载文件中写道:
Android 浏览器不会在按钮发布事件中下载文件。在后期事件中,该文件将是一些 .htm 垃圾文件。要克服这个问题,请执行以下操作。
在下载按钮中单击
As I wrote at downloading files from android:
Android browser will not download file in button Post events. In post events the file will be some .htm garbage file. to over come this do as below.
In download button click
我已经尝试了 Jspy 博客的所有建议,但到目前为止没有任何效果。内容处置使浏览器进入下载模式,但是除了启动下载的页面的 HTML 之外,没有下载任何内容。所以我的结论是,这纯粹是谷歌的错误,我们只能祈祷谷歌修复它。我的解决方法是将内容类型设置为来自移动浏览器接受标头的某种类型。它通常可以工作,您甚至可以将 zip 文件下载为文本。
I have tried all recommendations from Jspy blog and nothing worked so far. Content-disposition brings browser in downloading mode, however nothing gets downloaded except HTML of page from which download got initiated. So my conclusion, it is pure bug from Google and we may only pray for that Google fix it. My work around was to set content type to some type coming from Accept header form mobile browser. It generally works, you can even download zip files as text.
理论上,文件名参数应该设置在Content-Disposition上,而不是Content-Type上。不确定这是否对 Android 浏览器有帮助。
In theory, the filename parameter should be set on Content-Disposition, not Content-Type. Not sure whether this will help with the Android browser.