尝试下载并打开PDF文件-react-Native

时间:2020-04-24 01:25:04

标签: react-native rn-fetch-blob

我正在尝试将文件从base64转换为pdf文件,这就是为什么我使用RNFetchBlob软件包的原因,但是不幸的是该文件已创建,但是当我试图打开它时,Adobe给我一个错误,并且当我试图读取时从另一个应用程序获取它没人能帮助我

 readPdf = async(item) => {
    console.log("download")
    const android = RNFetchBlob.android

    RNFetchBlob
       .config({
           fileCache: true,
           appendExt: 'pdf',
           addAndroidDownloads : {
               useDownloadManager : true, // <-- this is the only thing required
               // Optional, override notification setting (default to true)
               notification : true,

               // Title of download notification
                title : 'Great ! Download Success ! :O ',
                // Make the file scannable  by media scanner
                mediaScannable : 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.'
           },

       })

       try {
        const granted = await PermissionsAndroid.request(
            PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE
        );

        if (granted === PermissionsAndroid.RESULTS.GRANTED) {
          console.log("Permission granted");

         // the path of downloaded file
         // resp.path()
         let base64Str = item;
         const fs = RNFetchBlob.fs;
         const dirs = RNFetchBlob.fs.dirs;
         const NEW_FILE_PATH = dirs.DownloadDir + '/test.pdf';

         //let pdfLocation = RNFetchBlob.fs.dirs.DocumentDir + '/' + 'test.pdf';
         android.actionViewIntent(NEW_FILE_PATH, 'application/pdf')

         console.log("pdf location"+NEW_FILE_PATH)
         RNFetchBlob.fs.writeFile(NEW_FILE_PATH, RNFetchBlob.base64.encode(base64Str), 'base64');
        }
        else {
          console.log('Permission denied');
      }
      } catch (err) {
          console.warn(err);
      }

   }

0 个答案:

没有答案