【问题标题】:Req.file object is always undefined, HTTP file uploadReq.file 对象始终未定义,HTTP 文件上传
【发布时间】:2017-03-29 13:03:10
【问题描述】:

我正在尝试使用 HTTP POST 方法和 enctype="multipart/form-data" 在流星中上传文件

WebApp.connectHandlers.use("/api/v1/upload", function(req, res, next)   {
  console.log(req.files); //undefined
  console.log(req.file); //undefined
  console.log(req);
})

我尝试使用 WebApp,但在请求对象下获得了未定义的文件属性

我也尝试过使用 multer 和 Picker,但没有成功。

const _multerInstanceConfig = { dest: '/tmp' }; // Temp dir for multer
const _multerInstance = multer(_multerInstanceConfig);

Picker.middleware(_multerInstance.single('photo'));
Picker.route('/api/v1/upload', function(params, req, res, next) {
  console.log(req.files); //undefined
  console.log(req.file); //undefined
  console.log(req);
})

这是我要上传的最简单的形式

<form action="http://localhost:3000/api/v1/upload" method="POST" enctype="multipart/form-data">
    <input type="file" name="file" />
    <input type="submit" value="Upload File" />
</form>

我在这里遗漏了什么吗?不确定。 另外,我不想通过 DDP 使用 base64 数据字符串上传文件,因为它非常慢。

我检查了几个链接也没有运气
multer - req.file always undefined
https://github.com/noris666/Meteor-Files-POST-Example

PS:我需要通过原生 Android/iOS 客户端上传图片。

【问题讨论】:

    标签: node.js meteor


    【解决方案1】:

    谢谢大家,谁花时间在我的问题上,我从 git issue 得到了问题的解决方案,我提出了。 如果其他人也面临类似问题,我会在此处发布我的解决方案。

    因为这个输入框的名字

    <input type="file" name="file" />
    

    不匹配
    Picker.middleware(_multerInstance.single('photo'));
    

    将它们中的任何一个更改为都会使其完美运行。

    【讨论】:

      猜你喜欢
      • 2021-11-21
      • 2016-06-21
      • 2020-04-29
      • 1970-01-01
      • 1970-01-01
      • 2018-08-04
      • 1970-01-01
      • 2019-04-04
      • 1970-01-01
      相关资源
      最近更新 更多