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

2023-12-06

任何人都可以帮忙解决这个问题吗?

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

enter image description here

我已经采取了以下实体类。

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等等现有的答案没有帮助。


在你的控制器中:

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;

或者你坚持将文件名字符串转换为ByteArray i.e Uint8Array

这是服务:

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

我希望你觉得这有帮助。如果您对实施有任何疑问,可以联系我。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

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

  • 将 Node.js(用于实时通知)添加到现有 PHP 应用程序

    我有一个现有的 PHP 应用程序 我需要向其中添加实时通知 为了实现这一点 我安装了node js 打算添加socket io以实现所有实时功能 然而 尽管在过去的三个小时里研究并试图弄清楚如何将两者结合起来 但我发现自己并没有更接近于获得
  • Mongoose 查找 array.length 大于 0 的所有文档并对数据进行排序

    我正在使用 mongoose 对 MongoDB 执行 CRUD 操作 这就是我的架构的样子 var EmployeeSchema new Schema name String description type String default
  • 如何将 node_modules 包含在单独的 browserify 供应商包中

    我正在尝试将 AngularJS 应用程序转换为使用 browserify 我已经使用 napa 在 node modules 中安装了所有的 Bower 包 现在我想将它们浏览到一个单独的供应商包中 并将它们声明为 外部 依赖项 我想给他
  • keystonejs 模型中的动态类型选择

    我想在 adminUI 中使用一个组合框 其中包含来自 Web 服务的字段 我正在考虑使用预 查找 挂钩获取数据 然后覆盖模式中 受众 属性的选项属性 Schema Compliance add title type Types Text
  • 如何记录数据库代码以查看数据库对象之间的依赖关系? [关闭]

    Closed 这个问题需要多问focused help closed questions 目前不接受答案 我想为我的宠物项目编写文档 我的 PostgreSQL 数据库中有 30 个表 近 50 个视图和大约 30 个函数 存储过程 我想看
  • 模拟节点外部模块默认使用 jest 的链式方法

    在我们的节点 CLI 中 我们有一个简单的方法 use strict const ora require ora module exports function startSpinner textOnStart color spinnerT
  • Socket.io 与服务器离线连接

    如何检测服务器是否离线或由于其他原因无法连接 我的代码看起来像这样 this socket io connect connectionInfo reconnect false 它不会抛出任何错误 因此 try catch 子句不起作用 Us
  • NodeJS:将 JSON 保存到 MongoDB

    我正在尝试从 API 获取 JSON 并将其存储到 MongoDB 数据库中 显然 这是行不通的 我的应用程序似乎停留在我尝试将数据保存到数据库的位置 请告知该怎么做 这是我的代码 var express require express v
  • REST - 复杂的应用程序

    我正在努力将 RESTful 原则应用到我正在开发的新 Web 应用程序中 特别是 为了实现 RESTful 每个 HTTP 请求本身都应该携带足够的信息 以便其接收者对其进行处理 从而与 HTTP 的无状态性质完全一致 该应用程序允许用户
  • Nodejs Express 隐式中间件应用于所有路由?

    我想知道 Express 是否允许我创建一个默认调用的路由中间件 而无需我将其明确放置在 app get arg 列表中 NodeJS 新手 var data title blah So I want to include this in
  • Laravel Homestead 中 npm 安装错误有解决方案吗?

    Windows 10 家园 虚拟盒6 0 8 流浪者2 2 5 节点 v12 5 0 npm v6 10 1 我想做的就是在新安装的 Laravel 应用程序中执行 npm install 命令 但我不断收到错误 经过两天的谷歌搜索并尝试了
  • Sails.js - 如何更新嵌套模型

    attributes username type email validated by the ORM required true password type string required true profile firstname s
  • 随机数据库与 AWS 中的 Django 和 Postgresql 断开连接

    我试图找出 Django 和数据库连接错误问题的根源 此时 我正在调试提示 因为我认为症状太不具体 一些背景 我一直在使用这个堆栈 在 AWS 中部署了很多年 没有出现任何问题 Ubuntu 在本例中为 20 04 LTS Nginx Uw
  • postgresql中数组的区别

    我有两个数组 1 2 3 4 7 6 and 2 3 7 在 PostgreSQL 中可能有共同的元素 我想做的是从第一个数组中排除第二个数组中存在的所有元素 到目前为止我已经取得了以下成果 SELECT array SELECT unne
  • postgresql 中的锁定表

    我有一个名为 games 其中包含一个名为 title 该列是唯一的 数据库中使用PostgreSQL 我有一个用户输入表单 允许他插入新的 game in games 桌子 插入新游戏的功能会检查之前输入的游戏是否存在 game 与相同的
  • npm 错误! cb.apply 不是函数

    我收到这个错误 npm ERR cb apply is not a function 在Linux中做的时候npm install虽然我的npm版本是6 9 0 我的节点版本是v12 18 3 如何解决这个问题 如果您共享的是 Window
  • PostgreSQL 仅当列存在时才重命名该列

    我在中找不到PostgreSQL 文档 https www postgresql org docs 12 sql altertable html如果有办法运行 ALTER TABLE tablename RENAME COLUMN IF E
  • 全局未在 ../node_modules/socket.io-parser/is-buffer.js 中定义

    预先感谢您帮助我 我正在尝试在我的一个角度组件中连接套接字 但在浏览器的控制台中它会抛出一个错误 指出 Global 未在 Object node modules socket io parser is buffer js 中定义 这是我的
  • 如何上传文件 - sails.js

    我可以下载图像和 pdf 但无法下载文档文件 doc pptx odt 下载文档 doc pptx odt 时 仅将其下载为 ZIP XML 文件 我可以做什么 我在用着 填写上传文件文档 https github com balderda
  • 了解使用 Windows 本机 WPF 客户端进行 ADFS 登录

    我已经阅读了大量有关 ADFS 与 NodeJS Angular 或其他前端 Web 框架集成以及一般流程如何工作的文献 并通过 Auth0 Angular 起始代码构建了概念证明 但我不明白如何这可以与本机 WPF Windows 应用程

随机推荐