开发者

How to Force a file to download using PHP on mobile Browsers?

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?

Thank You

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 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.b开发者_如何学Pythonody.innerHTML += "<iframe src='" + links + "' style='display:hide;' ></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.


Try with the following headers:

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

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜