【发布时间】:2021-12-19 07:13:10
【问题描述】:
谁能帮忙解决这个问题。
我需要使用 TypeORM/NestJS 将文件上传到 PostgreSQL 数据库。文件是表单的一部分。
我已经采取了以下实体类。
export class Certificate {
@ApiProperty()
@PrimaryGeneratedColumn()
@Exclude()
id: number;
@ApiProperty()
@Column({ type: 'varchar', length: 15 })
statusOfReport: string;
@ApiProperty()
@Column({ type: 'varchar', length: 100 })
sponser: string;
@ApiProperty()
@Column({ type: 'varchar', length: 100 })
address: string;
@ApiProperty()
@Column({ type: 'varchar', length: 100 })
address2: string;
@ApiProperty()
@Column()
zipCOde: string;
@ApiProperty()
@Column()
city: string;
@ApiProperty()
@Column()
protoColNo: string;
@ApiProperty()
@Column()
molecules: string;
@ApiProperty()
@Column()
unAuthMolecule: string;
@ApiProperty()
@Column()
phaseOfTrial: number;
@ApiProperty()
@Column()
noOfSubjects: number;
@ApiProperty()
@Column()
startDate: Date;
@ApiProperty()
@Column()
endDate: Date;
@ApiProperty()
@Column()
personInCharge: string;
@ApiProperty()
@Column()
country: string;
@ApiProperty()
@Column()
comments: string;
@ApiProperty()
@Column({ type: 'bytea' })
attachFile: Uint8Array;
下面是我的控制器方法。
@Post()
create(@Body() createCertificateDto: CreateCertificateDto): Promise<Certificate> {
return this.certificatesService.create(createCertificateDto);
}
下面是我的服务类方法。
async create(createCertificateDto: CreateCertificateDto): Promise<Certificate> {
return this.certificateRepository.save(createCertificateDto);
}
我正在将文件保存为数据。我需要做哪些更改才能在数据库中上传文件。文件可以是excel, pdf, text 等。现有的答案没有帮助。
【问题讨论】:
-
我使用的是 13.1。我只是将标签用于更广泛的受众。
-
好吧,as documented in the manual
pg_read_file返回text,而不是bytea您需要使用pg_read_binary_file- 但只有在运行 pgAdmin 的计算机上也运行 Postgres 时才有效
标签: node.js postgresql rest nestjs typeorm