Plumier added support for file upload using multi part form data, by default this feature is not enabled, you can enable the
multipart feature from the
WebApiFacility like below:
Plumier provided a parameter binding to automatically bound the
FormFile into action parameter. Its possible to bound it using name binding or decorator binding using
For example you have a simple form with a file input named
file like below
Note that the action of the form pointed to
/animal/save. The name of the file input is
file. Using that information you can create the controller like below
Controller above will be generated into route
POST /animal/save, it uses name binding to bind the file input into the action parameter. Note that the name of the action parameter
file is the same as the file input name in the html form
Its also possible to bind the file using decorator like below
Note that when using decorator parameter binding, the parameter name is not necessary to be the same as the file input.
For file input with
multiple property enabled, you need to specify the type of the action parameter as array of
FormFile like below
Above code showing file input has the
multiple property enabled, controller can be changed into below
Note that you need to provided type information using
@tsp.type([FormFile]) to prevent route analysis warning.
FormFile is a specialized class contains information about uploaded file input. Its has properties like below:
size: Size of the file (bytes)
path: Temporary path of the uploaded file
name: File name provided by client
type: Mime type of the file
mtime: The file timestamp
Plumier provided file validation using decorator for convenient, it can be applied on the
FormFile parameter like other validator.
Above code will restrict the size of uploaded file only 3MB allowed. The
@val.file() validator receive string/number as default to limit the file size. Internally the bytes string notation uses bytes to parse the string.
File validation also receive object parameter for more options.
Above code will restrict only file with mime type
Plumier provided a short hand for image validation, to restrict the uploaded file only with mime type
Above code will restrict uploaded file only of type image with maximum file size 3MB.