注意:该方法仅适用于浏览器端,兼容 IE10+ 和现代浏览器。
utilHelpers.download("hello world Ò", {
fileName: "text-string.txt",
type: "text/plain"
});
utilHelpers.download("data:text/plain,hello%20world", {
fileName: "text-dataurl.txt",
type: "text/plain"
});
utilHelpers.download(new Blob(["hello world"]), {
fileName: "text-blob.txt",
type: "text/plain"
});
utilHelpers.download("./test.txt", { dataType: "url" });
var str = "hello world",
arr = new Uint8Array(str.length);
str.split("").forEach(function (s, i) {
arr[i] = str.charCodeAt(i);
});
utilHelpers.download(arr, "text-uint8.txt");
utilHelpers.download(document.documentElement.outerHTML, {
fileName: "html-string.html",
type: "text/html"
});
utilHelpers.download(new Blob(["hello world".bold()]), {
fileName: "html-blob.html",
type: "text/html"
});
var xhr = new XMLHttpRequest();
xhr.open("GET", "./index.html");
xhr.responseType = "blob";
xhr.onload = function (e) {
utilHelpers.download(e.target.response, {
fileName: "html-ajax.html",
type: "text/html"
});
};
xhr.send();
// utilHelpers.download('./index.html', { dataType: 'url' });
utilHelpers.download("./test.jpeg", { dataType: "url" });
var xhr = new XMLHttpRequest();
xhr.open("GET", "./test.jpeg");
xhr.responseType = "blob";
xhr.onload = function (e) {
utilHelpers.download(e.target.response, "image-ajax.jpg");
};
xhr.send();
var xhr = new XMLHttpRequest();
xhr.open('GET', './test.xlsx'); // 也可能是 POST 请求方式
xhr.responseType = 'arraybuffer';
xhr.onload = function (e) {
// const blob = new Blob([e.target.response], { type: 'application/vnd.ms-excel' });
// utilHelpers.download(blob, 'excel-ajax.xlsx');
utilHelpers.download(e.target.response, 'excel-ajax.xlsx');
};
xhr.send();
utilHelpers.download("./test.docx", { dataType: "url" });
utilHelpers.download("./test.xlsx", { dataType: "url" });
utilHelpers.download("./test.pdf", { dataType: "url" });
utilHelpers.download("./test.mp3", { dataType: "url" });
utilHelpers.download("./test.mp4", { dataType: "url" });
utilHelpers.download("./test.zip", { dataType: "url" });