Skip to content

Instantly share code, notes, and snippets.

@mahbubur001
Forked from jakehasler/upload.js
Created December 10, 2020 17:18
Show Gist options
  • Save mahbubur001/381e18386279620b6abd06ff7eefe62f to your computer and use it in GitHub Desktop.
Save mahbubur001/381e18386279620b6abd06ff7eefe62f to your computer and use it in GitHub Desktop.

Revisions

  1. @jakehasler jakehasler renamed this gist Oct 31, 2016. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions upload.jsx → upload.js
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,4 @@
    // Function form the react-native-image-picker library
    ImagePicker.showImagePicker({ title: 'Select Image' }, (response) => {
    // format the image data
    const image = {
  2. @jakehasler jakehasler created this gist Oct 31, 2016.
    30 changes: 30 additions & 0 deletions upload.jsx
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,30 @@
    ImagePicker.showImagePicker({ title: 'Select Image' }, (response) => {
    // format the image data
    const image = {
    uri: response.uri,
    type: 'image/jpeg',
    name: 'myImage' + '-' + Date.now() + '.jpg'
    }
    // Instantiate a FormData() object
    const imgBody = new FormData();
    // append the image to the object with the title 'image'
    body.append('image', image);
    const url = `http://your-api.com/image-upload`;
    // Perform the request. Note the content type - very important
    fetch(url, {
    method: 'POST',
    headers: {
    'Accept': 'application/json',
    'Content-Type': 'multipart/form-data',
    },
    body: imgBody
    }).then(res => res.json()).then(results => {
    // Just me assigning the image url to be seen in the view
    const source = { uri: res.imageUrl, isStatic: true };
    const images = this.state.images;
    images[index] = source;
    this.setState({ images });
    }).catch(error => {
    console.error(error);
    });
    });