AdonisJs. File storage. Upload and download files from server

Artem Diashkin
May 9, 2020 · 4 min read

How to upload/download files to/from your server? Let’s find out…

1. Install

adonis install @adonisjs/drive

2. Changes after running script

1. @adonisjs/drive package will be added to package.json file:

"@adonisjs/drive": "^1.0.4",

2. New created drive.js file in /config folder

…/[ROOT]/config/drive.js

3. Registration

const providers = [
'@adonisjs/drive/providers/DriveProvider'
]

4. Creating new Route with Controller

To upload and download files we will need Routes and FileController.

At first we will start from upload functionality:

4.1. Creating new route

const Route = use('Route');

Route.group(
()
=> {
Route.post('upload', 'FileController.upload');
}
)
.prefix('api/v1/');

Here, we are basically saying :

“Application, use FileController’s “upload” method for handling user’s POST requests to the endpoint “/api/v1/upload”.

4.2. Creating new File Controller

adonis make:controller File --type http

After running this command you will find app/Controllers/Http/FileController.js file created in your project:

Let’s fill this new empty FileController.js file with some code:

5. Uploading file to server

  • First, for uploading file to server we need to select “Multipart Form
  • Next, we should specify param name that should be the same as in file method:
  • Select a file you want to download:

I have selected adonis-js.jpeg file, and what will happen after clicking Send button. Let’s see:

Result in out application:

With overwrite:true flag your file will be rewritten each time you will click Send. If you will change it to false :

6. Validations

const validationOptions = {
types: ['image'],
size: '1mb',
};

What will happen if we will change size property to 1kb. Let’s find out:

And what will happen if we will change types to video:

You can specify your own data too:

const validationOptions = {
types: ['image'],
size: '1mb',
extnames: ['png'],
};

7. Creating separate validator

7.1 Create new Validator

adonis make:validator FileUpload

After running this command you will see new created Validators/FileUpload.js file

We will need to make few changes to Validators/FileUpload.js file:

7.2 Remove old implementation from the file controller

const imageFile = request.file('custom-param-name');

Result will be exactly the same!

8. Download file from the server

Next, we need to update our FileController.js file by adding download method:

Let’s see the results:

File not exist

File successfully downloaded

In browser:

9. Uploading/Downloading video file

  • create new methods in FileController for video files
  • add new Routes
Route
.post('uploadVideo',
'FileController.uploadVideo');
Route
.get('downloadVideo/:fileName',
'FileController.download');

And that’s it, you are good to go.

Let’s check how it would work with uploading/downloading video after deploying project to Heroku server. For demo purposes we will use video from HERE.

1. Upload video file to Heroku deployed application:

2. Check how it works

LITSLINK

LITSLINK’s team is sharing their experience

Sign up for LITSLINK Newsletter

By LITSLINK

It's a test newsletter! Take a look.

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Artem Diashkin

Written by

Java, Spring, Node.js, AdonisJs, React.js and Flutter developer

LITSLINK

LITSLINK

Hands-on up to date experience sharing of the most skilled & talented software developers right here, right now. Subscribe to learn & understand more about the Software World.

Artem Diashkin

Written by

Java, Spring, Node.js, AdonisJs, React.js and Flutter developer

LITSLINK

LITSLINK

Hands-on up to date experience sharing of the most skilled & talented software developers right here, right now. Subscribe to learn & understand more about the Software World.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store