In this example, the form has an ID of "dropzone" and the action attribute is set to the route where you want to handle the file upload.
Step 3: Initialize Dropzone JS in your JavaScript file or in your main blade file:
<scripttype="text/javascript">
// Note that the name "dropzone" is the camelized id of the form.
Dropzone.options.dropzone = {
maxFilesize:12, // MB
maxFiles:10,
resizeQuality:1.0,
acceptedFiles:".jpeg,.jpg,.png,.webp",
addRemoveLinks:false,
timeout:60000,
dictDefaultMessage:"Drop your files here or click to upload",
dictFallbackMessage:"Your browser doesn't support drag and drop file uploads.",
dictFileTooBig:"File is too big ({{filesize}}MB). Max filesize: {{maxFilesize}}MB.",
dictInvalidFileType:"Invalid file type. Only JPG, JPEG, PNG and GIF files are allowed.",
dictMaxFilesExceeded:"You can only upload up to {{maxFiles}} files.",
success:function (file, response) {
console.log(response.success);
console.log(response);
},
error:function (file, response) {
alert(response);
this.removeFile(file);
console.log(response);
returnfalse;
}
};
</script>
This code initializes Dropzone JS with some options such as max filesize, accepted file types, and the text to display for the default message. The init function sets up event listeners for success and error messages when files are uploaded.
Step 4: Create a route in your Laravel application that handles the file upload in the following path: routes/web.php
This code checks if the uploaded file is valid, moves the file to the "uploads/gallery" directory and returns a JSON response with a success or error message.
In this above laravel controller method, we are also saving the data of images uploaded as Gallery. So, lets create the Model & Migration.
Step 6: Create a migration as follows:
lets run this laravel artisan command to create migration file for gallery
php artisan make:migration create_galleries_table
once, successfully created, lets add the column names as given below in following path: database/migration/create_galleries_table.php
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
classCreateGalleriesTableextendsMigration
{
/**
* Run the migrations.
*
* @returnvoid
*/
publicfunctionup()
{
Schema::create('galleries', function (Blueprint$table) {
$table->id();
$table->string('image');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @returnvoid
*/
publicfunctiondown()
{
Schema::dropIfExists('galleries');
}
}
Step 4: Create a model named Gallery as follows:
lets run this laravel artisan command to create Model file for gallery
php artisan make:model Gallery
once, successfully created, lets update the fields as given:
<?php
namespaceApp\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Factories\HasFactory;
classGalleryextendsModel
{
useHasFactory;
protected$table = 'galleries';
protected$fillable = [
'image'
];
}
That's it! You should now have a user-friendly drag and drop file upload form in your Laravel application using Dropzone JS.