如何将文档从本机 DocumentPicker 响应发送到 Web api 作为 byte[]

How to Send the document from react native DocumentPicker response to a web api as byte[]

我正在使用 react-native-document-picker 表单,使用以下代码从 Android 移动设备中选择文件

DocumentPicker.show({
  filetype: [DocumentPickerUtil.images()],
},(error,res) => {
  // Android
  console.log(
     res.uri,
     res.type, // mime type
     res.fileName,
     res.fileSize
  );
});

以上日志的输出类似于

uri: 'content://com.android.providers.media.documents/document/image%3A48',
type: 'image/jpeg'
fileName: 'TestImage.jpeg'
fileSize: 5301

如何在 React Native 中获取上传文件的字节数组并将其作为输入发送到 Web api post 请求。

这对我有用

    DocumentPicker.show({
      filetype: [DocumentPickerUtil.images()],
    },(error,res) => {
 
  
        const data = new FormData();
        data.append('name', 'testName'); // you can append anyone.
        data.append('photo', {
          uri: res.uri,
          type: res.type,
          name: res.fileName,
        });

        fetch('http://ApiUrl/api/Controller/UploadFile', {
          method: 'post',
          headers: {
            'Content-Type': 'multipart/form-data',
          },
          body: data
        }).then(res => {
          console.log(res)
        })
        .catch(function(error) {
          console.log('There has been a problem with your fetch operation: ' + error.message);
            throw error;
          });
});