javascript - 如何从 basse64 字符串下载 PDF
问题描述
我正在使用 api 来获取 base64 字符串,现在我想将其转换为普通的 pdf 并希望将其保存在下载中。我怎样才能做到这一点。
我尝试像下面给出的代码一样使用 rn-fetch-blob。
const { config, fs } = RNFetchBlob
let DownloadDir = fs.dirs.DownloadDir;
let options = {
addAndroidDownloads: {
useDownloadManager: true, // <-- this is the only thing required
// Optional, override notification setting (default to true)
notification: true,
// Optional, but recommended since android DownloadManager will fail when
// the url does not contains a file extension, by default the mime type will be text/plain
mime: 'application/pdf',
description: 'File downloaded by download manager.',
path: DownloadDir + '/' + 'test.pdf',
}
}
trackPromise(
config(options).fetch('get', Constant.url.BASE_URL + 'my url/which returns/base64 string', {
Authorization: 'Bearer' + token.access_token,
})
.then((res) => {
console.log('download resp', res)
console.log('The file saved to ', res.path());
let base64Str = res.data;
let pdfLocation = RNFetchBlob.fs.dirs.DocumentDir + '/' + 'test.pdf';
RNFetchBlob.fs.writeFile(pdfLocation, RNFetchBlob.base64.encode(base64Str), 'base64');
Common.showToast('File saved in Download folder', 'success');
})
.catch((err) => {
console.log(err);
showToast('Failed to download file', 'danger');
//throwException(err);
})
);
请帮助我找到解决方案。提前致谢。
解决方案
推荐阅读
- php - IPS4 php获取当前成员的ID
- javascript - '{}' 类型的参数不能分配给 Angular 8 中的类型参数
- python-3.x - 如何计算两行特定列之间的最大值
- python - 像素着色器中的 Mandelbrot
- docker - Docker-in-Docker 连接到内部容器网络(锚引擎)的问题
- reactjs - 从猫鼬模式文件中提取模式
- domain-driven-design - 当投注严重依赖账户余额时,如何为投注/会计 BoundedContexts 建模?
- java - Java中的静态字段初始化
- c# - 通用列表
怎么投? - amazon-web-services - --instanceType 选项是否决定挂载到 EC2 的 EBS 卷大小?