首页 > 解决方案 > 无法获取 req.files。React-native - Node js (multer)

问题描述

我正在尝试创建上传图片过程。我需要上传一组图片。

在我的客户上:

const data = new FormData();

pictures.forEach(pic => {
  data.append('pictures', {
   fileName: pic.fileName,
   uri: pic.image,
   type: pic.type,
  });
 });

const headers = {
    'Content-Type': 'multipart/form-data',
    'x-access-token': token,
  };

const diaryUpdatePost = await post(`diary/uploadPictures/${diary}`, pictures, {
    headers,
  });


在服务器端,我的路由器使用 upload.array (multer) 中间件:

const upload = multer({
  dest: 'uploads/',
  limits: { fieldSize: 25 * 1024 * 1024 },
  rename(fieldname, filename) {
    return filename;
  },
});

router.post('/uploadPictures/:name', upload.array('pictures'), diaryController.uploadDiaryPictures);

最后是问题:

在 diaryController.uploadDiaryPictures 我的 req.files 日志未定义。我做错了什么?

exports.uploadDiaryPictures = async (req, res) => {
 
//Logs undefined
  global.logger.info(`Updating diary pictures ${req.files}...`);

};

标签: node.jsreact-nativemultipartform-datamulter

解决方案


推荐阅读