2016-06-28 4 views
1

디스크에 저장하지 않고 sendgrid에 파일을 첨부하려고합니다. 나는 그것을 처리하기 위해 스트림을 사용하고 싶다. 내가 생각Sendgrid와 Multer를 사용하여 이메일에 파일을 첨부하는 방법

var multer = require('multer'); 
    var upload = multer({ storage: multer.memoryStorage({})}); 
    mail = new helper.Mail(from_email, subject, to_email, content); 
    console.log(req.body.File); 
    attachment = new helper.Attachment(req.body.File); 
    mail.addAttachment(attachment) 
+0

문제가 있습니까? nodemailer.js 프레임 워크를 사용하여 전자 메일을 보내시겠습니까? 'helper.Mail' 코드를 공유 할 수 있습니까? 무슨'console.log (req.body.File);'보여줍니까? – danilodeveloper

+0

@danilodeveloper의 요청에 따라 더 많은 정보/코드를 제공 할 수 있습니까? – cviejo

답변

3

는 스트림을 사용 할 수 없기 때문에 :

하지만 당신은 같은 첨부 파일로 반환 buffer를 사용하여 얻을 수 있습니다 큰 첨부 파일이나 높은 동시성과 함께 사용할 경우

var multer = require('multer') 
    , upload = multer({ storage: multer.memoryStorage({})}) 
    , helper = require('sendgrid').mail; 

app.post('/send', upload.single('attachment'), function (req, res, next) { 
    // req.file is the `attachment` file 
    // req.body will hold the text fields, if there were any 

    var mail = new helper.Mail(from_email, subject, to_email, content) 
    , attachment = new helper.Attachment() 
    , fileInfo = req.file; 

    attachment.setFilename(fileInfo.originalname); 
    attachment.setType(fileInfo.mimetype); 
    attachment.setContent(fileInfo.buffer.toString('base64')); 
    attachment.setDisposition('attachment'); 

    mail.addAttachment(attachment); 
    /* ... */ 
}); 
그것은 (메모리 부족 오류) 메모리 사용에 영향을 미치는 수

.

+0

니스, 나는 그것을 시험 할 것이다! 나는'req.file' 객체를 통해 반복하는 경우 여러 파일과 함께 작동 할 것입니다. –

+0

버퍼를 base64 문자열로 변환해야합니다. 그렇지 않으면 sharm처럼 작동합니다. D Thanks! 'attachment.setContent (fileInfo.buffer.toString ('base64')); ' –

+0

고마워요! 예제에서 수정하십시오. Buffer 객체를 처리하기 위해'Attachment' 객체를 사용한다고 가정합니다. – Dario

관련 문제