fix(download): Optimize download logic and adapt to iframe (#739)

* fix(download): Optimize download logic and adapt to iframe
This commit is contained in:
wenmine 2024-08-16 10:30:47 +08:00 committed by GitHub
parent 1ed91983b0
commit 59eaf035bb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 10 additions and 10 deletions

View File

@ -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)
}
/**

View File

@ -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