1

我正在使用 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

我如何在本机反应中获取上传文件的字节数组并将其作为输入发送到 web api 发布请求。

4

1 回答 1

3

这对我有用

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

于 2018-10-26T13:02:22.740 回答