我正在尝试写一个网页,可以上传到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>节点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);
});
});发布于 2019-02-11 14:09:43
您可以使用普通的SELECT查询从MySQL获取该文件。
有了文件之后,您可以通过调用Express提供的带有相关头部的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();您可能想要设置相关的头l
https://stackoverflow.com/questions/54624485
复制相似问题