Sending binary through HTTP using the React-Native Fetch API

Is there a way to use the Fetch API to load a binary file (e.g. in S3 using a signed URL)?

This would be a simple PUT for some "application / octet-stream".

The XHR library works, but I think Fetch is better, especially in a React-Native environment.
Does React-Native Fetch Blobcurrently support ?

Ideally, I would like to do something similar, but Blobnot defined:

fetch('https://s3.amazonaws.com/signedUrl/', {
  method: 'PUT',
  headers: {
    'Content-Type': 'application/octet-stream',
  },
  body: Blob(filePath)
})
+5
source share
1 answer

: https://github.com/github/fetch polyfill Fetch, .

:

var input = document.querySelector('input[type="file"]')

var data = new FormData()
data.append('file', input.files[0])
data.append('user', 'hubot')

fetch('/avatars', {
  method: 'POST',
  body: data
})

:

var input = document.querySelector('input[type="file"]')

function upload(e) {
  const file = input.files[0];
  const reader = new FileReader();

  reader.onload = (e) => { 
    fetch('/avatars', {
      method: 'POST',
      body: e.currentTarget.result
    })
  };

  reader.readAsArrayBuffer(file);
}

input.addEventListener('change', upload, false)
-3

Source: https://habr.com/ru/post/1663572/


All Articles