私は自分で解決策を見つけました。テクニックをAJAX
使う代わりに。Iframe
次jQuery
のコードを iframe に記述します。AJAX
サーバーにリクエストを送信する代わりに、iframe を介してダウンロードをトリガーします。
$(document).ready(function(){
$('#download').click(function(){
var ifr = $('<iframe id="secretIFrame" src="" style="display:none; visibility:hidden;"></iframe>');
$('body').append(ifr);
var iframeURL = 'http://you_domain.com/download.php?str='+encodeURIComponent(data_to_be_posted);
ifr.attr('src', iframeURL);
return false;
});
});
これはのコードですdownload.php
:
<?php
$str = html_entity_decode($_REQUEST['str']);
$file_name = 'export_data.txt';
header("Cache-Control: ");// leave blank to avoid IE errors
header("Pragma: ");// leave blank to avoid IE errors
header("Content-Disposition: attachment; filename=\"".$file_name."\"");
header("Content-length:".(string)(filesize($str)));
header("Content-Type: application/force-download");
header("Content-Type: application/download");
header('Content-Type: application/octet-stream');
header('Content-Type: application/txt');
header("Content-Transfer-Encoding: binary ");
echo $str;
exit;
?>