admin管理员组文章数量:1399119
How can I return a file from a BLOB column using NodeJS?
I'm using the oracledb
library to handle the database operations and I have the following code:
async function getFile(req, res) {
let filename = req.params.filename;
let file = await selectFileFromDb(filename);
file = file.rows[0][0]; //Column that contains the blob content
//I would like to return something like this
res.download(file);
}
What should I do to read the BLOB content from the column and return as a download to the requester?
Thank you.
How can I return a file from a BLOB column using NodeJS?
I'm using the oracledb
library to handle the database operations and I have the following code:
async function getFile(req, res) {
let filename = req.params.filename;
let file = await selectFileFromDb(filename);
file = file.rows[0][0]; //Column that contains the blob content
//I would like to return something like this
res.download(file);
}
What should I do to read the BLOB content from the column and return as a download to the requester?
Thank you.
Share Improve this question edited Oct 28, 2019 at 9:43 MT0 169k12 gold badges66 silver badges129 bronze badges asked Aug 29, 2019 at 13:06 WitnessTruthWitnessTruth 5892 gold badges11 silver badges32 bronze badges 1- For reference, the node-oracledb LOB documentation is here Working with CLOB and BLOB Data and there are examples in the node-oracledb project directory github./oracle/node-oracledb/tree/master/examples – Christopher Jones Commented Sep 2, 2019 at 0:16
1 Answer
Reset to default 3You have to send content header as the type of file that you have to download and then send the buffer (asuming what you got from the db is a buffer ) in the body . Finally end the response after sending the code. Here is a sample code .
async function getFile(req, res) {
let filename = req.params.filename;
let file = await selectFileFromDb(filename);
file = file.rows[0][0]; //Column that contains the blob content
res.setHeader('Content-Length', file.length);
res.write(file, 'binary');
res.end();
}
HOW TO GET THE BLOB CONTENT AS A BUFFER
Do not forget to set the oracledb.fetchAsBuffer
property:
const oracledb = require('oracledb');
oracledb.fetchAsBuffer = [oracledb.BLOB];
本文标签: javascriptNodeJSHow to download from a blobStack Overflow
版权声明:本文标题:javascript - NodeJS - How to download from a blob? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744212343a2595478.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论