11

I want to send an image as a file to the server in react native. How can I do this? Here is my code:-

export const editUserProfile = (sessionId,firstName,lastName,image,countryCode,phone) => 
new Promise((resolve,reject) => {

    const form = new FormData();
    form.append('image',{
        uri : image,
        type : 'image/jpeg',
        name : 'image.jpg'
    })
        return axios.post(base_url+'edit-profile',{
            session_id : sessionId, 
            firstname : firstName,  
            lastname : lastName,
            image : null,  
            country_code : countryCode, 
            phone : phone, 
            locale : 'en'
        }).then(response =>     
            {resolve(response)})
        .catch(error => 
            {reject(error)});
    });
1

3 Answers 3

16
//edit user profile
export const editUserProfile = 
    (sessionId,firstName,lastName,image,countryCode,phone) => 
new Promise((resolve,reject) => {

    var data = new FormData();
    data.append('session_id',sessionId);
    data.append('firstname',firstName);
    data.append('lastname',lastName);
    data.append('image',
      {
         uri:image,
         name:'userProfile.jpg',
         type:'image/jpg'
      });
    data.append('country_code',countryCode);
    data.append('phone',phone);
    data.append('locale','en');

        return axios.post(base_url+'edit-profile',data).then(response =>     
            {resolve(response)})
        .catch(error => 
            {reject(error)});
    });
Sign up to request clarification or add additional context in comments.

8 Comments

I am getting error Request failed with status code 422. What is the problem ?
Sorry that was not code side error, After searching long time I found that, that was server side error.
What is that name key in image as in react-native, if we are using an image picker that returns an object which has the image details like uri, type but no name.
And don't we need any special headers to send images or other files
name is the name of image to identify image like its user image so i set name userProfile.jpg....or u can also use name from imageObject that picked from image picker....
|
0

I was also stuck in this problem for a week. After research I found that we are sending more than 10MB file from RN to server which won't reach to server.You can resize image size and send to server. This package help me. https://github.com/bamlab/react-native-image-resizer

Comments

0

after wasting full day i just found the solution. first find the file path you have of your image. mine was this.

file:///storage/emulated/0/Pictures/f5ca988c-882d-4b5e-86bb-b47939909b09.jpg

after changing it from this to

file://storage/emulated/0/Pictures/f5ca988c-882d-4b5e-86bb-b47939909b09.jpg it worked :). just remove the third back slash.

1 Comment

It is not working for me and getting error while posting "No such file or directory"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.