RN从IOS中的URL获取blob下载docx或pdf

pu82cl6c  于 2023-10-21  发布在  iOS
关注(0)|答案(5)|浏览(105)

做了什么

在Android中实现,并在Android中的特定DCIM directory中下载。在ios中使用DocumentDir下载pdf或docx文件。使用"rn-fetch-blob": "^0.12.0";

错误

在IOS中,它在控制台中显示消息:
文件保存到/var/mobile/Containers/Data/Application/E6 FDC 2DD-7 FCA-44 DC-85 C4-A275078 F8825/Documents/wow13.pdf
下载的代码是这样的:

downloadFileOnSuccess = async () => {
    let dirs =
      Platform.OS == 'ios'
        ? RNFetchBlob.fs.dirs.DocumentDir
        : RNFetchBlob.fs.dirs.DCIMDir;
    console.log(dirs, 'document path');
    RNFetchBlob.config({
      // response data will be saved to this path if it has access right.

      fileCache: true,
      path: dirs + `/wow13.pdf`,
    })
      .fetch(
        'GET',
        'https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf',
        {
          //some headers ..
        },
      )
      .then(res => {
        // the path should be dirs.DocumentDir + 'path-to-file.anything'
        console.log('The file saved to ', res.path());
      });
  };

但是我不能得到文件在我的iPhone 7真实的设备中下载的位置。我在IOS中缺少任何权限吗?没有添加任何IOS权限。

8wigbo56

8wigbo561#

感谢Pruthvi,我得到了答案,对于iOS我需要提示用户:

.then(resp => {
      if (Platform.OS === "ios") {
        RNFetchBlob.ios.openDocument(resp.data);
      }
    })

希望对你们有帮助

huus2vyu

huus2vyu2#

最新工作解决方案:

const actualDownload = () => {
        const { dirs } = RNFetchBlob.fs;
        const dirToSave = Platform.OS == 'ios' ? dirs.DocumentDir : dirs.DownloadDir
        const configfb = {
            fileCache: true,
            useDownloadManager: true,
            notification: true,
            mediaScannable: true,
            title: pdfInfo.pdf,
            path: `${dirToSave}/${pdfInfo.pdf}`,
        }
        const configOptions = Platform.select({
            ios: {
                fileCache: configfb.fileCache,
                title: configfb.title,
                path: configfb.path,
                appendExt: 'pdf',
            },
            android: configfb,
        });
    
        console.log('The file saved to 23233', configfb, dirs);
    
        RNFetchBlob.config(configOptions)
            .fetch('GET', `https://aquatherm.s3.ap-south-1.amazonaws.com/pdfs/${pdfInfo.pdf}`, {})
            .then((res) => {
                if (Platform.OS === "ios") {
                    RNFetchBlob.fs.writeFile(configfb.path, res.data, 'base64');
                    RNFetchBlob.ios.previewDocument(configfb.path);
                }
                setisdownloaded(false)
                if (Platform.OS == 'android') {
                    showSnackbar('File downloaded');
                }
                console.log('The file saved to ', res);
            })
            .catch((e) => {
                setisdownloaded(true)
                showSnackbar(e.message);
                console.log('The file saved to ERROR', e.message)
            });
    }
wsxa1bj1

wsxa1bj13#

改进Ajmall Hasan's answer
我必须将${pdfInfo.pdf}更改为${'pdfInfo.pdf'}才能使其工作

const actualDownload = () => {
        const { dirs } = RNFetchBlob.fs;
        const dirToSave = Platform.OS == 'ios' ? dirs.DocumentDir : dirs.DownloadDir
        const configfb = {
            fileCache: true,
            useDownloadManager: true,
            notification: true,
            mediaScannable: true,
            title: pdfInfo.pdf,
            path: `${dirToSave}/${'pdfInfo.pdf'}`,
        }
        const configOptions = Platform.select({
            ios: {
                fileCache: configfb.fileCache,
                title: configfb.title,
                path: configfb.path,
                appendExt: 'pdf',
            },
            android: configfb,
        });
    
        console.log('The file saved to ', configfb, dirs);
    
        RNFetchBlob.config(configOptions)
            .fetch('GET', `https://aquatherm.s3.ap-south-1.amazonaws.com/pdfs/${'pdfInfo.pdf'}`, {})
            .then((res) => {
                if (Platform.OS === "ios") {
                    RNFetchBlob.fs.writeFile(configfb.path, res.data, 'base64');
                    RNFetchBlob.ios.previewDocument(configfb.path);
                }
                if (Platform.OS == 'android') {
                    showSnackbar('File downloaded');
                }
                console.log('The file saved to ', res);
            })
            .catch((e) => {
                showSnackbar(e.message);
                console.log('The file saved to ERROR', e.message)
            });
    }
bgibtngc

bgibtngc4#

为我工作使用react native分享

const downloadFile = (fileName) => {
    const source = "sample.pdf";
    const { dirs } = RNFetchBlob.fs;
    RNFetchBlob.config({
      fileCache: true,
      appendExt: 'pdf',
      path: `${dirs.DocumentDir}/${fileName}`,
      addAndroidDownloads: {
        useDownloadManager: true,
        notification: true,
        title: fileName,
        description: 'File downloaded by download manager.',
        mime: 'application/pdf',
      },
    })
      .fetch('GET', source)
      .then((res) => {
        // in iOS, we want to save our files by opening up the saveToFiles bottom sheet action.
        // whereas in Android, the download manager is handling the download for us.
        if (Platform.OS === 'ios') {
          const filePath = res.path();
          let options = {
            type: 'application/pdf',
            url: filePath,
            saveToFiles: true,
          };
          Share.open(options)
            .then((resp) => console.log(resp))
            .catch((err) => console.log(err));
        }
      })
      .catch((err) => console.log('BLOB ERROR -> ', err));
  };
scyqe7ek

scyqe7ek5#

GetItem_downloadbtn = (item, itemname) => {
var pdfInfo = itemname;
const { dirs } = RNFetchBlob.fs;
const dirToSave = Platform.OS == 'ios' ? dirs.DocumentDir : dirs.DownloadDir
const configfb = {
    fileCache: true,
    useDownloadManager: true,
    notification: true,
    mediaScannable: true,
    title: pdfInfo.pdf,
    path: `${dirToSave}/${itemname}`,
}
const configOptions = Platform.select({
    ios: {
        fileCache: configfb.fileCache,
        title: configfb.title,
        path: configfb.path,
        appendExt: 'pdf',
    },
    android: configfb,
});
console.log('The file saved to 23233', configfb, dirs);

RNFetchBlob.config(configOptions)
    .fetch('GET', item, {})
    .then((res) => {
        if (Platform.OS === "ios") {
            RNFetchBlob.fs.writeFile(configfb.path, res.data, 'base64');
            RNFetchBlob.ios.previewDocument(configfb.path);
        }
        setisdownloaded(false)
        if (Platform.OS == 'android') {
            showSnackbar('File downloaded');
        }
        console.log('The file saved to ', res);
    })
    .catch((e) => {
        setisdownloaded(true)
        showSnackbar(e.message);
        console.log('The file saved to ERROR', e.message)
    });

}

相关问题