In this article, we can try to Define the AdonisJS File uploads Module, A file upload module built for AdonisJS, and How to Manage File Uploads and Validate Them in AdonisJS.

File Uploads

AdonisJS provides you with a robust and performant API for dealing with file uploads. You can process and store uploaded files locally. Also, you can stream them directly to the cloud services like S3 or Google cloud storage.

You can check our previous article: AdonisJS: REST API simple CRUD Operation. IF you want then buy a good, reliable, secure web hosting service  from here: click here

Accessing Uploaded Files

In the body-parser middleware registered inside the start/kernel.ts file automatically processes all the files for multipart/form-data requests.

Here, you can access the files using the request.file procedure. And the method accepts the field name and returns an instance of the File class, and null if no file was uploaded into your project.

import Route from '@ioc:Adonis/Core/Route'
import Application from '@ioc:Adonis/Core/Application'

Route.post('posts', async ({ request }) => {
const coverImage = request.file('cover_image')

if (coverImage) {
await coverImage.move(Application.tmpPath('uploads'))
}
})

So, when you accept multiple files from the same input, here you can use the request.files method returns an array of the file instances.

import Route from '@ioc:Adonis/Core/Route'
import Application from '@ioc:Adonis/Core/Application'

Route.post('gallery', async ({ request }) => {
const images = request.files('images')

for (let image of images) {
await image.move(Application.tmpPath('uploads'))
}
})

Validating Files

However, you can also validate the file by specifying the rules for the file extension. The file size and AdonisJS will perform the validation round.

You can purchase your hosting from Cloudsurph.comCloudsurph hosting is a reliable hosting option for business and personal projects. We offer insight and help on system configuration issues and code errors or bugs.
const coverImage = request.file('cover_image', {
size: '2mb',
extnames: ['jpg', 'png', 'gif'],
})
if (!coverImage) {
return
}
if (!coverImage.isValid) {
return coverImage.errors
}
await coverImage.move(Application.tmpPath('uploads'))

Validating Files Using the Validator

Also. You can use the validator to validate the user-uploaded files alongside the rest of the form.

The schema.file method validates the input to be a valid file, along with any custom validation rules provided for the file size and the extension for your application or project.

So, if the file validation fails, then you can access the error message alongside the form errors.  Otherwise, you can access the file instance and move it to the desired file location.

import Route from '@ioc:Adonis/Core/Route'
import { schema } from '@ioc:Adonis/Core/Validator'
import Application from '@ioc:Adonis/Core/Application'

Route.post('posts', async ({ request }) => {
const postSchema = schema.create({
cover_image: schema.file({
size: '2mb',
extnames: ['jpg', 'gif', 'png'],
}),
})
const payload = await request.validate({ schema: postSchema })
await payload.cover_image.move(Application.tmpPath('uploads'))
})

Saving Files

Now you can save user-uploaded files using the moveToDisk method and it will use AdonisJS Drive under the hood to save your files.

const coverImage = request.file('cover_image', {
size: '2mb',
extnames: ['jpg', 'png', 'gif'],
})!
await coverImage.moveToDisk('./')
// Get the name of the saved file; to store it in your database, for example.
const fileName = coverImage.fileName;
If you want to know more about AdonisJS Model relationships then please visit AdonisJs main website.

If you enjoyed reading this article and have more questions please reach out to our support team via live chat or email and we would be glad to help you. we provide server hosting for all types of need and we can even get your server up and running with the service of your choice.