Как отправить документ из реагирующего нативного ответа DocumentPicker на веб-API в виде байта [] - PullRequest
0 голосов
/ 07 октября 2018

Я использую форму реагирования-выбора документов, выбирая файл на мобильном телефоне 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

Как получить массив байтов загруженного файла в оригинальном реагировании и отправить его в качестве входных данных для запроса на публикацию в веб-API.

1 Ответ

0 голосов
/ 26 октября 2018

Это работает для меня

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