RequestBody.file 没有收到任何文件

RequestBody.file did not receive any file

所以我在过去的 2-3 个月里一直在使用 LoopbackJS,目前我正在构建一个文件上传器(专门用于个人资料头像图像)。但是每次我将图像文件发送到服务器从未接收到的端点并将其标记为“未定义”时,这会中断进程。任何人都可以帮助我确定我的代码有什么问题吗?

这是我目前的代码。

文件-upload.controller.ts

@post('/files', {
    responses: {
      '204': {
        description: 'Uploaded',
      },
    },
  })
  async fileUpload(
    @requestBody.file()
    request: Request,
    @inject(RestBindings.Http.RESPONSE) response: Response,
  ): Promise<string> {
    console.log(request.file.filename);

    const storage = multer.diskStorage({
      destination: './public/uploads',
      filename: function (req, file, cb) {
        cb(null, 'image-' + Date.now() + path.extname(file.filename));
      },
    });

    const upload = multer({storage: storage}).single('image');

    upload(request, response, (err: unknown) => {
      if (err) console.log(err);
      else {
        console.log(request.file.filename);
      }
    });

    return 'Yes';
  }

对于任何人提出的问题,我使用 console.log(request.file.filename) 来调试请求是否实际带来了文件。

Here's my request using Postman

错误日志

Unhandled error in POST /files: 500 TypeError: Cannot read property 'filename' of undefined
    at FileUploadController.fileUpload (D:\Project\API\carena-api\src\controllers\file-upload.controller.ts:42:30)
    at invokeTargetMethod (D:\Project\API\carena-api\node_modules\@loopback\context\src\invocation.ts:255:47)
    at D:\Project\API\carena-api\node_modules\@loopback\context\src\invocation.ts:232:12
    at Object.transformValueOrPromise (D:\Project\API\carena-api\node_modules\@loopback\context\src\value-promise.ts:298:12)
    at invokeTargetMethodWithInjection (D:\Project\API\carena-api\node_modules\@loopback\context\src\invocation.ts:227:10)
    at InterceptedInvocationContext.invokeTargetMethod (D:\Project\API\carena-api\node_modules\@loopback\context\src\invocation.ts:118:14)       
    at targetMethodInvoker (D:\Project\API\carena-api\node_modules\@loopback\context\src\interceptor.ts:349:23)
    at D:\Project\API\carena-api\node_modules\@loopback\context\src\interceptor-chain.ts:218:14
    at Object.transformValueOrPromise (D:\Project\API\carena-api\node_modules\@loopback\context\src\value-promise.ts:298:12)
    at GenericInterceptorChain.invokeNextInterceptor (D:\Project\API\carena-api\node_modules\@loopback\context\src\interceptor-chain.ts:213:12)  
    at GenericInterceptorChain.next (D:\Project\API\carena-api\node_modules\@loopback\context\src\interceptor-chain.ts:201:17)
    at GenericInterceptorChain.invokeInterceptors (D:\Project\API\carena-api\node_modules\@loopback\context\src\interceptor-chain.ts:178:17)     
    at Object.invokeInterceptors (D:\Project\API\carena-api\node_modules\@loopback\context\src\interceptor-chain.ts:250:16)
    at D:\Project\API\carena-api\node_modules\@loopback\context\src\interceptor.ts:351:14
    at tryCatchFinally (D:\Project\API\carena-api\node_modules\@loopback\context\src\value-promise.ts:222:14)
    at Object.tryWithFinally (D:\Project\API\carena-api\node_modules\@loopback\context\src\value-promise.ts:197:10)

谢谢。

代码顶部的控制台日志导致错误。您应该使用行号验证错误。 any IDE 中的调试模式有助于找出问题所在。 这是更正后的代码

@post('/files')
  async fileUpload(
    @requestBody({
      content: {
        'multipart/form-data': {
          'x-parser': 'stream',
          schema: {type: 'object'},
        },
      },
    })
    request: Request,
    @inject(RestBindings.Http.RESPONSE) response: Response,
  ): Promise<string> {
    const storage = multer.memoryStorage();
    const upload = multer({storage});
    const fileArr = await new Promise<any[]>((resolve, reject) => {
      upload.any()(<any>request, <any>response, err => {
        if (err) reject(err);
        else {
          resolve(<any[]>request.files);
        }
      });
    });
    console.log(fileArr[0].originalname);

    ....
    ....
    ....
  
    return 'Yes';
  }

使用fileArr[0].buffer将文件写入磁盘