Skip to content
This repository has been archived by the owner on Sep 27, 2021. It is now read-only.

Update 03-File-Uploads.adoc #366

Open
wants to merge 1 commit into
base: 4.1
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions 06-Digging-Deeper/03-File-Uploads.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,14 @@ Route.post('upload', async ({ request }) => {
----

[ol-spaced]
1. The `request.file` method accepts two arguments (a *field name* and *a validation object* to apply to the uploaded file), and returns a link:https://github.com/adonisjs/adonis-bodyparser/blob/develop/src/Multipart/File.js[File, window="_blank"] instance.
1. The `request.file` method accepts two arguments (a *field name* and *a validation object* to apply to the uploaded file), and returns a link:https://github.com/adonisjs/adonis-bodyparser/blob/develop/src/Multipart/File.ts[File, window="_blank"] instance.
2. Next, we call the `profilePic.move` method which attempts to move the file to the defined directory (in this case, called with options to save the file with a new *name* and to *overwrite* the file if needed).
3. Finally, we check whether the move operation was successful by calling the `profilePic.moved()` method (returning errors if any were found).

== Multiple File Uploads
AdonisJs makes uploading multiple files as simple as uploading a single file.

When multiple files are uploaded together, `request.file` returns a link:https://github.com/adonisjs/adonis-bodyparser/blob/develop/src/Multipart/FileJar.js[FileJar, window="_blank"] instance instead of a link:https://github.com/adonisjs/adonis-bodyparser/blob/develop/src/Multipart/File.js[File, window="_blank"] instance:
When multiple files are uploaded together, `request.file` returns a link:https://github.com/adonisjs/adonis-bodyparser/blob/develop/src/Multipart/FileJar.js[FileJar, window="_blank"] instance instead of a link:https://github.com/adonisjs/adonis-bodyparser/blob/develop/src/Multipart/File.ts[File, window="_blank"] instance:

[source, edge]
----
Expand Down Expand Up @@ -156,7 +156,7 @@ await avatar.move()

== Error Types

When upload validation fails, the link:https://github.com/adonisjs/adonis-bodyparser/blob/develop/src/Multipart/File.js[File, window="_blank"] `error` method returns an object containing the failed `fieldName`, original `clientName`, an error `message`, and the rule `type` that triggered the error.
When upload validation fails, the link:https://github.com/adonisjs/adonis-bodyparser/blob/develop/src/Multipart/File.ts[File, window="_blank"] `error` method returns an object containing the failed `fieldName`, original `clientName`, an error `message`, and the rule `type` that triggered the error.

NOTE: The link:https://github.com/adonisjs/adonis-bodyparser/blob/develop/src/Multipart/FileJar.js[FileJar, window="_blank"] `errors` method returns an *array* of errors.

Expand Down Expand Up @@ -187,7 +187,7 @@ Are few example error objects are listed below.
----

== File Properties
The following file properties can be accessed on the link:https://github.com/adonisjs/adonis-bodyparser/blob/develop/src/Multipart/File.js[File, window="_blank"] instance:
The following file properties can be accessed on the link:https://github.com/adonisjs/adonis-bodyparser/blob/develop/src/Multipart/File.ts[File, window="_blank"] instance:

[role="resource-table", options="header", cols="45, 20, 20, 15"]
|====
Expand Down Expand Up @@ -224,7 +224,7 @@ class StoreUser {
module.exports = StoreUser
----

1. The `file` rule ensures the `avatar` field is a valid link:https://github.com/adonisjs/adonis-bodyparser/blob/develop/src/Multipart/File.js[File].
1. The `file` rule ensures the `avatar` field is a valid link:https://github.com/adonisjs/adonis-bodyparser/blob/develop/src/Multipart/File.ts[File].
2. The `file_ext` rule defines the `extnames` allowed for the file.
3. The `file_size` rule defines the maximum `size` for the file.
4. The `file_types` rule defines the `types` allowed for the file.
Expand Down