直接上代碼:
downPic({ res_id: _this.rowDataForImg.res_id }).then(resp => { // 請(qǐng)求的接口
if (resp.status == 200) {
let url = window.URL.createObjectURL(new Blob([resp.body])); // 將文件流轉(zhuǎn)成url,這里轉(zhuǎn)換出來的url是不帶圖片后綴的讲逛,所以在設(shè)置文件名的時(shí)候名船,需要帶上后綴阴颖,不然下載的文件,將不是圖片格式激况,有可能是txt格式的文件流繁涂,或者是jfif格式的圖片
let link = document.createElement("a"); // 創(chuàng)建一個(gè)a標(biāo)簽
link.style.display = "none";
link.href = url;
link.setAttribute("download", '網(wǎng)絡(luò)拓?fù)鋱D.jpg'); // 一定要設(shè)置download第练,不然點(diǎn)擊以后只是在新頁面打開圖片而不是下載,圖片名的后綴要是自己要的格式
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
} else {
this.$Message.error(resp.body.resultMsg);
}
});