如何通过将本机DocumentPicker响应以字节[]的形式发送到Web api来发送文档

时间:2018-10-07 15:30:41

标签: react-native react-native-android document react-native-web

我正在使用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中获取上载文件的Byte数组并将其作为输入发送到Web api发布请求。

1 个答案:

答案 0 :(得分:0)

这对我有用

    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;
          });
});