File upload 在react native中使用axios、formdata和react native图像裁剪选择器上载图像
我想上传一系列照片,为此,我安装了上述软件包,并试图实现这一点。 *我试过取,也试过取斑点,但没有锁 *开始时,捕获错误是网络错误 现在的问题是,我正在向服务器发送一个空数组 这是我的密码:File upload 在react native中使用axios、formdata和react native图像裁剪选择器上载图像,file-upload,axios,File Upload,Axios,我想上传一系列照片,为此,我安装了上述软件包,并试图实现这一点。 *我试过取,也试过取斑点,但没有锁 *开始时,捕获错误是网络错误 现在的问题是,我正在向服务器发送一个空数组 这是我的密码: export default class Upload extends Component { constructor() { super(); this.state = { token: "", photos: []
export default class Upload extends Component {
constructor() {
super();
this.state = {
token: "",
photos: []
}
}
render() {
return (
<Text onPress={() => this._TakePhoto()}> Pick </Text>
);
}
_TakePhoto() {
ImagePicker.openPicker({
multiple: true
}).then(images => {
images.map((item, index) => {
ImageResizer.createResizedImage(item.path, 1200, 1200, "JPEG", 100)
.then(response => {
// console.warn('Resized img is: ', response);
this.state.photos.push(response);
console.warn('Resized img state is: ', this.state.photos)
this._submit_pictures()
})
.catch(err => {
});
});
});
}
_submit_pictures() {
let formData = new FormData();
for (let i = 0; i < this.state.photos.length; i++) {
let file = {
uri: this.state.photos[0].path,
// uri: this.state.photos[0].path.replace('file:///', ''),
// uri: this.state.photos[0].uri,
type: this.state.photos[0].mime,
name: this.state.photos[0].name
};
formData.append("pics[]", file);
}
// uri: this.state.photos[0].uri.replace("file:///", "file://"),
formData.append("postId", postId);
formData.append("token", token);
console.log('formData value: ', formData)
axios({
url: "https://rahnama.com/webservice/submitPictures",
method: "POST",
headers: {
// "Accept": "application/json",
"Content-Type": "multipart/form-data"
},
// formData
body: formData
})
.then(response => {
console.warn('upload res: ', response);
}).catch((error) => console.warn('upload err: ', error.response.request._response));
}
}
导出默认类上载扩展组件{
构造函数(){
超级();
此.state={
令牌:“”,
照片:[]
}
}
render(){
返回(
这个。_TakePhoto()}>拾取
);
}
_拍照{
ImagePicker.openPicker({
多重:对
})。然后(图像=>{
images.map((项目,索引)=>{
ImageResizer.createResizedImage(item.path,12001200,“JPEG”,100)
。然后(响应=>{
//console.warn('Resized img is:',response);
this.state.photos.push(响应);
console.warn('调整后的img状态为:',this.state.photos)
这个._提交_图片()
})
.catch(错误=>{
});
});
});
}
_提交图片(){
设formData=new formData();
for(设i=0;i{
console.warn('upload res:',response);
}).catch((错误)=>console.warn('upload err:',error.response.request.\u response));
}
}
我通过将数据作为base64(图像)发送解决了这个问题