From a1d7e81859f554f3a53680cc35f0f49bf1f77098 Mon Sep 17 00:00:00 2001
From: wwf <1971391498@qq.com>
Date: 星期四, 14 五月 2026 14:37:02 +0800
Subject: [PATCH] 导入项目
---
src/utils/download.ts | 100 ++++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 100 insertions(+), 0 deletions(-)
diff --git a/src/utils/download.ts b/src/utils/download.ts
new file mode 100644
index 0000000..32fc624
--- /dev/null
+++ b/src/utils/download.ts
@@ -0,0 +1,100 @@
+const download0 = (data: Blob, fileName: string, mineType: string) => {
+ // 鍒涘缓 blob
+ const blob = new Blob([data], { type: mineType })
+ // 鍒涘缓 href 瓒呴摼鎺ワ紝鐐瑰嚮杩涜涓嬭浇
+ window.URL = window.URL || window.webkitURL
+ const href = URL.createObjectURL(blob)
+ const downA = document.createElement('a')
+ downA.href = href
+ downA.download = fileName
+ downA.click()
+ // 閿�姣佽秴杩炴帴
+ window.URL.revokeObjectURL(href)
+}
+
+const download = {
+ // 涓嬭浇 Excel 鏂规硶
+ excel: (data: Blob, fileName: string) => {
+ download0(data, fileName, 'application/vnd.ms-excel')
+ },
+ // 涓嬭浇 Word 鏂规硶
+ word: (data: Blob, fileName: string) => {
+ download0(data, fileName, 'application/msword')
+ },
+ // 涓嬭浇 Zip 鏂规硶
+ zip: (data: Blob, fileName: string) => {
+ download0(data, fileName, 'application/zip')
+ },
+ // 涓嬭浇 Html 鏂规硶
+ html: (data: Blob, fileName: string) => {
+ download0(data, fileName, 'text/html')
+ },
+ // 涓嬭浇 Markdown 鏂规硶
+ markdown: (data: Blob, fileName: string) => {
+ download0(data, fileName, 'text/markdown')
+ },
+ // 涓嬭浇 Json 鏂规硶
+ json: (data: Blob, fileName: string) => {
+ download0(data, fileName, 'application/json')
+ },
+ // 涓嬭浇鍥剧墖锛堝厑璁歌法鍩燂級
+ image: ({
+ url,
+ canvasWidth,
+ canvasHeight,
+ drawWithImageSize = true
+ }: {
+ url: string
+ canvasWidth?: number // 鎸囧畾鐢诲竷瀹藉害
+ canvasHeight?: number // 鎸囧畾鐢诲竷楂樺害
+ drawWithImageSize?: boolean // 灏嗗浘鐗囩粯鍒跺湪鐢诲竷涓婃椂甯︿笂鍥剧墖鐨勫楂樺��, 榛樿鏄甯︿笂鐨�
+ }) => {
+ const image = new Image()
+ // image.setAttribute('crossOrigin', 'anonymous')
+ image.src = url
+ image.onload = () => {
+ const canvas = document.createElement('canvas')
+ canvas.width = canvasWidth || image.width
+ canvas.height = canvasHeight || image.height
+ const ctx = canvas.getContext('2d') as CanvasRenderingContext2D
+ ctx?.clearRect(0, 0, canvas.width, canvas.height)
+ if (drawWithImageSize) {
+ ctx.drawImage(image, 0, 0, image.width, image.height)
+ } else {
+ ctx.drawImage(image, 0, 0)
+ }
+ const url = canvas.toDataURL('image/png')
+ const a = document.createElement('a')
+ a.href = url
+ a.download = 'image.png'
+ a.click()
+ }
+ },
+ base64ToFile: (base64: any, fileName: string) => {
+ // 灏哹ase64鎸夌収 , 杩涜鍒嗗壊 灏嗗墠缂� 涓庡悗缁唴瀹瑰垎闅斿紑
+ const data = base64.split(',')
+ // 鍒╃敤姝e垯琛ㄨ揪寮� 浠庡墠缂�涓幏鍙栧浘鐗囩殑绫诲瀷淇℃伅锛坕mage/png銆乮mage/jpeg銆乮mage/webp绛夛級
+ const type = data[0].match(/:(.*?);/)[1]
+ // 浠庡浘鐗囩殑绫诲瀷淇℃伅涓� 鑾峰彇鍏蜂綋鐨勬枃浠舵牸寮忓悗缂�锛坧ng銆乯peg銆亀ebp锛�
+ const suffix = type.split('/')[1]
+ // 浣跨敤atob()瀵筨ase64鏁版嵁杩涜瑙g爜 缁撴灉鏄竴涓枃浠舵暟鎹祦 浠ュ瓧绗︿覆鐨勬牸寮忚緭鍑�
+ const bstr = window.atob(data[1])
+ // 鑾峰彇瑙g爜缁撴灉瀛楃涓茬殑闀垮害
+ let n = bstr.length
+ // 鏍规嵁瑙g爜缁撴灉瀛楃涓茬殑闀垮害鍒涘缓涓�涓瓑闀跨殑鏁村舰鏁板瓧鏁扮粍
+ // 浣嗗湪鍒涘缓鏃� 鎵�鏈夊厓绱犲垵濮嬪�奸兘涓� 0
+ const u8arr = new Uint8Array(n)
+ // 灏嗘暣褰㈡暟缁勭殑姣忎釜鍏冪礌濉厖涓鸿В鐮佺粨鏋滃瓧绗︿覆瀵瑰簲浣嶇疆瀛楃鐨刄TF-16 缂栫爜鍗曞厓
+ while (n--) {
+ // charCodeAt()锛氳幏鍙栫粰瀹氱储寮曞瀛楃瀵瑰簲鐨� UTF-16 浠g爜鍗曞厓
+ u8arr[n] = bstr.charCodeAt(n)
+ }
+
+ // 灏咶ile鏂囦欢瀵硅薄杩斿洖缁欐柟娉曠殑璋冪敤鑰�
+ return new File([u8arr], `${fileName}.${suffix}`, {
+ type: type
+ })
+ }
+}
+
+export default download
--
Gitblit v1.8.0