我正在尝试将图像或文件从移动设备发送到后端,但是图像选择器无法给我图像提供无法识别的URI。
_pickImage = async () => {
let result = await DocumentPicker.getDocumentAsync({});
console.log(result);
if (!result.cancelled) {
this.setState({ files: result.uri });
// upload file && get File Id
axios({
method: "post",
url: "https://someurl",
data: {
image: this.state.file
},
headers: {
"Content-Type": "multipart/form-data",
// TODO: upadte with dynamic token
Authorization:
"Bearer {token}"
}
}).then(
function(response) {
console.log(response.data.message);
// this.setState({
// hospItems: response.data.data
// });
}.bind(this)
);
}
};
console.log(结果);返回:: 对象{ “ name”:“ 1554736582840.jpg”, “尺寸”:45240, “ type”:“成功”, “ uri”:“文件:///数据/用户/0/host.exp.exponent/cache/ExperienceData/%2540abhinavofficial%252Fmobi-medic/DocumentPicker/cd34bbd8-7c0c-414a-a76d-8d06e2abf670jpg” }
来自服务器的响应: 图片字段为必填项。