我正在向服务器发出一个POST请求,以上传图像,并使用axios在react本机中发送格式数据。我收到了“网络错误”。我也尝试了获取,但是没有work.using反应本地图像选择器libeary为选择image.in邮递员api工作良好。
formData.append('title', Title);
formData.append('class_id', selectClass._id)
formData.append('subject_id', checkSelected)
formData.append('teacher_id', userId)
formData.append('description', lecture);
formData.append('type', 'image');
var arr=[];
arr.push(imageSource)
arr.map((file,index)=>{
formData.append('file',{
uri:file.path,
type:file.type,
name:file.name
})
})
axios({
method: 'post',
url: URL + 'admin/assignment/create',
data: data,
headers: {
"content-type": "multipart/form-data",
'x-auth-token': token,
},
})
.then(function (response) {
//handle success
console.log('axios assigment post',response);
})
.catch(function (response) {
//handle error
console.log('axios assigment post',response);
});发布于 2021-06-15 07:21:35
如果使用expo和expo-image-picker,那么问题只存在于图像类型而没有其他问题。
在最新的更新中,他们删除了与path相关的bug (正如其他答案所提到的,更改路径的开头对于旧版本是正确的)。
现在,为了消除这个问题,我们只需要改变类型,并被其他答案提到使用mime,这很好;
import mime from 'mime'
const data = new FormData();
data.append('image', {
uri: image.uri,
name: image.uri.split('/').pop() // getting the text after the last slash which is the name of the image
type: mime.getType(image.uri) // image.type returns 'image' but mime.getType(image.uri) returns 'image/jpeg' or whatever is the type
})https://stackoverflow.com/questions/61175557
复制相似问题