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

本文标签: javascriptNodeJSHow to download from a blobStack Overflow