Skip to content

Instantly share code, notes, and snippets.

@EQuimper
Created October 3, 2016 15:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EQuimper/464e09e9101b6977e9c4e25470d6679d to your computer and use it in GitHub Desktop.
Save EQuimper/464e09e9101b6977e9c4e25470d6679d to your computer and use it in GitHub Desktop.
Upload React-Native photo to server
const photo = {
uri: image,
type: 'image/jpeg',
name: 'avatar.jpg',
};
/* globals FormData */
const data = new FormData();
data.append('avatar', photo);
const options = {
body: data,
headers: {
...api.headers,
'Content-Type': 'multipart/form-data'
},
method: 'POST',
};
const res = await fetch('/upload', options)
@sunnynegi
Copy link

Need help over how to upload image in react native here is my code
onFormSubmit = (e) => {

var photo = {
   uri: "file:///storage/emulated/0/Pictures/1511787860629.jpg",
   type: 'image/jpeg',
   name: 'photo.jpg',
   size: this.state.uri[0].size,
 };
 var form = new FormData();
 form.append("imageLink", photo);

 const config = {
  method: 'POST',
  headers: {
    'Accept': 'application/json',
    'Content-Type': 'multipart/form-data',
  },
  body: form,
 }

 fetch("http://localhost:port/api/User/imageUpload", config)
  .then((responseData) => {
      // Log the response form the server
      // Here we get what we sent to Postman back
      console.log(responseData);
  })
  .catch(err => {
    console.log(err);
  })
}

Issue i am facing is {"statusCode":400,"error":"Bad Request","message":"Invalid content-type header: multipart missing boundary"}

@koswarabilly
Copy link

@sunnynegi maybe because of the url your are sending too, localhost:port is an invalid request because you do not let them to use the default port but you do not define any port yourself

@sunnynegi
Copy link

@koswarabilly Well thanks for suggestion, Problem is solved i am using fecth blob for uploading images.

@osinakayah
Copy link

@sunnynegi Can u please post your code sample for uploading?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment