反应本机失败的上传图像 [英] React Native Failed Upload Image

查看:72
本文介绍了反应本机失败的上传图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

过去2天,我一直在努力破解使用React Native将文件/图像上传到MongoDB的问题.我从字面上看所有相关的论坛,但没有运气.我读了几个论坛,他们举了一个例子,但我没有成功.这是我编写的示例代码.

I am struggling from the past 2 days to crack the file/image upload with React Native to MongoDB. I literally read all the related forums but there is no luck. I read couple of forums and they gave a sample example but I wasn't succeeded. Here are the sample codes that I wrote.

const { uri } = await this.camera.takePictureAsync(options);

let formData = new FormData();
formData.append('file', {
  uri: uri.replace("file:///", ""),
  type:'image/jpg', name:'userProfile.jpg',
});

const rawResponse = await fetch('http://192.168.1.5:9000/api/contrats/upload', {
  method: 'POST',
  body: formData,
  headers: {
    Accept: 'application/json',
    'Content-Type': 'multipart/form-data; charset=utf-8',
    },
});


const content = await rawResponse.json();

console.log(content);

var storage = multer.diskStorage({
  destination: (req, file, cb) => {
    
 
    cb(null, __basedir + '/resources/static/assets/uploads');
    
  },
  filename: (req, file1, cb) => {
    console.log("file : ", file);
    let name = file.originalname || file.name;
    let extension = name.substr((~-name.lastIndexOf(".") >>> 0) + 2);
    let filename = generateId() +"."+ extension;                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       nsion;
    cb(null, filename)

  },


});

var upload = multer({
  storage: storage,
  limits: {
    fileSize: 1024 * 1024 * 5
  }
});

推荐答案

尝试以下方法

      let body = new FormData();
       let filename = uri.split('/').pop();
       body.append('file',  {uri:uri, name:filename, type:'image/jpg', });
       const header = {
           'Accept': 'application/json',
           'content-type': 'multipart/form-data',
         }
           fetch("http://192.168.1.5:9000/api/contrats/upload", {
               method: 'POST',
               headers: header,
               body:body,
           }).then(response => response.json())
            .then(res => console.log(res))
            .catch(err => console.log("err", err)

这篇关于反应本机失败的上传图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆