如何使用axios在react-native中上传图像?

时间:2019-03-01 11:29:32

标签: react-native post axios

我正在尝试使用Axios上传图像,但得到:请求失败,状态码为500。我没有后端问题,因为我可以使用邮递员上传图像,一切都很好。 这是我在FileUpload.js中的addDocument()。

 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'});
        } 
    });
};

这是我在document.service.js中的addDocument()。

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,

            }

    });

0 个答案:

没有答案