最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

下载PDF从MySQL如何使用Node.js和EJS

IT培训 admin 3浏览 0评论

下载PDF从MySQL如何使用Node.js和EJS

我想写可以上传PDF到MySQL数据库中,然后在以后的阶段重新下载页面。数据库我已经设置为BLOB,并在这个阶段能够上传PDF与下面的代码数据库。我只是不知道如何从数据库中下载的文件回pdf格式使用EJS和node.js中有没有人已经做到了这一点还是知道如何做到这一点?

HTML

//EJS - HTML side using bodyParser to return value to my .js server

<input type="file" name="file" value="" class="form-control"/><br></p>

NODE JS

//.js server side
app.post('/addfile', function(req, res){
console.log(req.body);
connection.query("INSERT INTO data (file) VALUES ('"+req.body.file+"')", function (err, result){
console.log(result);
res.redirect(URL);
});
});
回答如下:

您可以使用普通的SELECT查询从MySQL获得的文件。

之后,你有这个文件,你可以通过调用res.send快报有关头部中提供的方法发送文件。

它应该是这个样子:

var fileData = new Buffer(blob);
res.writeHead(200, {
  'Content-Type': 'application/pdf',
  'Content-Disposition', 'attachment; filename=data.pdf',
  'Content-Length': fileData.length
});
res.write(fileData);
res.end();

您可能需要设置相关的标题升

下载PDF从MySQL如何使用Node.js和EJS

我想写可以上传PDF到MySQL数据库中,然后在以后的阶段重新下载页面。数据库我已经设置为BLOB,并在这个阶段能够上传PDF与下面的代码数据库。我只是不知道如何从数据库中下载的文件回pdf格式使用EJS和node.js中有没有人已经做到了这一点还是知道如何做到这一点?

HTML

//EJS - HTML side using bodyParser to return value to my .js server

<input type="file" name="file" value="" class="form-control"/><br></p>

NODE JS

//.js server side
app.post('/addfile', function(req, res){
console.log(req.body);
connection.query("INSERT INTO data (file) VALUES ('"+req.body.file+"')", function (err, result){
console.log(result);
res.redirect(URL);
});
});
回答如下:

您可以使用普通的SELECT查询从MySQL获得的文件。

之后,你有这个文件,你可以通过调用res.send快报有关头部中提供的方法发送文件。

它应该是这个样子:

var fileData = new Buffer(blob);
res.writeHead(200, {
  'Content-Type': 'application/pdf',
  'Content-Disposition', 'attachment; filename=data.pdf',
  'Content-Length': fileData.length
});
res.write(fileData);
res.end();

您可能需要设置相关的标题升

发布评论

评论列表 (0)

  1. 暂无评论