如何 post 图像到 NodeJS 服务器表单 Angular 2+

How to post images to NodeJS server form Angular 2+

我有一个 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 后端。

谢谢!

通过表单数据发送图片文件:

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