jquery調用文件流獲取文件(ajax)



function workDownLoad(workID) {
var fileName = "";
var status = false;
$.ajax({
type: "Post",
url: "/Common/GetWorkOrPlayName",
datatype: "json",
data: {
id: workID,
type: 0
},
async: false,
success: function (data) {
if (data.status == 0) {
fileName = data.WorkName;
status = true;
} else {
status = false;
}

},
error: function () { }
});

if (!status) {
return;
}

var url = "/Common/WorkDownLoad/" + workID;
var xhr = new XMLHttpRequest();
xhr.open('GET', url, true);//get請求,請求地址,是否異步
xhr.responseType = "blob"; // 返回類型blob
xhr.onload = function (data, textStatus, request) {// 請求完成處理函數
if (this.status === 200) {
var blob = this.response;// 獲取返回值
var a = document.createElement('a');
//var name = request.getResponseHeader('Content-Disposition');
a.download = fileName;
a.href = window.URL.createObjectURL(blob);
a.click();
}
};
// 發送ajax請求
xhr.send();

}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM