React Native fench FormData 上传文件

前端之家收集整理的这篇文章主要介绍了React Native fench FormData 上传文件前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

首先说下这个问题很坑,append的Js的object类型无效,我们后台用的是java
上错不成功的代码

let url = 'http://192.168.1.120:8090/app/release';
        let params = {parentId: "0",text: this.state.textContent,postType: 0,profitType: 0}

        let formdata = new FormData();
        formdata.append('file',{uri: this.state.envURL[0].uri,type: 'image/jpg',name: 'image.jpg'});
        formdata.append('appUserPost',String(params));

        fetch(url,{
            method: 'post',headers: {
                'Content-Type': 'multipart/form-data; boundary=6ff46e0b6b5148d984f148b6542e5a5d',},body: formdata
        }).then(response => {
            console.log("image uploaded")
        }).catch(err => {
            console.log(err)
        })

正确的是拆分object
成功的代码

let url = 'http://192.168.0.89:8080/app/release';

        var appUserPost = { 'appUserPost': params,type: 'application/octet-stream' }
        var file = { uri: this.state.envURL,type: 'multipart/form-data',name: 'image.png' }
        let formdata = new FormData();

        formdata.append('file',file); // you can append anyone.

        formdata.append('parentId',"0")
        formdata.append('text',"asdfasd")
        formdata.append('postType',0)
        formdata.append('profitType',1)
        console.log("formdata",JSON.stringify(formdata))

        fetch(url,{
            method: 'POST',headers: {
                'Accept': 'application/json',// 'Content-Type': 'multipart/form-data',processData: false,contentType: false,body: formdata
        }).then(function (response) {
            console.log(response)
        }).catch(err => {
            console.log(err)
        })


    }
原文链接:https://www.f2er.com/react/302292.html

猜你在找的React相关文章