JavaScript下載后端返回的文件流的三種方法
方法一
后端返回的結(jié)果

/* 創(chuàng)建一個(gè)js寫(xiě)以下代碼 */
import axios from 'axios';
/*
先安裝: npm install js-file-download
main.js引入 import fileDownload from 'js-file-download';
* */
/*
* url: 請(qǐng)求接口地址
* fileName: 文件名,例如: ccc.png
* */
export function $fileDownload(url, fileName, config = {}) {
axios.request({
url: url,
method: "get",
data: config.data,
responseType: 'blob'
}).then(
response => {
console.log("fileName: ", fileName)
let fileDownload = require('js-file-download');
fileDownload(response.data, fileName);
if (typeof config.resolve === 'function') {
config.resolve();
}
},
error => {
let hasError = true;
if (error.response) {
const status = error.response.status;
if (status === 401) {
hasError = false;
}
}
if (hasError) {
this.$showError('下載出錯(cuò),請(qǐng)稍后再試');
}
if (typeof config.reject === 'function') {
config.reject();
}
}
);
}/*
* 引入import {$fileDownload} from "../../plugin/utils";
* 然后在methods中調(diào)用封裝的$fileDownload即可
*/
// 打開(kāi)附件
openFile (item) {
console.log(item);
/*item {
id: "D64C87AD4AF51CCFE0537C0AA8C0A129"
trainingManagementId: "D3E0484993A472DCE0537C0AA8C01C26"
fileName: "cls_jpg1.jpg"
filePath: "/data/apcos/6332/app-standard/app-standard/cdtraining/2022_01_24/c1ff8d5b-f254-4297-b4cb-20839a4316f2cls_jpg1.jpg"
createTime: "2022-01-24 11:26:10"
}*/
let url = `${this.url}/cdtraining/api/download?id=${item.id}`
$fileDownload(url, item.fileName)
},結(jié)果

方法二
methods: {
// 打開(kāi)附件
openFile(item) {
this.$axios.get('api', {
responseType: 'arraybuffer'
}).then(res => {
let dt = `data: image/jpeg;base64,${btoa(new Uint8Array(res.data).reduce((data, byte) => data + String.fromCharCode(byte), ''))}`;
console.log("dt: ", dt);
});
// console.log(item);
// let url = `${this.url}/upload/downloadFile?fileAddress=${item.other}&fileName=${item.fileAddress}`
// $fileDownload(url, item.fileAddress)
},
}結(jié)果

方法三
在downloadFile.js中
export async function downloadFile({ data, headers }) {
// todo 增加后端報(bào)錯(cuò)時(shí)的錯(cuò)誤捕獲
// 通過(guò)content-disposition獲取文件名稱(chēng)
const fileName = /.*filename=(.*)/i.exec(headers["content-disposition"])?.[1] || "下載";
// 開(kāi)始下載
const a = document.createElement("a");
a.href = (window.URL || window.webkitURL).createObjectURL(
new Blob([data], {
type: headers["content-type"]
})
);
a.download = decodeURIComponent((fileName));
a.dispatchEvent(new MouseEvent("click"));
}在接口文件api.js
// 導(dǎo)出
export function materialExport(data) {
return http.post(`/api/import/materialExport`, data, {
responseType: "arraybuffer" // 須指定返回類(lèi)型為 arraybuffer
});
}在html中引入downloadFile.js, api.js
<template>
<button @click="handelExport">導(dǎo)出下載</button>
</template>
<script>
import { downloadFile } from @/downloadFile
import { api } from @/api
methods: {
async handelExoprt() {
let obj = {}
}
if (this.tableData.length === 0) {
this.$message({
message: "無(wú)數(shù)據(jù), 不能下載",
type: "warning"
})
}else {
await downloadFile(await materialExport(obj));
}
}
</script>結(jié)果

到此這篇關(guān)于JavaScript下載后端返回的文件流的三種方法的文章就介紹到這了,更多相關(guān)JavaScript下載返回文件流內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
threejs全景圖和錨點(diǎn)編輯的實(shí)現(xiàn)方案
大家都知道可以利用Threejs中的立方體或者球體實(shí)現(xiàn)全景圖功能,下面這篇文章主要給大家介紹了關(guān)于threejs全景圖和錨點(diǎn)編輯的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-04-04
webpack的 rquire.context用法實(shí)現(xiàn)工程自動(dòng)化的方法
這篇文章主要介紹了webpack的 rquire.context用法實(shí)現(xiàn)工程自動(dòng)化的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02
解決webpack無(wú)法通過(guò)IP地址訪問(wèn)localhost的問(wèn)題
下面小編就為大家分享一篇解決webpack無(wú)法通過(guò)IP地址訪問(wèn)localhost的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-02-02
TypeScript 中如何限制對(duì)象鍵名的取值范圍
TypeScript由微軟開(kāi)發(fā)的自由和開(kāi)源的編程語(yǔ)言,是一種給 JavaScript 添加特性的語(yǔ)言擴(kuò)展,接下來(lái)通過(guò)本文給大家介紹TypeScript 中如何限制對(duì)象鍵名的取值范圍,感興趣的朋友跟隨小編一起看看吧2021-05-05
javascript入門(mén)之window對(duì)象【新手必看】
本文系統(tǒng)介紹了javascript的window對(duì)象以及一些控制函數(shù)的用法,僅供大家參考2016-11-11
JavaScript實(shí)現(xiàn)簡(jiǎn)單評(píng)論功能
這篇文章主要為大家詳細(xì)介紹了JavaScript實(shí)現(xiàn)簡(jiǎn)單評(píng)論功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08
JS實(shí)現(xiàn)程序暫停與繼續(xù)功能代碼解讀
程序暫停與繼續(xù)的實(shí)現(xiàn)方法有很多,在本文為大家介紹下js中是如果做到的,并對(duì)具體的功能代碼進(jìn)行注釋說(shuō)明,感興趣的朋友不要錯(cuò)過(guò)2013-10-10
js 去掉空格實(shí)例 Trim() LTrim() RTrim()
js 去掉空格實(shí)例Trim(),LTrim(),RTrim() 需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助2014-01-01

