Upload multiple files with Dropzone.js and Laravel MediaLibrary

Tram Ho

In the previous post, I wrote an article about uploading a translated file from Laravel Daily , but it was too old because it was written in 2017. In this article, I will write a new way to upload files with Dropzone.js and Laravel MediaLibrary. This article I also translated on Daily Laravel , so I would like to keep the examples of the author. My original link below the article.

Uploading files is one of the most popular features of the web today. And there are quite a few libraries that can help us with this function. Here we use Dropzone for the client side and Spatie MediaLibrary for the server side.

First, let’s see the demo image of Dropzone.

Step 1. Install MediaLibrary

We install MediaLibrary with this command:

Next, we publish the migration file:

And run migration:

Up to now, we have the media table in our database.

This table uses polymorphic relationships .

Step 2. Work with Dropzone.js

In view file using this blade, we need to use JavaScript code of Dropzone .

In the above code, we see:

  • Forms will be sent via route projects.store – we’ll talk about this later.
  • Dropzone it only includes 1 Div tag containing id and class of dropzone.

Now, use JavaScript at the end of the code:

  • route (‘admin.projects.storeMedia’) – it will be the URL to process the selected file before the form is submitted.
  • $ (‘form’). append () – after the above URL has uploaded the file, we will get the file name and assign it to the value of the hidden input.
  • There is also a function to remove files , after deleting, the hidden inputs are also deleted.
  • A few other details such as CSRF-token or upload file limit , please find out in the code above.

Note: this JavaScript code will still be ok with the edit function, not just with create .

Now we need to add some css and js from cdn:

Now we can upload a file to Dropzone, but it can’t be submitted yet because we need to do a few more.

Step 3. Upload the file

First, in the routes / web.php file, we will have this line, if not, add it:

Next, open the app / Http / Controllers / ProjectsController.php file:

Nothing special here, just use the normal PHP – Laravel functions to upload the file, create its unique file name and return along with the original name, like the JSON result, so the Dropzone script can continue.

Note: storage / tmp / uploads is only a temporary path, you can choose another path.

Now we have the files uploaded to the server, but there is no data in the database yet, because the form hasn’t been submitted yet, so it looks like this:

Step 4. Submit the form

After pressing the Submit button, we call the [email protected] () function, which is the resource function available in Laravel:

Step 5. Edit / Update form

With the Edit function, the client’s Dropzone has not changed, only the server side needs to look a bit:

Understand simple code above is: first we delete the unused files, then assign only those files not in the list.

Other case

Assuming the user has chosen to upload their files but not the submit button, those files will still be saved on the server. This will depend on your processing: save to the user’s memory for later use or use a cron job to clean up but unused files, for example.

Reference: This article I translated from https://laraveldaily.com/multiple-file-upload-with-dropzone-js-and-laravel-medialibrary-package/ .

Thank you for taking the time to look over.

Share the news now

Source : Viblo