|
@@ -41,14 +41,31 @@ export function object2QueryString(obj) {
|
|
|
return queryString.stringify(obj);
|
|
|
}
|
|
|
|
|
|
+function toDataURL(url) {
|
|
|
+ return fetch(url)
|
|
|
+ .then((response) => {
|
|
|
+ return response.blob();
|
|
|
+ })
|
|
|
+ .then((blob) => {
|
|
|
+ return URL.createObjectURL(blob);
|
|
|
+ });
|
|
|
+}
|
|
|
+
|
|
|
// 下载文件
|
|
|
-export function downloadFileURL(url, name) {
|
|
|
+export async function downloadFileURL(url, name) {
|
|
|
const link = document.createElement("a");
|
|
|
link.style.display = "none";
|
|
|
- // txt 文件会直接在浏览器里面打开,这时候只能修改服务器设置,加上 Content-Disposition: attachment
|
|
|
- link.href = url;
|
|
|
const fileName = name || url.split("/").pop();
|
|
|
link.setAttribute("download", fileName);
|
|
|
+
|
|
|
+ // txt 文件会直接在浏览器里面打开,这时候只能修改服务器设置,加上 Content-Disposition: attachment
|
|
|
+ if ([".txt"].some((v) => fileName.endsWith(v))) {
|
|
|
+ // const urlObj = new URL(url);
|
|
|
+ // link.href = await toDataURL(url.replace(urlObj.origin));
|
|
|
+ link.href = await toDataURL(url);
|
|
|
+ } else {
|
|
|
+ link.href = url;
|
|
|
+ }
|
|
|
document.body.appendChild(link);
|
|
|
link.click();
|
|
|
document.body.removeChild(link);
|