小编Sgr*_*lic的帖子

需要能够在 componentdidmount 时下载给定 BASE64 字符串的 PDF

我正在尝试提供在给定 Base64 字符串的情况下下载 PDF 的能力。我可以使用“react-native-view-pdf”查看PDF。只是无法弄清楚如何实际下载文件。这将需要适用于 android 和 ios。

我已经尝试了各种论坛,但遗憾的是却无处可去。

注意:this.props.pdf 是 Base64 字符串。

尝试 1)

var path = RNFetchBlob.fs.dirs.DocumentDir + "/bill.pdf";
RNFetchBlob.fs.writeFile(path, this.props.pdf, "base64").then(res => {
  console.log("File : ", res);
});
Run Code Online (Sandbox Code Playgroud)

尝试 2)

RNFetchBlob.config({
  fileCache : true,
  appendExt : 'pdf'
})
.fetch('GET',`${this.props.PDFLink}`)
.then((res) => {
  // open the document directly
  if(Platform.OS == "ios"){
  RNFetchBlob.ios.previewDocument(res.path())
  }
  else{
    RNFetchBlob
    .config({
        addAndroidDownloads : {
            useDownloadManager : true, // <-- this is the only thing required
            // Optional, override notification setting (default to true) …
Run Code Online (Sandbox Code Playgroud)

react-native react-native-android react-native-ios react-native-fetch-blob

3
推荐指数
1
解决办法
4329
查看次数