我的后端使用 NestJS、Node 和 Express,前端使用 Angular。我有一个步进器,用户可以在其中逐步浏览并输入有关他们自己的信息以及个人资料照片和他们想要发布的任何艺术照片(这是一个草稿)。我使用以下代码将文件发送到后端:
<h2>Upload Some Photos</h2>
<label for="singleFile">Upload file</label>
<input id="singleFile" type="file" [fileUploadInputFor]= "fileUploadQueue"/>
<br>
<mat-file-upload-queue #fileUploadQueue
[fileAlias]="'file'"
[httpUrl]="'http://localhost:3000/profile/artPhotos'">
<mat-file-upload [file]="file" [id]="i" *ngFor="let file of fileUploadQueue.files; let i = index"></mat-file-upload>
</mat-file-upload-queue>
Run Code Online (Sandbox Code Playgroud)
前端将照片作为文件数组发送;我试图改变它,使它只发送一个文件,但无法让它工作。我不太关注这一点,因为用户可能需要上传多个文件,所以无论如何我都想弄清楚。在后端,我使用 multer、multer-s3 和 AWS-SDK 来帮助上传文件,但它不起作用。这是控制器代码:
@Post('/artPhotos')
@UseInterceptors(FilesInterceptor('file'))
async uploadArtPhotos(@Req() req, @Res() res): Promise<void> {
req.file = req.files[0];
delete req.files;
// tslint:disable-next-line:no-console
console.log(req);
await this._profileService.fileupload(req, res);
}
Run Code Online (Sandbox Code Playgroud)
这是 ProfileService:
import { Profile } from './profile.entity';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import …Run Code Online (Sandbox Code Playgroud)