Node.js-使用multer上传文件,并使用Sendgrid

const multer  = require('multer');
const storage = multer.memoryStorage();
const upload = multer({ storage: storage }).single('attachment');

// sgMail intialization

exports.sendMail = (req,res) => {
  upload(req,res,err => {
    if(!err) {
      const fileInfo = req.file;
      const msg = {
        to: 'toemail@gmail.com',from: 'fromemail@gmail.com',subject: 'Welcome!',html: `<p>Some text description</p>`,attachments: [
          {
            content: fileInfo.buffer.toString('base64'),filename: fileInfo.key,type: 'text/plain',disposition: 'attachment'
          },],};
      sgMail.send(msg)
        .then(() => {
          res.json({data: 'success});
        })
        .catch((err) => {
          res.json({data: 'error'});
        });
     }
  });
}

错误:“ fileInfo”未定义,因此出现以下错误:

TypeError: Cannot read property 'buffer' of undefined

查询:

  1. 未设置Content-Type标头。如果需要,值是多少?
  2. multer实施有问题
zb415 回答:Node.js-使用multer上传文件,并使用Sendgrid

const fileInfo = req.file;应该为const fileInfo = req.files;(复数)。

输出req.files的内容,因为您很可能必须以上传的数组索引或键名作为目标。

本文链接:https://www.f2er.com/3138675.html

大家都在问