美文网首页
cordova-cordova-plugin-file

cordova-cordova-plugin-file

作者: 三亿 | 来源:发表于2023-03-06 15:40 被阅读0次

    /**

    • @param {String} basePath 文件更目录,如cordova.file.externalApplicationStorageDirectory、cordova.file.externalRootDirectory、cordova.file.externalDataDirectory、cordova.file.documentsDirectory
    • @param {String} folderName 文件夹名
    • @param {String} folderName 文件名
    • @description 创建文件,并返回文件的全路径,基于promise
      */

    export default async (basePath, folderName, fileName) => {
    const appPath = basePath + folderName,
    isExistDirectory = () => {
    // 判断文件夹存不存在,不存在这创建文件夹,返回文件夹路径,如果创建失败则终止操作
    return new Promise(resolve => {
    window.resolveLocalFileSystemURL(appPath, app_path => {
    resolve(app_path)
    }, () => {
    window.resolveLocalFileSystemURL(basePath, base_path => {
    base_path.getDirectory(folderName, {
    create: true
    }, app_path => {
    resolve(app_path)
    }, () => {
    resolve()
    })
    }, () => {
    resolve()
    })
    })
    })
    },
    app_path = await isExistDirectory(),
    createFile = () => {
    // 判断文件存不存在,不存在这创建文件,返回文件路径,存在直接返回路径
    return new Promise(resolve => {
    if (!app_path) {
    resolve()
    return
    }
    app_path.getFile(fileName, {
    create: true,
    exclusive: true
    }, file_path => {
    resolve(file_path.toURL())
    }, () => {
    resolve(app_path.toURL() + fileName)
    })
    })
    }
    return createFile()
    }

    相关文章

      网友评论

          本文标题:cordova-cordova-plugin-file

          本文链接:https://www.haomeiwen.com/subject/frfpldtx.html