HTTP标头“内容类型:应用程序/强制下载"的实用程序;为手机? [英] Utility of HTTP header "Content-Type: application/force-download" for mobile?

查看:21
本文介绍了HTTP标头“内容类型:应用程序/强制下载"的实用程序;为手机?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发一个 PHP 脚本,该脚本允许您通过访问链接从您的移动设备下载媒体内容(视频、音频、图片...).(即 http://www.my-web-site.com/download.php?id=7ejs8ap)当我用最近的手机(三星 Galaxy S、iPhone 4S、其他一些……)测试它时,我的脚本运行良好,但在我的旧手机三星 C3050 上出现错误.我想下载的媒体只是一个音频mp3文件,我通常很容易下载.

I am currently working on a PHP script that allows you to download media contents (video, audio, pictures...) from your mobile device by accessing a link. (i.e. http://www.my-web-site.com/download.php?id=7ejs8ap) My script worked very vell when I was testing it with recent mobile (Samsung Galaxy S, iPhone 4S, some others...) but an error occured on my old mobile Samsung C3050. The media I wanted to download was just an audio mp3 file that I usually download easily.

错误似乎是未知的内容类型".因此,由于我唯一的 HTTP 标头 Content-Type 是application/force-download",我尝试对此发表评论并重试.然后,它起作用了.但是现在,我目前正在问这个 Content-Type 是什么意思,以及它是否可以强制用于其他移动设备.我在 iPhone 4 上没有使用 Content-Type 进行测试,它可以工作,但我不确定这种兼容性是否适用于所有移动设备.

The error appears to be "Unknown content type." So, as my only HTTP header Content-Type was "application/force-download", I try to comment this and try again. Then, it works. But now, I am currently asking what this Content-Type means and if it can be mandatory for others mobile. I tested without the Content-Type on the iPhone 4 and it works, but I'm not sure of this compatibility for all mobile.

有人可以向我解释一下 Content-Type 是如何工作的,为什么这不是标准 MIME 或其他所有可以帮助我确保这是每次下载的可选 Content-Type,无论文件、浏览器或我正在下载的设备?

Can someone explain me how that Content-Type works, why this isn't a standard MIME or everything else that can help me to be sure this is an optionnal Content-Type for every download, whatever the file, the browser or the device I am downloading on?

谢谢大家.

这是我发送的 PHP 标头:

Here is my PHP headers sent:

<?php
//Assume that $filename and $filePath are correclty set.
header('Content-Description: File Transfer');
header('Content-Disposition: attachment; filename="'.$filename.'"');
// header('Content-Type: application/force-download'); Non-standard MIME-Type, incompatible with Samsung C3050 for example. Let it commented
readfile($filePath);
?>

我刚刚尝试使用 Sony Xperia,但下载不成功:我只看到我想要下载的文件的html 编码"字节.如果 application/octet-stream 或 application/force-download 不起作用,我怎么知道我必须使用什么内容类型?

EDIT : I just tried with a Sony Xperia, and the download wasn't successful: I only see the "html-encoded" bytes of my file I want to download. How can I know what content-type I have to use if application/octet-stream or application/force-download doesn't work?

推荐答案

Content-Type: application/force-download 的意思是我,网络服务器,会骗你(浏览器) 了解此文件是什么,这样您就不会将其视为 PDF/Word 文档/MP3/任何内容,而是提示用户将神秘文件保存到磁盘".这是一个肮脏的黑客,当客户端不执行保存到磁盘"时会严重破坏.

Content-Type: application/force-download means "I, the web server, am going to lie to you (the browser) about what this file is so that you will not treat it as a PDF/Word Document/MP3/whatever and prompt the user to save the mysterious file to disk instead". It is a dirty hack that breaks horribly when the client doesn't do "save to disk".

为您使用的任何媒体使用正确的 mime 类型(例如 audio/mpeg 用于 mp3).

Use the correct mime type for whatever media you are using (e.g. audio/mpeg for mp3).

使用Content-Disposition:附件;etc etc 标头,如果您想鼓励客户端下载它而不是遵循默认行为.

Use the Content-Disposition: attachment; etc etc header if you want to encourage the client to download it instead of following the default behaviour.

这篇关于HTTP标头“内容类型:应用程序/强制下载"的实用程序;为手机?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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