如何在 Loopback4 中发送一定数量的图像?

How can I work with sending a certain number of images in Loopback4?

提问人:Felipe Renovato 提问时间:11/10/2023 更新时间:11/10/2023 访问量:10

问:

我需要我的 API 来接收三个文件,一个文件对我来说很容易,因为我不必使用 multipart/form-data,我需要将这三个文件更新到它对应的地方(使用相应的存储库)。

我的 API 有什么问题?

  @post('uploads/rentalDataFiles/{id}')
  @response(200, {
    description: 'Save the VIN, Voucher and Registration Card images of the rental data.',
    content: {
      'multipart/form-data': {
        schema: {
          type: 'object',
          properties: {
            vinFile: {
              type: 'string',
            },
            voucherFile: {
              type: 'string',
            },
            registrationCardFile: {
              type: 'string',
            },
          },
        },
      },
    },
  })
  async fileUpload(
    @requestBody({
      description: 'The input of file upload function',
      required: true,
      content: {
        'multipart/form-data': {
          // Skip body parsing
          'x-parser': 'stream',
          schema: {
            type: 'object',
            properties: {
              vinFile: {
                type: 'string',
                format: 'binary',
              },
              voucherFile: {
                type: 'string',
                format: 'binary',
              },
              registrationCardFile: {
                type: 'string',
                format: 'binary',
              },
            },
          },
        },
      },
    })
    request: Request,
    @inject(RestBindings.Http.RESPONSE)
    responseEndpoint: Response,
    @param.path.string('id')
    rentalDataId: number
  ): Promise<object | false> {
    const RENTAL_DATA_PATH_ABSOLUTE = path.join(__dirname, UploadFilesKeys.RENTAL_DATA_IMAGE_PATH);

    const files = ['vinFile', 'voucherFile', 'registrationCardFile'];
    const updateObject = {};

    for (const file of files) {
      const optionFile: FileUploadOptions = {
        storePath: RENTAL_DATA_PATH_ABSOLUTE,
        fieldname: file,
        request,
        responseEndpoint,
        acceptedExt: UploadFilesKeys.IMAGE_ACCEPTED_EXT,
      };

      const responseRentalData = await this.serviceUpload.storeFilesToPath(optionFile);
      if (responseRentalData) {
        const filename = responseEndpoint.req.file?.filename;
        let updateObject: { [key: string]: string } = {};
        if (filename) {
          let fullPath = path.join(RENTAL_DATA_PATH_ABSOLUTE, filename);
          updateObject[file] = fullPath;
        }
      }
    }

    await this.rentalDataRepository.updateById(rentalDataId, updateObject);
    return updateObject;
  }

这是我使用的服务方法:

public storeFilesToPath(options: FileUploadOptions): Promise<object> {
    return new Promise<object>((resolve, reject) => {
      const storage = this.getMulterStorageConfig(options.storePath);

      const upload = multer({
        storage: storage,
        fileFilter: function (req, file, callback) {
          const ext = path.extname(file.originalname).toUpperCase();
          if (options.acceptedExt.includes(ext)) {
            return callback(null, true);
          }
          return callback(new HttpErrors[400]('This format file is not supported.'));
        },
      }).fields([{ name: 'vinFile' }, { name: 'voucherFile' }, { name: 'registrationCardFile' }]);

      upload(options.request, options.responseEndpoint, (err: string) => {
        if (err) {
          reject(err);
        }
        resolve(options.responseEndpoint);
      });
    });
  }

我有下一个错误: 请求 POST /uploads/rentalDataFiles/5 失败,状态代码未定义。错误:Multipart._final处意外结束

JavaScript 节点 .js 文件 环回

评论


答: 暂无答案