Files can be uploaded and downloaded using FoalTS file system. It allows you to use different types of file storage such as the local file system or cloud storage.
First install the package.
Then specify in your configuration the file storage to be used and its settings. In this example, we will use the local file system with the
uploaded directory (you must create it at the root of your project).
Files can be uploaded using
multipart/form-data requests. The
@ValidateMultipartFormDataBody hook parses the request body, validates the submitted fields and files and save them in streaming to your local or Cloud storage. It also provides the ability to create file buffers if you wish.
enctypeof your requests must be of type
multipart/form-data. If needed, you can use a FormData object for this.
The names of the file fields must be provided in the
files parameter of the hook. Uploaded files which are not listed here are simply ignored.
required parameter tells the hook if it should return a
400 - BAD REQUEST error if no file has been uploaded for the given field. In this case, the controller method is not executed.
When the upload is successful, the request body object is set with the buffer files.
|Value of ||Files uploaded||Value in the request object|
|At least one||A buffer|
|None||An empty array|
|At least one||An array of buffers|
Instead of using buffers, you can also choose to save directly the file to your local or Cloud storage. To do this, you need to add the name of the target directory in your hook options. The value returned in the
ctx is an object containing the relative path of the file.
With the previous configuration, this path is relative to the
uploadeddirectory. Note that must create the
uploaded/images/profilesdirectories before you can upload a file.
When uploading files, the browser sends additional metadata. This can be accessed in the controller method.
|Encoding type of the file|
|Name of the file on the user's computer|
|Mime type of the file|
|Path where the file has been saved. If the |
|Buffer containing the entire file. If the |
Multipart requests can also contain non-binary fields such as a string. These fields are validated and parsed by the hook.
Optional settings can be provided in the configuration to limit the size or number of files uploaded.
|fileSizeLimit||number||The maximum file size (in bytes).|
|fileNumberLimit||number||The maximum number of files (useful for |
Files can be downloaded using the method
createHttpResponse of the
Disk service. The returned object is optimized for downloading a (large) file in streaming.
|forceDownload||boolean||It indicates whether the response should include the |
|filename||string||Default name proposed by the browser when saving the file. If it is not specified, FoalTS extracts the name from the path (|
This example shows how to attach a profile picture to a user and how to retrieve and update it.
Create a new directory
uploaded/images/profiles at the root of your project.
If necessary, this directory can be modified using the configuration key
In case you need to add a virtual prefix path to your static files, you can do so with the
staticPathPrefix configuration key.
| Static file | URL path with no prefix | URL path with the prefix
| --- | --- | --- |
| index.html |
| styles.css |
| app.js |