你好,
我正在使用@sendgrid/mail - "^6.3.1" & node - 10.14.1。
我的问题是,当我从 excel 附件中的数据库中发送单行数据时,我能够收到带有附件的邮件,但是当我从 excel 中的数据库数据中作为附件发送多行数据时,我收到的附件为 0 字节。 我也在文件中嵌入了图像。 我正在尝试附加通过 exceljs 模块创建的相同文件。 我尝试了很多但失败了..我可以在浏览器中使用此代码生成相同的 excel 文件-
"
res.setHeader("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
res.setHeader("Content-Disposition", "attachment;filename='" + excelFile.excelOffFile + "'");
workbookFinal.xlsx.write(res).then(() => {
重发(); });s
"
我发送邮件的代码是 -
sgMail.send({
to: config.get("mail.defaultAdd"),
来自:来自邮件添加,
主题:“通过软件发送带有excel附件的订单”,
html: <h1>Test Mail</h1>
,
附件:[
{
内容:fs.readFileSync(excelFile.excelOffFilePath, {encoding:"base64"}),
文件名:excelFile.excelOffFile,
类型:“应用程序/vnd.ms-excel”
},
{
内容:fs.readFileSync(excelFile.excelFactFilePath,{编码:“base64”}),
文件名:excelFile.excelFactFile,
类型:“应用程序/vnd.ms-excel”
},
{
内容:fs.readFileSync(excelFile.excelBrFilePath,{编码:“base64”}),
文件名:excelFile.excelBrFile,
类型:“应用程序/vnd.ms-excel”
},
{
内容:fs.readFileSync(excelFile.excelSuFilePath,{编码:“base64”}),
文件名:excelFile.excelSuFile,
类型:“应用程序/vnd.ms-excel”
}
]
})
邮件截图——
请帮助我如何解决相同的问题。
你好@prasoojalan ,
听起来您的文件路径或文件编码有问题。 我会仔细检查代码中excelFile.*
的值,以确保文件存在。 然后,我会确保文件本身是正确的 base64 编码。 也许这个工具可以帮助后者。
最诚挚的问候,
埃尔默
你好@埃尔默
我创建和发送文件的完整代码如下 -
我尝试给一个附件根路径,但它也没有奏效。 生成的相同 excel 文件已在浏览器中成功下载..我缺少什么代码或者我无法正确理解这个概念......你能帮帮我吗..
在以“C”为参数的 createExcelFile 函数中,我将返回工作簿,而不是在其他情况下。
exports.getExcelFile = (req, res, next) => {
const excelFile = {};
Order.findById(req.params.id).then(order => {
excelFile.excelOffFile = "Off_" + order.factory + "_" + order._id + ".xls";
excelFile.excelOffFilePath = path.join("data", "excel", excelFile.excelOffFile);
excelFile.excelFactFile = "Fact_" + order.factory + "_" + order._id + ".xls";
excelFile.excelFactFilePath = path.join("data", "excel", excelFile.excelFactFile);
excelFile.excelBrFile = "Br_" + order.factory + "_" + order._id + ".xls";
excelFile.excelBrFilePath = path.join("data", "excel", excelFile.excelBrFile);
excelFile.excelSuFile = "Su_" + order.factory + "_" + order._id + ".xls";
excelFile.excelSuFilePath = path.join("data", "excel", excelFile.excelSuFile);
let workbookFinal = createExcelFile(order, "C", excelFile.excelOffFilePath);
createExcelFile(order, "F", excelFile.excelFactFilePath);
createExcelFile(order, "B", excelFile.excelBrFilePath);
createExcelFile(order, "S", excelFile.excelSuFilePath);
sgMail.send({
to: config.get("mail.defaultAdd"),
from: defaultMailId,
subject: "Order with excel attachment send through software ",
html: `<h1>
The mail has been sent as trial through program for testing excel file attachement .
</h1>
<h3>
Office file - ${url.format({ protocol: req.protocol, host: req.get("host"), pathname: "orders/genExcelFile/" + excelFile.excelOffFile })}</br>
Fact file - ${url.format({ protocol: req.protocol, host: req.get("host"), pathname: "orders/genExcelFile/" + excelFile.excelFactFile })}</br>
Br file - ${url.format({ protocol: req.protocol, host: req.get("host"), pathname: "orders/genExcelFile/" + excelFile.excelBrFile })}</br>
Su file - ${url.format({ protocol: req.protocol, host: req.get("host"), pathname: "orders/genExcelFile/" + excelFile.excelSuFile })}</br>
</h3>
`,
attachments: [
{
content: fs.readFileSync(path.join(rootDir.rootDir, excelFile.excelOffFilePath), { encoding: "base64" }),
filename: excelFile.excelOffFile,
},
{
content: fs.readFileSync(excelFile.excelFactFilePath, { encoding: "base64" }),
filename: excelFile.excelFactFile,
},
{
content: fs.readFileSync(excelFile.excelBrFilePath, { encoding: "base64" }),
filename: excelFile.excelBrFile,
},
{
content: fs.readFileSync(excelFile.excelSuFilePath, { encoding: "base64" }),
filename: excelFile.excelSuFile,
}
]
}).then(result => {
console.log("mail sent successfully...");
}).catch(err => console.log(err));
res.setHeader("Content-Type", "application/vnd.ms-excel");
res.setHeader("Content-Disposition", "attachment;filename='" + excelFile.excelOffFile + "'");
workbookFinal.xlsx.write(res).then(() => {
res.end();
});
}).catch(err => console.log(err));
};
你好@埃尔默
感谢您的建议,它帮助我理解了我的代码流程,这让我很困惑。
我找出了我的错误并解决了它。 我的代码在发送邮件时生成了没有记录的 excel 文件。 所以,我从函数中删除了我的邮件代码并创建了一个单独的函数,然后在 getExcelFile 函数之后调用该函数。 这样我就可以确定文件已创建并且文件已正确附加。
再次感谢您在提出任何进一步问题之前帮助我专注于我的代码。
最诚挚的问候,
Prasoon Kumar Jalan