|
@@ -1,3 +1,5 @@
|
|
|
|
+import { objTypeOf, blobToText } from "./utils";
|
|
|
|
+
|
|
function parseDownloadFilename(dispositionInfo) {
|
|
function parseDownloadFilename(dispositionInfo) {
|
|
const strs = dispositionInfo.split(";");
|
|
const strs = dispositionInfo.split(";");
|
|
let filename = "";
|
|
let filename = "";
|
|
@@ -17,18 +19,24 @@ function parseDownloadFilename(dispositionInfo) {
|
|
* @param {String}} fileName 文件名
|
|
* @param {String}} fileName 文件名
|
|
* @returns Promise<Boolean>
|
|
* @returns Promise<Boolean>
|
|
*/
|
|
*/
|
|
-export function downloadByApi(fetchFunc, fileName) {
|
|
|
|
- return fetchFunc()
|
|
|
|
- .then(res => {
|
|
|
|
- const filename =
|
|
|
|
- fileName || parseDownloadFilename(res.headers["content-disposition"]);
|
|
|
|
- console.log(filename);
|
|
|
|
- downloadByBlob(new Blob([res.data]), filename);
|
|
|
|
- return true;
|
|
|
|
- })
|
|
|
|
- .catch(e => {
|
|
|
|
- return Promise.reject(e);
|
|
|
|
- });
|
|
|
|
|
|
+export async function downloadByApi(fetchFunc, fileName) {
|
|
|
|
+ let errorInfo = null;
|
|
|
|
+ const res = await fetchFunc().catch(e => {
|
|
|
|
+ errorInfo = e;
|
|
|
|
+ });
|
|
|
|
+
|
|
|
|
+ // 展示后台错误信息
|
|
|
|
+ if (errorInfo && objTypeOf(errorInfo) === "blob") {
|
|
|
|
+ const res = await blobToText(errorInfo).catch(() => {});
|
|
|
|
+ if (!res) return Promise.reject("下载失败!");
|
|
|
|
+ const resJson = JSON.parse(res);
|
|
|
|
+ return Promise.reject(resJson.message);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ const filename =
|
|
|
|
+ fileName || parseDownloadFilename(res.headers["content-disposition"]);
|
|
|
|
+ downloadByBlob(new Blob([res.data]), filename);
|
|
|
|
+ return true;
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|