1

I am trying to upload Image with Axios but getting: Request failed with status code 500.I don't have backend problem because I can upload image with postman and everything is fine. This is my addDocument() in FileUpload.js.

 addDocument(){
   let { title, description, imgUri } = this.state;
   console.log(this.state.imgUri);
   const body = new FormData();
   body.append('image', {
        uri: imgUri,
        type: 'image',
        name : `${new Date().getTime()}.jpg`,
    });

    addDocument(title, description, body).then((response) => {
        if (response.isSuccess == true) {
            this.setState({ loading: false });
            this.props.navigation.navigate('FileList',{isUpdate:'true'});
        } 
    });
};

This is my addDocument() in document.service.js.

export const addDocument = async (title, description, imageFile) => {
const trekkerId = await AsyncStorage.getItem("trekker_id");

const model = {
    profileDocumentId: '',
    title: title,
    description: description
}

console.log(model);
console.log(imageFile);

if (trekkerId) {
    return axios({
        method: 'post',
        url: baseUrl + "/api/Document/Document",
        data: {
            file: imageFile,
            model: model
        },
        headers: {
            'profileId': trekkerId,
            'Authorization': 'Bearer ' + await AsyncStorage.getItem("id_token"),
            'Content-Type': 'multipart/form-data'
        },
    }).then((response) => {
        // console.log(response);
        return {
            isSuccess: true
        };

    }).catch((error) => {
          console.log(error);
            return {
                isSuccess: false,

            }

    });
Isuru Avishka
  • 43
  • 1
  • 1
  • 4
  • Possible duplicate of [How do I set multipart in axios with react?](https://stackoverflow.com/questions/41878838/how-do-i-set-multipart-in-axios-with-react) – Rocky Mar 01 '19 at 11:50

0 Answers0