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
查询:
- 未设置Content-Type标头。如果需要,值是多少?
- multer实施有问题