javascript - NodeJS - How to download from a blob? - Stack Overflow

How can I return a file from a BLOB column using NodeJS?I'm using the oracledb library to handle t

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
Add a ment  | 

1 Answer 1

Reset to default 3

You 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];

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744212343a4563393.html

相关推荐

  • javascript - NodeJS - How to download from a blob? - Stack Overflow

    How can I return a file from a BLOB column using NodeJS?I'm using the oracledb library to handle t

    8天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信