在 Android 上通过浏览器下载文件时避免内容类型问题 [英] Avoiding content type issues when downloading a file via browser on Android

查看:26
本文介绍了在 Android 上通过浏览器下载文件时避免内容类型问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个文件通过我的 web 应用程序提供给浏览器,我通常只是将 URL 设置为类似 http://website.com/webapp/download/89347/image.jpg.然后我设置 HTTP 标头 Content-Type: application/octet-stream;filename=image.jpgContent-Disposition: Attachment.

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.

但是,在 Android 上.似乎我可以下载文件的唯一方法是设置 Content-Type: image/jpg.否则文件名显示 并且出现错误

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

有什么办法可以让 Android 在不保留 mime 类型列表的情况下通过浏览器下载和打开文件?

Is there any way I can get Android to download and open the file through the browser without keeping a list of mime types?

推荐答案

要使任何下载按预期在所有(尤其是较旧的)Android 版本上正常运行,您需要...

To make any downloads work on all (and especially older) Android versions as expected, you need to...

  1. 将 ContentType 设置为 application/octet-stream
  2. 将 Content-Disposition 文件名值放在双引号中
  3. 以大写形式写入 Content-Disposition 文件扩展名

阅读我的博文了解更多详情:
http://digiblog.de/2011/04/19/android-and-the-download-file-headers/

Read my blog post for more details:
http://digiblog.de/2011/04/19/android-and-the-download-file-headers/

这篇关于在 Android 上通过浏览器下载文件时避免内容类型问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆