question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Sending a image to backend server

See original GitHub issue

Hey there. I am trying to send a image which i get from the response and send it via fetch to my server.

 UIImagePickerManager.showImagePicker(options, (didCancel, response) => {
        console.log('Response = ', response);
        if (didCancel) {
            console.log('User cancelled image picker');
        }
        else {
            var fileName = response.uri.substring(response.uri.lastIndexOf("/") + 1);
            var fsPath = response.uri;
            var binaryResponse = response.data;
            fetch('http://XXXX/user/avatar', {
                method: 'POST', // default 'POST',support 'POST' and 'PUT'
                headers: {
                    'Authorization': AuthStore.getUserTokenWithBearer()
                },
                body: JSON.stringify({image: fsPath})
            }).then(function (response) {
                console.log(response)
            }).catch(function(error){
                console.log(error);
            });
        }

I know this isn´t a modules issue, but trying to find solution from stackoverflow or any other sources failed. Maybe someone has tried this with this component and success?

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:22 (5 by maintainers)

github_iconTop GitHub Comments

11reactions
haitaolicommented, Apr 27, 2017

@ShanthiSenguttuvan grab the url from image picker, then try this:

const xhr = new XMLHttpRequest();

xhr.onload = () => {
  if (xhr.status < 400) {
    // succeeded
  } else {
    const error = new Error(xhr.response);
  }
};

xhr.onerror = (error) => {
};

xhr.open('PUT', uploadURL);
xhr.setRequestHeader('content-type', contentType);
xhr.send({ uri: imageURI });

Update: this is client code for uploading image to your server. If your question is about how to get image content on server side, then you can refer to nodejs documentation.

11reactions
yfukscommented, Dec 14, 2015

Hello ! I don’t really understand what is your problem but i can give you an example, in the folowing example i post an image to cloudinary using fetch, fileUpload is still a good solution !

var _ = require('underscore');
let { UIImagePickerManager: ImagePickerManager } = NativeModules;
var sha1 = require('sha1');

 UIImagePickerManager.showImagePicker(options, (didCancel, response) => {
       console.log('Response = ', response);
       if (didCancel) {
           return console.log('User cancelled image picker');
       }
       var url = 'https://api.cloudinary.com/v1_1/' + cloud_name + '/image/upload';
       var header = {
            method: 'post',
            headers: {
              'Accept': 'application/json',
              'Content-Type': 'application/json'
           }
       };
      var timestamp = Date.now();

      var values = {
       file: 'data:image/png;base64,' + response.data,
       api_key: api_key,
       timestamp: timestamp,
       tags: tags,
       signature: sha1("tags=" + tags + "&timestamp=" + timestamp + api_secret)
     };

     var request = _.extend({
        body: JSON.stringify(values)
     }, header);

     fetch(url, request)
         .then((response) => response.json())
         .then((data) => {
      console.log(data);
    })
    .catch((err) => { console.log(err) });}
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

The Better Way of Sending Data to the Backend
At first, you have to create an instance of the formdata. var formData = new FormData(); and then you have to append the...
Read more >
Send an image to your backend with the fetch() function
Send an image to your backend with the fetch() function — JavaScript, React ; File or ; Blob objects to specify the file...
Read more >
how to upload image from frontend to backend - Stack Overflow
On click that button,a modal is opening. There is two different field called Image Title, Input File. On submit the Save button. It...
Read more >
Sending image files from back-end to front-end - JavaScript
I'm having difficulty figuring out how to send an image file from my express back-end to my front-end react application. It's a chat...
Read more >
how to send images quickly from a backend to a frontend
You can use FormData () constructor to send binary data directly to the backend. At first, you have to create an instance of...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found