23

我需要能够将字符串保存到本地文件中。根据这里的代码,我得到了以下内容:

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.

对此有什么解释/解决方案吗?

谢谢!

4

5 回答 5

33

感谢 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();
  }
}
于 2014-11-27T00:12:32.717 回答
33

IE 10 和 11 使用不同的语法将 blob 下载或保存到客户端计算机。创建 Blob 后,请使用:

window.navigator.msSaveBlob(blob, 'file.txt'); 

或者

window.navigator.msSaveOrOpenBlob(blob, 'file.txt');

触发文件保存或文件保存/打开对话框。

有关详细信息,请参阅http://msdn.microsoft.com/en-us/library/ie/hh673542(v=vs.85).aspx

于 2014-01-04T23:13:13.043 回答
9

对于现代浏览器,解决方案是这样的,经过测试: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);	
}

于 2016-03-25T14:28:11.603 回答
3

加上即边缘:

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 { ... }
于 2015-09-04T22:53:04.897 回答
0

您可以使用FileSaver库来实现这一点。易于使用,照顾浏览器类型和版本。还有一个AngularJS版本可用。

于 2016-10-14T09:21:21.657 回答