如何在react-native-document-picker中将PDF转换为base64?

问题描述 投票:0回答:2

我想将react-native-document-picker的响应转换为base64格式。作为响应,我得到了文件的 Uri,但不幸的是没有 base64。

这是我的代码:

const openDocumentFile = async () =>{
        try {
            const results = await DocumentPicker.pickMultiple({
              type: [DocumentPicker.types.pdf],
              readContent: true
            });
            for (const res of [results]) {
           
              console.log(res)
          
            };
                
          } catch (err) {
            if (DocumentPicker.isCancel(err)) {
            } else {
              throw err;
            }
          }
    } ```


javascript reactjs react-native base64
2个回答
5
投票

你可以使用这个库

反应本机图像-base64 或者 rn-fetch-blob

import RNFetchBlob from 'rn-fetch-blob';

 RNFetchBlob.fs
  .readFile(filePath, 'base64')
  .then((data) => {
    setdata(data);
  })
  .catch((err) => {});

0
投票

您还可以使用 react-native-fs 库。最好解码文件路径/uri。

import RNFS from 'react-native-fs';
...
const base64 = await RNFS.readFile(decodeURIComponent(filePath), 'base64');
...
© www.soinside.com 2019 - 2024. All rights reserved.