【问题标题】:How to post images to NodeJS server form Angular 2+如何从 Angular 2+ 将图像发布到 NodeJS 服务器
【发布时间】:2018-01-11 15:51:58
【问题描述】:

我有一个 NodeJS rest-API,它在同一个函数中接受图像和文本等内容。

我在我的 NodeJS 服务器中使用 multer 来处理图像,更准确地说,我在下面有相同的示例,只是我也有内容。

var cpUpload = upload.fields([{ name: 'avatar', maxCount: 1 }, { name: 'gallery', maxCount: 8 }])
app.post('/cool-profile', cpUpload, function (req, res, next) {
  // req.files is an object (String -> Array) where fieldname is the key, and the value is array of files
  //
  // e.g.
  //  req.files['avatar'][0] -> File
  //  req.files['gallery'] -> Array
  //
  // req.body will contain the text fields, if there were any
})

如何将来自 Angular 4 前端页面的图像发送到 NodeJS 后端。

谢谢!

【问题讨论】:

标签: javascript node.js angular express multer


【解决方案1】:

通过 formdata 发送图片文件:

let myForm = new FormData();
myForm.append('avatar',  image);

【讨论】:

    猜你喜欢
    • 2015-09-13
    • 2020-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-27
    • 2016-04-19
    相关资源
    最近更新 更多