Node.js 将文件对象保存到本地文件系统:NodeJS EJS
我有一个应用程序,应该能够将数据从MySQL下载到用户的本地文件系统中 我能够获取头文件和文件对象,但无法将其保存到我的目录中 这是下载PDF文件的函数Node.js 将文件对象保存到本地文件系统:NodeJS EJS,node.js,ejs,Node.js,Ejs,我有一个应用程序,应该能够将数据从MySQL下载到用户的本地文件系统中 我能够获取头文件和文件对象,但无法将其保存到我的目录中 这是下载PDF文件的函数 /* Function to download pdf file */ function handlesDownloadPDFfile(req, res, next) { pmid = '1'; userAdminDao.handlesDownloadPDFfile(pmid, function (result) {
/* Function to download pdf file */
function handlesDownloadPDFfile(req, res, next) {
pmid = '1';
userAdminDao.handlesDownloadPDFfile(pmid, function (result) {
console.log(result);
var filePath = __dirname;
console.log(filePath);
var file = 'test.pdf';
res.end(new Buffer(result.pdf_content), 'binary');
res.writeHead(200, {
"Content-Type": "application/pdf",
"Content-Disposition": "attachment; filename=" + file
});
fs.createReadStream(filePath + file).pipe(res);
})
}
console.log(result)
确实为我提供了pdf对象,但是,我无法将其保存到文件路径中
更新
我现在可以下载文件了,但是我遇到了这个错误
Error: ENOENT: no such file or directory, open '\routes\test.pdf'
Emitted 'error' event on ReadStream instance at:
at internal/fs/streams.js:136:12
at FSReqCallback.oncomplete (fs.js:156:23)
我也得到了这个错误
Error: ENOENT: no such file or directory, open '\routes\test.pdf'
Emitted 'error' event on ReadStream instance at:
at internal/fs/streams.js:136:12
at FSReqCallback.oncomplete (fs.js:156:23)
谢谢你,谢谢你抽出时间 有关以下内容,请参阅文档:
此方法向服务器发出信号,表示所有响应头和主体都已发送;该服务器应考虑完成此消息。< /P>
在您的代码中,您应该替换res.writeHead()和res.end()的顺序我可以下载该文件,但是,在下载该文件后,我收到此错误
失败的网络错误
。不确定这是否是网络问题或与代码有关。我也收到此错误error:enoint:没有这样的文件或目录,请在ReadStream实例上打开“\routes\test.pdf”发出的“error”事件,地址为:at internal/fs/streams.js:136:12 at FSReqCallback.oncomplete(fs.js:156:23)