下载前端项目中自带的文件到本地
作者:互联网
function download(down_questionNo, type) {
$.ajax({
url: "/comtion-web/doc/" + down_questionNo + type, //本地文件存放的位置
type: "GET",
async: false,
mimeType: "text/plain; charset=x-user-defined", //jq ajax请求文件流的方式 (起作用的重点)
success: function (data) {
var rawLength = data.length;
var array = new Uint8Array(new ArrayBuffer(rawLength));
for (i = 0; i < rawLength; i++) {
array[i] = data.charCodeAt(i) & 0xff;
}
//上面是把后台请求到的文件流进行转化为符合的流
var blob = new Blob([array], { type: "application/pdf;charset-UTF-8" });
var a = document.createElement("a");
var event = new MouseEvent("click");
a.download = down_questionName + type; //文件名
a.href = window.URL.createObjectURL(blob);
a.dispatchEvent(event);
},
error: function () {
console.log("error");
},
});
}
标签:function,前端,down,本地,var,new,自带,type,array 来源: https://www.cnblogs.com/33shan/p/16658156.html