Commit 4beffbb2 authored by 以墨为白's avatar 以墨为白 🎧

前端直连下载文件

parent 07f0cf39
......@@ -819,6 +819,46 @@ let uploadOSSProgress = (filename, bucket, file, progressFunction) => {
});
});
};
/**
* 直连oss下载,数据较大时需要配合进度条使用
* @param {*} bucket
* @param {*} filename
* @param {*} originFileName
* @returns
*/
let downloadOSS = (bucket, filename, originFileName) => {
return new Promise((resolve, reject) => {
fetch(`api/rest/file/getDownloadUrl?bucket=${bucket}&filename=${filename}`, {
headers: new Headers(addHeaders({}))
}).then(res => {
if (res.status == 200) {
return res.json();
} else {
console.log(res.statusText);
reject(HTTP_STATUS);
}
}).then(res => {
if (res.code == 9000) {
return fetch(res.data, {
method: "GET",
})
} else {
return Promise.reject({ err: res.message });
}
}).then(res => res.blob().then(blob => {
filename = originFileName|| filename;
let a = document.createElement('a');
let url = window.URL.createObjectURL(blob);
a.href = url;
a.download = filename;
a.click();
window.URL.revokeObjectURL(url);
resolve(res.data);
})).catch(err => {
reject(err);
});
});
}
//#endregion
//#region sse支持
......@@ -967,5 +1007,7 @@ export {
httpSSERequestPoly,
uploadOSS,
uploadOSSProgress,
fetchEventSource
fetchEventSource,
downloadOSS
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment