HTML / JavaScript:如何使用IE下载文件

时间:2015-11-16 13:25:24

标签: javascript jquery html internet-explorer

我使用下面的代码使用Chrome从HTML中的textarea下载。 我想知道如何在使用IE(Internet Explorer)

时下载此内容
function saveTextAsFile1()
{      
// grab the content of the form field 
    var textToWrite = document.getElementById("fileToLoad").files[0];
    var textFileAsBlob = new Blob([textToWrite], {type:'xlsx'});
    var fileNameToSaveAs = "LANG_TEST.xlsx";
    var downloadLink = document.createElement("a");
    downloadLink.download = fileNameToSaveAs;
    downloadLink.innerHTML = "My Hidden Link";
    window.URL = window.URL || window.webkitURL;
    downloadLink.href = window.URL.createObjectURL(textFileAsBlob);
    downloadLink.onclick = destroyClickedElement;
    downloadLink.style.display = "none";
    document.body.appendChild(downloadLink);
    downloadLink.click();
}

谢谢: - )

1 个答案:

答案 0 :(得分:0)

试试这个......

$filepath = str_replace('\\', '/', realpath($filename));
$filesize = filesize($filepath);
$filename = substr(strrchr('/'.$filepath, '/'), 1);
$extension = strtolower(substr(strrchr($filepath, '.'), 1));

// use this unless you want to find the mime type based on extension
$mime = array('application/octet-stream');

header('Content-Type: '.$mime);
header('Content-Disposition: attachment; filename="'.$filename.'"');
header('Content-Transfer-Encoding: binary');
header('Content-Length: '.sprintf('%d', $filesize));
header('Expires: 0');

// check for IE headers Important
if (preg_match('~MSIE|Internet Explorer~i', $_SERVER['HTTP_USER_AGENT']) || (strpos($_SERVER['HTTP_USER_AGENT'], 'Trident/7.0; rv:11.0') !== false)) {
  header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
  header('Pragma: public');
} else {
  header('Pragma: no-cache');
}

$handle = fopen($filepath, 'rb');
fpassthru($handle);
fclose($handle);