gpt4 book ai didi

javascript - 使用 NestJS 将文件存储在 PostgreSQL 数据库中

转载 作者:行者123 更新时间:2023-12-02 01:54:10 25 4
gpt4 key购买 nike

我在 React 中有一种形式。它有很多领域。一旦用户单击提交按钮,所有字段都应保存在数据库中。它还包含一个文件附件(pdf)

enter image description here

我不知道存储文件的变量的数据类型是什么,我应该在实体类中采用。另外数据库列类型应该是什么。我正在使用 TypeORM 来实现同样的目的。

   @IsNotEmpty()
@IsDate()
endDate: Date;
@Column()
@IsNotEmpty()
@IsString()
personIncharge: string;

@Column()
@IsNotEmpty()
@IsString()
country: string;

@Column()
@IsNotEmpty()
@IsString()
comments: string;

attachFile: string; // Should I take file or string?

最佳答案

您可能会在此 StackOverflow comment 中找到您的解决方案

基本上,您可以在 TypeORM 注释中将列类型转换为 blob 或 longblob,然后在实体的字段中使用类型 Buffer

@Column({type: 'longblob'})
attachFile: Buffer;

然后您将能够提供该文件,如帖子示例中所示:

app.get("/file/:id", async (req, res)=>{
try {
const repo = getConnection().getRepository(MYFile)
const result_find = await repo.findOne(req.params.id)
console.log(result_find);
var fileData = result_find.data;
res.writeHead(200, {
'Content-Type': result_find.mimeType,
'Content-Disposition': 'attachment; filename=' + result_find.name,
'Content-Length': fileData.length
});
res.write(fileData);
res.end();
} catch (error) {
console.log(error)
res.send("ERROR")
}
})

关于javascript - 使用 NestJS 将文件存储在 PostgreSQL 数据库中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69821083/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com