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

Tam*_*ago 7 javascript base64 reactjs react-native

我想将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;
            }
          }
    } ```


Run Code Online (Sandbox Code Playgroud)

小智 6

你可以使用这个库

react-native-image-base64rn-fetch-blob

import RNFetchBlob from 'rn-fetch-blob';

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