【问题标题】:Req.file returns [object, object] when trying to upload picture using multer and cloudinaryReq.file 在尝试使用 multer 和 cloudinary 上传图片时返回 [object, object]
【发布时间】:2020-04-15 20:26:14
【问题描述】:

我正在尝试使用 multer 和 cloudinary 从表单中保存图像,但在 request.file 和 request.body 中返回的唯一内容是 [object, object]

我的 app.js 文件

const multer = require("multer");
const cloudinary = require("cloudinary");
const cloudinaryStorage = require("multer-storage-cloudinary");

cloudinary.config({
    cloud_name: process.env.CLOUD_NAME,
    api_key: process.env.API_KEY,
    api_secret: process.env.API_SECRET 
});
const storage = cloudinaryStorage({
    cloudinary: cloudinary,
    folder: "demo",
    allowedFormats: ["jpg", "png"],
    transformation: [{ width: 500, height: 500, crop: "limit" }]
});
const parser = multer({ storage: storage });

app.post("/", parser.single("image"), (req, res)=>{
  console.log(req.file) // <--- this returns [object, object]
 }
  

html表单

<form action="/" method="post" encType="multipart/form-data">
  <input type="file" name="image" />
</form>

【问题讨论】:

  • 请发布您的&lt;form&gt;的渲染HTML。
  • 您是否使用调试器准确检查过req.file 是什么?它似乎是一组已发布的文件。

标签: node.js express multer cloudinary


【解决方案1】:

您的表单没有文件上传器doohicky。将输入的类型更改为file 而不是text,然后尝试。

此外,当您想通过console.log 检查对象时,您应该这样做:

console.log( JSON.stringify( theObject ) );

【讨论】:

  • 我在提交问题时不小心把它设置为 type="text",在我的文档中输入的是文件类型。
猜你喜欢
  • 2020-08-07
  • 2013-12-04
  • 1970-01-01
  • 2019-01-16
  • 2021-09-02
  • 2020-11-01
  • 2021-06-02
  • 1970-01-01
  • 2016-05-11
相关资源
最近更新 更多