首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >请求formData到API,在上传图像时获取axios中的“网络错误”

请求formData到API,在上传图像时获取axios中的“网络错误”
EN

Stack Overflow用户
提问于 2020-04-12 17:35:35
回答 15查看 18.5K关注 0票数 13

我正在向服务器发出一个POST请求,以上传图像,并使用axios在react本机中发送格式数据。我收到了“网络错误”。我也尝试了获取,但是没有work.using反应本地图像选择器libeary为选择image.in邮递员api工作良好。

代码语言:javascript
复制
        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);
    });
EN

Stack Overflow用户

发布于 2021-06-15 07:21:35

如果使用expoexpo-image-picker,那么问题只存在于图像类型而没有其他问题。

在最新的更新中,他们删除了与path相关的bug (正如其他答案所提到的,更改路径的开头对于旧版本是正确的)。

现在,为了消除这个问题,我们只需要改变类型,并被其他答案提到使用mime,这很好;

代码语言:javascript
复制
 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

})
票数 0
EN
查看全部 15 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61175557

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档