Переглянути джерело

chore: 合并同类工具方法

喵你个旺呀 1 рік тому
батько
коміт
83f038c9dc

+ 1 - 1
Admin.NET/Admin.NET.Web.Entry/wwwroot/template/index.vue.vm

@@ -2,7 +2,7 @@
 import { ref, reactive, onMounted } from "vue";
 import { auth } from '/@@/utils/authFunction';
 import { ElMessageBox, ElMessage } from "element-plus";
-import { downloadStreamFile } from "/@@/utils/downloadFile";
+import { downloadStreamFile } from "/@@/utils/download";
 @if(Model.PrintType == "custom") {
 @:// 推荐设置操作 width 为 200
 @:import { hiprint } from 'vue-plugin-hiprint';

+ 15 - 2
Web/src/utils/download.ts

@@ -1,5 +1,6 @@
 import { AxiosResponseHeaders, RawAxiosResponseHeaders } from 'axios';
 import { dataURLtoBlob, urlToBase64 } from './base64Conver';
+import * as url from "node:url";
 
 /**
  * Download online pictures
@@ -55,7 +56,7 @@ export function downloadByData(data: BlobPart, filename: string, mime?: string,
  * Download file according to file address
  * @param {*} sUrl
  */
-export function downloadByUrl({ url, target = '_blank', fileName }: { url: string; target?: TargetContext; fileName?: string }): boolean {
+export function downloadByUrl({ url, target = '_blank', fileName }: { url: string; target?: string; fileName?: string }): boolean {
 	const isChrome = window.navigator.userAgent.toLowerCase().indexOf('chrome') > -1;
 	const isSafari = window.navigator.userAgent.toLowerCase().indexOf('safari') > -1;
 
@@ -87,7 +88,7 @@ export function downloadByUrl({ url, target = '_blank', fileName }: { url: strin
 	return true;
 }
 
-export function openWindow(url: string, opt?: { target?: TargetContext | string; noopener?: boolean; noreferrer?: boolean }) {
+export function openWindow(url: string, opt?: { target?: string; noopener?: boolean; noreferrer?: boolean }) {
 	const { target = '__blank', noopener = true, noreferrer = true } = opt || {};
 	const feature: string[] = [];
 
@@ -106,3 +107,15 @@ export function getFileName(headers: RawAxiosResponseHeaders | AxiosResponseHead
 	}
 	return fileName;
 }
+
+/**
+ * 文件流下载
+ * @param res
+ * @param fileName 文件名
+ */
+export function downloadStreamFile(res: any, fileName: string | undefined) {
+	const contentType = res.headers['content-type'];
+	fileName = fileName || getFileName(res.headers['content-disposition']);
+	const blob = res.data instanceof Blob ? res.data : new Blob([res.data], { type: contentType });
+	downloadByUrl({ url: window.URL.createObjectURL(blob), fileName });
+}

+ 0 - 31
Web/src/utils/downloadFile.ts

@@ -1,31 +0,0 @@
-/**
- * 下载文件
- * @param url 下载链接
- * @param filename 文件名
- */
-export function downloadFile(url: string, filename: string | undefined = undefined) {
-    const urlSplit = url.split('/');
-    filename = filename || urlSplit[urlSplit.length - 1];
-
-    const link = document.createElement('a');
-    link.setAttribute('download', filename);
-    link.href = url;
-
-    document.body.appendChild(link);
-    link.click();
-
-    document.body.removeChild(link);
-    window.URL.revokeObjectURL(url);
-}
-
-/**
- * 文件流下载
- * @param res
- */
-export function downloadStreamFile(res: any) {
-    const contentType = res.headers['content-type'];
-    const contentDisposition = res.headers['content-disposition'];
-    const filename = decodeURIComponent(contentDisposition.split('; ')[1].split('=')[1])
-    const blob = res.data instanceof Blob ? res.data : new Blob([res.data], { type: contentType });
-    downloadFile(window.URL.createObjectURL(blob), filename);
-}