[英]Saving text in a local file in Internet Explorer 10
我需要能夠將字符串保存到本地文件中。 根據此處的代碼,我可以進行以下操作:
function saveTextAsFile(fileNameToSaveAs, textToWrite) {
var textFileAsBlob = new Blob([textToWrite], {
type: 'text/plain'
});
var downloadLink = document.createElement("a");
downloadLink.download = fileNameToSaveAs;
downloadLink.innerHTML = "Download File";
if (true) { //window.webkitURL !== null) {
// Chrome allows the link to be clicked
// without actually adding it to the DOM.
downloadLink.href = window.URL.createObjectURL(textFileAsBlob);
} else {
// Firefox requires the link to be added to the DOM
// before it can be clicked.
downloadLink.href = window.URL.createObjectURL(textFileAsBlob);
downloadLink.onclick = destroyClickedElement;
downloadLink.style.display = "none";
document.body.appendChild(downloadLink);
}
downloadLink.click();
}
對於Chrome和Firefox來說,這工作正常,但對於Internet Explorer 10,效果不佳
downloadLink.click();
給出:
SCRIPT5: Access is denied.
是否對此有任何解釋/解決方案?
謝謝!
IE 10和11使用不同的語法將blob下載或保存到客戶端計算機。 創建Blob后,請使用:
window.navigator.msSaveBlob(blob, 'file.txt');
要么
window.navigator.msSaveOrOpenBlob(blob, 'file.txt');
觸發文件保存或文件保存/打開對話框。
有關更多信息,請參見http://msdn.microsoft.com/zh-cn/library/ie/hh673542(v=vs.85).aspx
謝謝mstubna,這是Chrome,FF和IE> 9的解決方案:
function saveTextAsFile(fileNameToSaveAs, textToWrite) {
/* Saves a text string as a blob file*/
var ie = navigator.userAgent.match(/MSIE\s([\d.]+)/),
ie11 = navigator.userAgent.match(/Trident\/7.0/) && navigator.userAgent.match(/rv:11/),
ieEDGE = navigator.userAgent.match(/Edge/g),
ieVer=(ie ? ie[1] : (ie11 ? 11 : (ieEDGE ? 12 : -1)));
if (ie && ieVer<10) {
console.log("No blobs on IE ver<10");
return;
}
var textFileAsBlob = new Blob([textToWrite], {
type: 'text/plain'
});
if (ieVer>-1) {
window.navigator.msSaveBlob(textFileAsBlob, fileNameToSaveAs);
} else {
var downloadLink = document.createElement("a");
downloadLink.download = fileNameToSaveAs;
downloadLink.href = window.URL.createObjectURL(textFileAsBlob);
downloadLink.onclick = function(e) { document.body.removeChild(e.target); };
downloadLink.style.display = "none";
document.body.appendChild(downloadLink);
downloadLink.click();
}
}
對於現代瀏覽器,解決方案是經過測試的:IE11,FF和Chrome
var csvData = new Blob([arg.data], {type: 'text/csv;charset=utf-8;'}); //IE11 & Edge if (navigator.msSaveBlob) { navigator.msSaveBlob(csvData, exportFilename); } else { //In FF link must be added to DOM to be clicked var link = document.createElement('a'); link.href = window.URL.createObjectURL(csvData); link.setAttribute('download', exportFilename); document.body.appendChild(link); link.click(); document.body.removeChild(link); }
加上例如EDGE:
var ieEDGE = navigator.userAgent.match(/Edge/g);
if (ie || ie11 || ieEDGE) {
if (ieVer>9 || ieEDGE) {
var textFileAsBlob = new Blob([textToWrite], {
type: 'text/plain'
});
window.navigator.msSaveBlob(textFileAsBlob, fileName);
} else {
console.log("No supported on IE ver<10");
}
} else { ... }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.