如何强制文件中使用移动浏览器PHP下载? [英] How to Force a file to download using PHP on mobile Browsers?

查看:210
本文介绍了如何强制文件中使用移动浏览器PHP下载?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要写一个PHP脚本下载一些文件 - 移动浏览器(扩展名.apk文件,.dcm,.PDF,.zip文件等)。我已经写一个PHP code下载这些文件,它是在所有的浏览器(而不是移动浏览器)工作的罚款。但我想用HTC手机,并试图打开,而不是下载(如打开一个网页)的文件了。

I want to write a php script to download some files(extensions - .apk, .dcm, .pdf, .zip etc...) on mobile browsers. I have written a php code to download those files and it is working fine on all the browsers(not mobile browsers). But I tried it using a HTC mobile and it is trying to open the file instead of downloading(like opening a web page).

我怎样才能使移动浏览器的下载?

How can I enable the download on mobile browsers?

感谢您

PS:
这就是我要做的事。
我用一个jQuery code一些参数发送到PHP文件,它会返回相应的下载文件的路径。

PS: This is how I do it. I use a jquery code to send some parameters to a php file and it will return the appropriate download file path.

jQuery的code片段是:

jQuery code snippets is:

$.post('saveData.php',{ name: name.val(), email: email.val(), phone:phone.val(), address:address.val(), version:vers, swVersion:swvers, type:type }, 
function(data) 
{                       
    var links = data;
    document.body.innerHTML += "<iframe src='" + links + "' style='display:hide;' ></iframe>";
});

链接变量包含PHP文件下载路径的回报。
IFRAME允许下载窗口弹出。不过,这并不在移动浏览器的工作。

"links" variable contains the download path return from the php file. Iframe allow the download window to popup. But it does not work on the mobile browsers.

推荐答案

尝试用下面的标题:

header('Content-Type: application/force-download');
header('Content-Disposition: attachment; filename="filename"');

我们的想法是设置内容类型的东西,浏览器不知道如何打开 - 这样它会显示保存对话框。你可以用实际的MIME类型尝试,它应该工作,但现在我无法进行测试。

The idea is to set the content type to something that the browser doesn't know how to open - that way it will show the save dialogue. You can try it with the actual MIME type, it should work, but I can't test it right now.

这篇关于如何强制文件中使用移动浏览器PHP下载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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