NodeJS - 如何从 blob 下载?
NodeJS - How to download from a blob?
如何使用 NodeJS return 来自 BLOB 列的文件?
我正在使用 oracledb
库来处理数据库操作,我有以下代码:
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);
}
我应该怎么做才能从列中读取 BLOB 内容并 return 下载给请求者?
谢谢。
您必须发送内容 header 作为您必须下载的文件类型,然后在 body 中发送缓冲区(假设您从数据库获得的是一个缓冲区)。最后发送代码后结束响应。这是示例代码。
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();
}
如何获取 BLOB 内容作为缓冲区
别忘了设置 oracledb.fetchAsBuffer
属性:
const oracledb = require('oracledb');
oracledb.fetchAsBuffer = [oracledb.BLOB];
如何使用 NodeJS return 来自 BLOB 列的文件?
我正在使用 oracledb
库来处理数据库操作,我有以下代码:
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);
}
我应该怎么做才能从列中读取 BLOB 内容并 return 下载给请求者?
谢谢。
您必须发送内容 header 作为您必须下载的文件类型,然后在 body 中发送缓冲区(假设您从数据库获得的是一个缓冲区)。最后发送代码后结束响应。这是示例代码。
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();
}
如何获取 BLOB 内容作为缓冲区
别忘了设置 oracledb.fetchAsBuffer
属性:
const oracledb = require('oracledb');
oracledb.fetchAsBuffer = [oracledb.BLOB];