我有来自节点 js 服务器的 base 64 图像,我在 Image 标记中使用这个 base64 和源代码data:image/jpg;base64,${mybase64}
,它的显示效果很好。现在,当我尝试使用 RNFetchblob 使用相同的 base64 URL 下载图像时,问题就出现了。文件已创建,但它是一个损坏的文件,与下载的图像不同。这是我到目前为止所尝试的。
List.js
const downloadFile = async (FileData, fileName) => {
try {
const granted = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE,
{
title: "Storage permission required",
message: "App needs access to your storage to download"
}
)
if (granted === PermissionsAndroid.RESULTS.GRANTED) {
console.log('Storage Permission Granted')
const fs = RNFetchBlob.fs;
const dirs = RNFetchBlob.fs.dirs;
console.log(dirs.DownloadDir);
const NEW_FILE_PATH = dirs.DownloadDir + `/${fileName}`;
let options = {
fileCache: true,
addAndroidDownloads: {
useDownloadManager: true,
notification: true,
path: NEW_FILE_PATH,
Description: 'Downloading image',
mime: "image/*"
}
}
RNFetchBlob.config(options);
RNFetchBlob.fs.createFile(NEW_FILE_PATH, FileData, 'base64').then(res => {
console.log(res);
});
} else {
alert('Storage Permission Not Granted')
}
} catch (error) {
console.log('error while uploading', error)
}
}