From 59eaf035bbb6117c46747ac6a09aa1d18d55eac3 Mon Sep 17 00:00:00 2001 From: wenmine Date: Fri, 16 Aug 2024 10:30:47 +0800 Subject: [PATCH] fix(download): Optimize download logic and adapt to iframe (#739) * fix(download): Optimize download logic and adapt to iframe --- packages/utils/src/fs/fszip.js | 14 +++++++------- packages/utils/src/fs/index.js | 6 +++--- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/packages/utils/src/fs/fszip.js b/packages/utils/src/fs/fszip.js index 4efca735d..d97654081 100644 --- a/packages/utils/src/fs/fszip.js +++ b/packages/utils/src/fs/fszip.js @@ -19,13 +19,13 @@ import JSZIP from 'jszip' * @param {string} fileName 文件名 */ export function saveAs(blobData, fileName) { - const zipLink = document.createElement('a') - zipLink.download = fileName - zipLink.style.display = 'none' - zipLink.href = URL.createObjectURL(blobData) - document.body.appendChild(zipLink) - zipLink.click() - document.body.removeChild(zipLink) + const downloadLink = document.createElement('a') + downloadLink.download = fileName + downloadLink.style.display = 'none' + downloadLink.href = URL.createObjectURL(blobData) + document.body.appendChild(downloadLink) + downloadLink.click() + document.body.removeChild(downloadLink) } /** diff --git a/packages/utils/src/fs/index.js b/packages/utils/src/fs/index.js index 26b5656ea..ecf7ecc73 100644 --- a/packages/utils/src/fs/index.js +++ b/packages/utils/src/fs/index.js @@ -25,7 +25,7 @@ export const isSupportFileSystemAccess = * @returns dirHandle 目录句柄 */ export const getUserBaseDirHandle = async (options = {}) => { - if (!window.showOpenFilePicker) { + if (!isSupportFileSystemAccess) { return createZip() } const dirHandle = await window.showDirectoryPicker({ mode: 'readwrite', ...options }) @@ -81,8 +81,8 @@ export async function getFileHandle(baseDirHandle, filePath, { create = false } * @returns fileHandle 文件句柄 */ export const getUserFileHandle = async (options = {}) => { - if (!window.showOpenFilePicker) { - throw new Error('不支持的浏览器!') + if (!isSupportFileSystemAccess) { + throw new Error('不支持的浏览器或处于iframe中') } const [fileHandle] = await window.showOpenFilePicker({ mode: 'readwrite', ...options }) return fileHandle