react native Current request is

2020-08-21  本文已影响0人  追逐繁星的阿忠

版本说明很重要:

"react-native": "0.62.2",

文件上传后台报错


code: 500
data: {}
msg: "Current request is not a multipart request"
success: false

实操


ImagePicker.showImagePicker(options, response => {
      if (response.didCancel) {
        console.log('User cancelled image picker');
      } else if (response.error) {
        console.log('ImagePicker Error: ', response.error);
      } else if (response.customButton) {
        console.log('User tapped custom button: ', response.customButton);
      } else {
        let source = {uri: response.uri};
        console.log(source);
        setImgSource(source);
        let fileObj = {
          //name: response.fileName || '营业执照.jpg',
          name: 'image.png' || 'image.jpg',
          //path: response.path,
          //type: response.type,
          type: 'multipart/form-data',
          uri: response.path,
        }
        uploadImg(fileObj);
      }
    });


const uploadImg = (fileObj) => {
     // api
     console.log(fileObj);
     // 方案一
     let formData = new FormData();//如果需要上传多张图片,需要遍历数组,把图片的路径数组放入formData中
     formData.append("file",fileObj);   //这里的files就是后台需要的key
fetch('/api/blade-resource/oss/endpoint/put-file',{
       method:'POST',
       mode: "cors",
       headers:{
         'Content-Type':'multipart/form-data',
       },
       body:formData,
     })
         .then((response) => response.text() )
         .then((responseData)=>{
           Alert.alert('111111111111');
           console.log('responseData=',responseData);
         })
         .catch((error)=>{console.error('error',error)});
}

上一篇下一篇

猜你喜欢

热点阅读