有没有一种简单的方法可以使用 Node/PostgreSQL 对任何类型的文件进行简单的文件上传?
Is there an easy way to do a simple file upload using Node/PostgreSQL for any type of file?
我想通过快速服务器上传文件,将文件存储在现有的 postgreSQL 数据库中。
文件像这样进入 POST 终点:
{ name: 'New Data.xlsx',
data: <Buffer 50 4c 03 04 14 01 06 00 08 00 00 24 21 00 1f 0a 93 21 cf 02 00 00 4f 1f 00 00 13 00 08 02 5b 43 6f 6e 74 65 6e 74 ... >,
size: 6880975,
encoding: '7bit',
tempFilePath: '',
truncated: false,
mimetype: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
md5: '535c8576e1c94d169ea5a637487ee3b4',
mv: [Function: mv] }
这是一份相当大的 excel 文档。 Word 文档、pdf、简单的 CSV 等也需要能够上传。
我以类似的方式尝试了 node-postgres 和 sequelize 库:
app.post('/upload', async(req, res) => {
var {upload} = req.files
var data = upload.data
console.log(data);
const x = await pool.query(`insert into attachment (data, file_name, category) values (${data}, '${upload.name}', 'test')`)
// Deconstruct x to get response values
res.send("OK")
});
一些文件,如 txt、纯 csv 文件可以正常上传,但我收到错误消息,例如
error: invalid message format
对于 excel 或 word 文件。
我以前用 MongoDB 做过类似的事情,但我无法切换数据库。我的另一个想法是将生产服务器上的文件简单地存储在 'uploads' 文件中,但我不确定这是好的做法。
有什么建议吗?
已使用查询参数解决。
app.post('/upload', async(req, res) => {
var {upload} = req.files
var data = upload.data
const x = await pool.query(`insert into attachment (data, file_name, category) values (, '${upload.name}', 'test')`, [data])
res.send("OK")
});
我想通过快速服务器上传文件,将文件存储在现有的 postgreSQL 数据库中。
文件像这样进入 POST 终点:
{ name: 'New Data.xlsx',
data: <Buffer 50 4c 03 04 14 01 06 00 08 00 00 24 21 00 1f 0a 93 21 cf 02 00 00 4f 1f 00 00 13 00 08 02 5b 43 6f 6e 74 65 6e 74 ... >,
size: 6880975,
encoding: '7bit',
tempFilePath: '',
truncated: false,
mimetype: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
md5: '535c8576e1c94d169ea5a637487ee3b4',
mv: [Function: mv] }
这是一份相当大的 excel 文档。 Word 文档、pdf、简单的 CSV 等也需要能够上传。
我以类似的方式尝试了 node-postgres 和 sequelize 库:
app.post('/upload', async(req, res) => {
var {upload} = req.files
var data = upload.data
console.log(data);
const x = await pool.query(`insert into attachment (data, file_name, category) values (${data}, '${upload.name}', 'test')`)
// Deconstruct x to get response values
res.send("OK")
});
一些文件,如 txt、纯 csv 文件可以正常上传,但我收到错误消息,例如
error: invalid message format
对于 excel 或 word 文件。
我以前用 MongoDB 做过类似的事情,但我无法切换数据库。我的另一个想法是将生产服务器上的文件简单地存储在 'uploads' 文件中,但我不确定这是好的做法。
有什么建议吗?
已使用查询参数解决。
app.post('/upload', async(req, res) => {
var {upload} = req.files
var data = upload.data
const x = await pool.query(`insert into attachment (data, file_name, category) values (, '${upload.name}', 'test')`, [data])
res.send("OK")
});