【问题标题】:react native upload image with axios用 axios 反应原生上传图片
【发布时间】:2020-11-16 09:35:49
【问题描述】:

我有这个数据 来自 react-native-image-picker

data: "/9j/4AAQSkZJRgABAQAAAQABAAD//gA7Q1JFQVRPUjogZ2Qtan" => 90kb
fileName: "77677.jpg"
fileSize: 67542
height: 600
isVertical: true
originalRotation: 0
path: "/storage/emulated/0/Download/77677.jpg"
type: "image/jpeg"
uri: "content://media/external/images/media/18584"
width: 399
__proto__: Object

我尝试将此数据设置为对象类型#FromData 以上传#img

var binaryDataInBase64 = new FormData();
        binaryDataInBase64.append('file', {
            // data: response.data,
            uri: 'file://' + response.uri,
            name: 'MMM.jpg',
            type: response.type
        })
        console.log('=======================>', binaryDataInBase64)
        axios.post(
            'https://danskeclouddemo.com/api/file',
            { file: binaryDataInBase64 },
            { headers: { Authorization: `Bearer ${token}`, 'Content-Type': 'multipart/form-data'  } }
        ).then(res => {
            console.log(res)
        }).catch(error => {
            console.log(error)
        })

这是后端邮递员工作良好Postman collection

//======================= //======================= 编辑

有人说0.61.5之后的react native版本的问题 在这个链接issues

【问题讨论】:

  • 请勿发布图片。邮政编码/文字。
  • 好的,我会编辑那个,
  • 另外,你正在做console.log(error),尝试做console.log(error.response);
  • 错误更清楚了
  • 但还是不行

标签: reactjs typescript react-native axios react-native-android


【解决方案1】:

你的表单数据一定是这样的。

formdata.append('file',{
    uri: Platform.OS === 'android' ? photo.uri : 'file://' + photo.uri,
    name: 'test',
    type: 'image/jpeg' // or your mime type what you want
});

然后

axios.post('/users/profilepic', formdata, config).then((res) => {
    console.log(res.data);
    return res.data;
  });

【讨论】:

【解决方案2】:

而不是使用,axiom 使用 fetch 简单,方法来上传,图像

this.setState({imageLoading:true})
    const credentials={
       userId:this.state.userId
    }
    try {
      let response = await fetch(
        'url',
        {
            'method': 'POST',
            headers: {
                'Accept': 'application/json',
                'Content-Type': 'multipart/form-data',
            },
             body: createFormData(source, credentials)
        }
      );
      
      if (response.status == 200) {
            response.json().then(data => {
            
            switch (data.status) {
                case 200:
                   
                    break;
            }
         });
      }else {
        this.setState({imageLoading:false ,isLoading:false})
             }
    } catch (error) {
      this.setState({imageLoading:false,isLoading:false})
      console.error(error);
    }


const createFormData=(image,body)=>{
var data = new FormData();
data.append(image, {
uri:  Platform.OS === "android" ? image.uri : image.uri.replace("file://", ""), 
name: `dummy${Date.now()}.jpg`,
type: 'image/*'
})


Object.keys(body).forEach(key => {
    data.append(key, body[key]);
});
return data
}

【讨论】:

猜你喜欢
  • 2023-03-08
  • 2018-01-31
  • 1970-01-01
  • 1970-01-01
  • 2018-10-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-01
相关资源
最近更新 更多