需要使用 TypeORM/NestJS 在 PostgreSQL 数据库中上传文件

Need to upload file in PostgreSQL database using TypeORM/NestJS

谁能帮忙解决这个问题。

我需要使用 TypeORM/NestJS 将文件上传到 PostgreSQL 数据库。文件是表单的一部分。

我选择了以下实体 class。

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);
}

下面是我的服务class方法。

async create(createCertificateDto: CreateCertificateDto): Promise<Certificate> {
    return this.certificateRepository.save(createCertificateDto);
}

我正在将文件保存为数据。我需要做哪些更改才能在数据库中上传文件。文件可以是 excel, pdf, text 等。现有答案没有帮助。

据我所知,您缺少控制器上的文件装饰器。

@Post()
@UseInterceptors(FileInterceptor('file'))
create(@Body() createCertificateDto: CreateCertificateDto, @UploadedFile() file: any): Promise<Certificate> {
    return this.certificatesService.create(createCertificateDto, file);
}

您可能还想查看此 以查看事物的 TypeORM 方面。

在您的控制器上:

import {
    Controller,
    Post,
    Body,
    Patch,
    Param,
    Get,
    Delete,
    UsePipes,
    Query,
    Request,
    UseInterceptors, UploadedFile, UseGuards,
    } from '@nestjs/common';
   import {FileInterceptor} from '@nestjs/platform-express';
   import { extname, join } from 'path';
   import { diskStorage } from 'multer';

   @Post()
   @UseInterceptors(FileInterceptor('file', {
     storage: diskStorage({
     destination: './uploads/files',
     filename: (req, file, cb) => {
        const randomName = Array(32).fill(null).map(() => 
       (Math.round(Math.random() * 16)).toString(16)).join('');
         return cb(null, `${randomName}${extname(file.originalname)}`);
         },
       }),
      }))
     create(
     @Body() createCertificateDto: CreateCertificateDto, 
     @UploadedFile() file): Promise<Certificate> {
        return this.certificatesService.create(createCertificateDto, file);
    }

将实体更新为:

 @Column({ type: 'varchar', length: 300, nullable: true })
   attachFile: string;

或者您坚持将文件名字符串转换为 ByteArrayUint8Array

这是服务:

async create(createCertificateDto: CreateCertificateDto, file): Promise<any> {
console.log(file);
console.log(createCertificateDto);
 try{
     const cert = new Certificate();
        cert.statusOfReport = createCertificateDto?.statusOfReport;
        cert.sponser = createCertificateDto?.sponser;
        cert.address = createCertificateDto?.address;
        cert.address2 = createCertificateDto?.address2;
        cert.zipCOde = createCertificateDto?.zipCOde;
        cert.city = createCertificateDto?.city;
        cert.protoColNo = createCertificateDto?.protoColNo;
        cert.molecules = createCertificateDto?.molecules;
        cert.unAuthMolecule = createCertificateDto?.unAuthMolecule;
        cert.phaseOfTrial = createCertificateDto?.phaseOfTrial;
        cert.noOfSubjects = createCertificateDto?.noOfSubjects;
        cert.startDate = createCertificateDto?.startDate;
        cert.endDate = createCertificateDto?.endDate;
        cert.personInCharge = createCertificateDto?.personInCharge;
        cert.country = createCertificateDto?.country;
        cert.comments = createCertificateDto?.comments;
        cert.attachFile = (file) ? file.filename : '';
        cert.statusOfReport = createCertificateDto?.statusOfReport;
        await cert.save();
        return {success: true, cert};
    } catch (e) {
        return {success: false, message: e.message};
    }
 }

希望对您有所帮助。执行上有什么问题可以联系我