我在我的Angular.js控制器中有下载CSV文件:
var blob = new Blob([csvContent.join('')], { type: 'text/csv;charset=utf-8'});
var link = document.createElementNS('http://www.w3.org/1999/xhtml', 'a');
link.href = URL.createObjectURL(blob);
link.download = 'teams.csv';
link.click();
这适用于Chrome,但不适用于IE。浏览器控制台日志说:
HTML7007:通过关闭为其创建的blob来撤消一个或多个blob URL。这些URL将不再解析,因为已释放支持URL的数据。
它是什么意思,我该如何解决?
尝试使用,this
或useragent
if (navigator.appVersion.toString().indexOf('.NET') > 0)
window.navigator.msSaveBlob(blob, filename);
else
{
var blob = new Blob(['stringhere'], { type: 'text/csv;charset=utf-8' });
var link = document.createElementNS('http://www.w3.org/1999/xhtml', 'a');
link.href = URL.createObjectURL(blob);
link.download = 'teams.csv';
link.click();
}
IE不允许你直接打开blob。你必须使用msSaveOrOpenBlob
。还有msSaveBlob
if (window.navigator && window.navigator.msSaveOrOpenBlob) {
window.navigator.msSaveOrOpenBlob(blob, fileName);
} else {
var objectUrl = URL.createObjectURL(blob);
window.open(objectUrl);
}
我需要使用Blob来下载转换后的base64 PNG图像。我能够用window.navigator.msSaveBlob
在IE11上成功下载blob
请参阅以下msdn链接:http://msdn.microsoft.com/en-us/library/hh779016(v=vs.85).aspx
具体来说,你应该打电话:
window.navigator.msSaveBlob(blobObject, 'msSaveBlob_testFile.txt');
其中blobObject
是以通常方式创建的Blob。
这个页面上有很多不错的东西,但是我必须结合使用一些东西来完成所有工作。希望这会对你有所帮助。
download()
的函数:<button class="button-no save-btn" ng-click="download()">DOWNLOAD</button>
$scope.download = function () {
// example shows a JSON file
var content = JSON.stringify($scope.stuffToPutInFile, null, " ");
var blob = new Blob([content], {type: 'application/json;charset=utf-8'});
if (window.navigator && window.navigator.msSaveBlob) {
// Internet Explorer workaround
$log.warn("Triggering download using msSaveBlob");
window.navigator.msSaveBlob(blob, "export.json");
} else {
// other browsers
$log.warn("Triggering download using webkit/
var url = (window.URL || window.webkitURL).createObjectURL(blob);
// create invisible element
var downloadLink = angular.element('<a></a>');
downloadLink.attr('href', url);
downloadLink.attr('download', 'export.json');
// make link invisible and add to the DOM (Firefox)
downloadLink.attr('style','display:none');
angular.element(document.body).append(downloadLink);
// trigger click
downloadLink[0].click();
}
};
你的IE浏览器版本是什么?你需要一个现代浏览器或IE10 + http://caniuse.com/bloburls
也许你需要一些延迟。怎么样:
link.click();
setTimeout(function(){
document.body.createElementNS('http://www.w3.org/1999/xhtml', 'a');
URL.revokeObjectURL(link.href);
}, 100);
我需要让下载功能在Chrome和IE11中运行。我在这段代码上取得了很大的成功。
HTML
<div ng-repeat="attachment in attachments">
<a ng-click="openAttachment(attachment)" ng-href="{{attachment.fileRef}}">{{attachment.filename}}</a>
</div>
JS
$scope.openAttachment = function (attachment) {
if (window.navigator && window.navigator.msSaveOrOpenBlob) {
window.navigator.msSaveOrOpenBlob(
b64toBlob(attachment.attachment, attachment.mimeType),
attachment.filename
);
}
};
这样做,对我来说很好。
downloadFile(data) {
if (navigator.msSaveBlob) {
let blob = new Blob([data], {
"type": "text/csv;charset=utf8;"
});
navigator.msSaveBlob(blob, this.fileName);
}
else {
let blob = new Blob(['\ufeff' + data], { type: 'text/csv;charset=utf-8;' });
let $link = document.createElement("a");
let url = URL.createObjectURL(blob);
$link.setAttribute("target", "_blank");
$link.setAttribute("href", url);
$link.setAttribute("download", this.fileName);
$link.style.visibility = "hidden";
document.body.appendChild($link);
$link.click();
document.body.removeChild($link);
}
}
尝试使用它:var blob = file.slice(0,file.size);