1、核心代码
export async function downloadFileBase64(fileEntry, base64) {var arr = base64.split(',');var mime = arr[0].match(/:(.*?);/)[1];var bstr = atob(arr[1]);var n = bstr.length;var u8arr = new Uint8Array(n);while (n--) {u8arr[n] = bstr.charCodeAt(n);}const blob = new Blob([u8arr], { type: mime });console.log('Blob', blob)return new Promise((resolve, reject) => {fileEntry.createWriter(fileWriter => {fileWriter.onwriteend = function () {console.log(`文件写入成功`);resolve(true);};fileWriter.onerror = function (e) {console.error(`文件写入失败`);reject(false);};fileWriter.write(blob);});});
}
2、fileEntry的获取
async function updateSign(item) {const user = JSON.parse(localStorage.getItem('user'))const ROOT_DIR_PATH = isCordova() ? cordova.file.externalDataDirectory : '';const rootDirEntry = await resolveLocalFileSystemURL(ROOT_DIR_PATH);const userDirEntry = await getDirectory(rootDirEntry, user.displayName);const parentDirEntry = await getDirectory(userDirEntry, '签名');const fileEntry = await getFile(parentDirEntry, item.name);const state = await downloadFileBase64(fileEntry, item.base64);return state
}
3、使用到的技术
React
ES6
Cordova