I am attempting to create a multiple file upload with Dropzone.js and Laravel.
Blade view:
<div class="card">
<div class="card-header">
<h3 class="card-title">Dropzone.js <small><em>jQuery File Upload</em> like look</small></h3>
</div>
<div class="card-body">
<div class="row" id="actions">
<div class="col-lg-6">
<div class="btn-group w-100">
<span class="btn btn-success col fileinput-button">
<i class="fas fa-plus"></i>
<span>Aggiungi files</span>
</span>
<button type="submit" class="btn btn-primary col start">
<i class="fas fa-upload"></i>
<span>Inizia upload</span>
</button>
<button type="reset" class="btn btn-warning col cancel">
<i class="fas fa-times-circle"></i>
<span>Azzera</span>
</button>
</div>
</div>
<div class="col-lg-6 d-flex align-items-center">
<div class="fileupload-process w-100">
<div id="total-progress" class="progress progress-striped active" role="progressbar" aria-valuemin="0" aria-valuemax="100" aria-valuenow="0">
<div class="progress-bar progress-bar-success" style="width:0%;" data-dz-uploadprogress></div>
</div>
</div>
</div>
</div>
<div class="table table-striped files" id="previews">
<div id="template" class="row mt-2">
<div class="col-auto">
<span class="preview"><img src="data:," alt="" data-dz-thumbnail /></span>
</div>
<div class="col d-flex align-items-center">
<p class="mb-0">
<span class="lead" data-dz-name></span>
(<span data-dz-size></span>)
</p>
<strong class="error text-danger" data-dz-errormessage></strong>
</div>
<div class="col-4 d-flex align-items-center">
<div class="progress progress-striped active w-100" role="progressbar" aria-valuemin="0" aria-valuemax="100" aria-valuenow="0">
<div class="progress-bar progress-bar-success" style="width:0%;" data-dz-uploadprogress></div>
</div>
</div>
<div class="col-auto d-flex align-items-center">
<div class="btn-group">
<button class="btn btn-primary start">
<i class="fas fa-upload"></i>
<span>Upload</span>
</button>
<button data-dz-remove class="btn btn-warning cancel">
<i class="fas fa-times-circle"></i>
<span>Cancella</span>
</button>
</div>
</div>
</div>
</div>
</div>
</div>
Dropzone.js setup:
// DropzoneJS Demo Code Start
Dropzone.autoDiscover = false;
// Get the template HTML and remove it from the doumenthe template HTML and remove it from the doument
var previewNode = document.querySelector("#template");
previewNode.id = "";
var previewTemplate = previewNode.parentNode.innerHTML;
previewNode.parentNode.removeChild(previewNode);
var myDropzone = new Dropzone(document.body, { // Make the whole body a dropzone
url: "{{ route('back.images.store') }}", // Set the url
headers: {
'X-CSRF-TOKEN': "{{ csrf_token() }}"
},
thumbnailWidth: 80,
thumbnailHeight: 80,
parallelUploads: 20,
previewTemplate: previewTemplate,
acceptedFiles: "image/*",
autoQueue: false, // Make sure the files aren't queued until manually added
previewsContainer: "#previews", // Define the container to display the previews
clickable: ".fileinput-button" // Define the element that should be used as click trigger to select files.
});
myDropzone.on("addedfile", function(file) {
// Hookup the start button
file.previewElement.querySelector(".start").onclick = function() { myDropzone.enqueueFile(file); };
});
// Update the total progress bar
myDropzone.on("totaluploadprogress", function(progress) {
document.querySelector("#total-progress .progress-bar").style.width = progress + "%";
});
myDropzone.on("sending", function(file) {
// Show the total progress bar when upload starts
document.querySelector("#total-progress").style.opacity = "1";
// And disable the start button
file.previewElement.querySelector(".start").setAttribute("disabled", "disabled");
});
// Hide the total progress bar when nothing's uploading anymore
myDropzone.on("queuecomplete", function(progress) {
document.querySelector("#total-progress").style.opacity = "0";
Toast.fire({
icon: 'success',
title: 'Upload terminato.',
didDestroy: (toast) => {
window.location.reload();
}
});
});
// Setup the buttons for all transfers
// The "add files" button doesn't need to be setup because the config
// `clickable` has already been specified.
document.querySelector("#actions .start").onclick = function() {
myDropzone.enqueueFiles(myDropzone.getFilesWithStatus(Dropzone.ADDED));
};
document.querySelector("#actions .cancel").onclick = function() {
myDropzone.removeAllFiles(true);
};
// DropzoneJS Demo Code End
Store ethod in Laravel resource:
public function store(Request $request)
{
$image = $request->file('file');
$imgName = \Carbon\Carbon::now()->timestamp.'.'.$image->getClientOriginalExtension();
$image->move(public_path('/assets/front/images/galleries/'),$imgName);
$imgUpload = new Image;
$imgUpload->img = $imgName;
$imgUpload->save();
return response()->json(["status" => "success"]);
}
When I try to upload multiple files together, the operation seems to finish correctly but only the last image is loaded in the upload folder and as many images as there are uploaded are saved in the database but with the name of the highest of the series (the one moved to the upload folder ). Do you have any suggestions? If I try a dd ($ request-> file ('file')) in the console I find an answer with the file data for each file I try to upload.