Routing-controllers: @UploadedFile example usage

Created on 20 Feb 2017  路  4Comments  路  Source: typestack/routing-controllers

I was wondering if you could provide an example on how to use the @UploadedFile decorator. I'm unsure how I should be configuring multer before invoking the decorater. Appreciate any tips.

Thanks,
Alex

question

Most helpful comment

Could you also provide the code of the client how to send a file?

All 4 comments

This is how I use it:

 @Post("/:id/photos")
    async uploadPhoto( @UploadedFile("file") file: any, @BodyParam("photo") photo: Photograph) {
//photo is a Form parameter    
  if (photo) {
        this.createDirectoryForCollection(photo.photoCollectionId);
        photo = await this.photoRepository.persist(photo);
        photo.preview = this.createPreviewUrl(photo.photoCollectionId, photo.id);
        photo.source = this.createFullsizeUrl(photo.photoCollectionId, photo.id);
//This is the file      
  let buffer: Buffer = file.buffer;

        await  this.moveImage(buffer, photo.source);

        console.log(photo);
        return this.photoRepository.persist(photo);
      }


    }

Hope this helps

@AleskiWeb there is uploadOptions in decorator, you can use it for example this way:

```ts
@UploadedFiles("files", { uploadOptions: FILE_UPLOAD_OPTIONS }) files: any[]

```ts
export const FILE_UPLOAD_OPTIONS = {
    storage: multer.diskStorage({
        destination: (req: any, file: any, cb: any) => { ...
        },
        filename: (req: any, file: any, cb: any) => { ...
        }
    }),
    fileFilter: (req: any, file: any, cb: any) => { ...
    },
    limits: {
        fieldNameSize: 255,
        fileSize: 1024 * 1024 * 2
    }
}

Could you also provide the code of the client how to send a file?

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

xujif picture xujif  路  6Comments

circy picture circy  路  3Comments

mlarsson picture mlarsson  路  5Comments

GBeushausen picture GBeushausen  路  5Comments

ghost picture ghost  路  5Comments