Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Upload image using formdata in react-native

How can i upload file to server using react-native's fetch api Need to achieve something similar to this-

------WebKitFormBoundaryRAxza9ew2ADaYHn5
Content-Disposition: form-data; name="file"; filename="66154520.jpg"
Content-Type: image/jpeg


------WebKitFormBoundaryRAxza9ew2ADaYHn5--
like image 376
Rakesh Yadav Avatar asked Nov 23 '16 09:11

Rakesh Yadav


1 Answers

You didn't show any kind of code, but in theory...

You should pass url from something like ImagePicker or CameraRoll (which should look similar to file:///storage/emulated/0/Pictures/shj15791-4v61-67g6-z1b6-v8451z956j5k.jpg) to formData and then pass it along with the request:

const form = new FormData();

form.append('image', {
  uri: "file:///...",
  type: 'image/jpg',
  name: 'image.jpg',
});

fetch('https://example.com/api/upload', {
  method: 'POST',
  body: form
});
like image 68
biphobe Avatar answered Nov 15 '22 15:11

biphobe