JS直接下载PDF文件

if (ext == "pdf") {
var index = _fileUri.lastIndexOf('/');
var name = _fileUri.substr(index + 1);

var xhr = new XMLHttpRequest();
xhr.open('get', _fileUri, true);
xhr.responseType = 'blob';
xhr.onreadystatechange = function () {
if (xhr.readyState == 4 && xhr.status == 200) {
if (typeof window.chrome !== 'undefined') {
// Chrome version
var link = document.createElement('a');
link.href = window.URL.createObjectURL(xhr.response);
link.download = name;
link.click();
} else if (typeof window.navigator.msSaveBlob !== 'undefined') {
// IE version
var blob = new Blob([xhr.response], { type: 'application/pdf' });
window.navigator.msSaveBlob(blob, name);
} else {
// Firefox version
var file = new File([req.response], name, { type: 'application/force-download' });
window.open(URL.createObjectURL(file));
}
}
};
xhr.send();
}

posted @ 2024-02-02 11:44  冰山一杰  阅读(816)  评论(0编辑  收藏  举报