Dropzone - max files not working - javascript

I have tried setting up the limit of uploading files to only one. I have tried all the suggestions from the previous questions here but nothing worked for me. Each time I was able to upload multiple files and as many as like.
This was one of my attempts:
var token = "{{ csrf_token() }}";
Dropzone.autoDiscover = false;
var myDropzone = new Dropzone("div#dropzoneFileUpload", {
url: "/admin/upload",
params: {
_token: token
}
});
Dropzone.options.myAwesomeDropzone = {
paramName: "file", // The name that will be used to transfer the file
maxFilesize: 2, // MB
addRemoveLinks: true,
maxFiles: 1,
init: function() {
this.on("maxfilesexceeded", function() {
if (this.files[1]!=null){
this.removeFile(this.files[0]);
}
});
},
accept: function(file, done) {
}
};
And this is how I call the scripts:
<script src="{{ asset('js/dropzone/dropzone.js') }}"></script>
<script src="{{ asset('js/image-upload.js') }}"></script>

You are splitting the dropzone configuration into two different methods. And only the first one is being used the one that contains the url option, the second, that contains the maxFiles option is ignored.
You have to either include all the configuration inside the first method that creates dropzone programmatically like this:
Dropzone.autoDiscover = false;
var myDropzone = new Dropzone("div#dropzoneFileUpload", {
url: "/admin/upload",
params: {
_token: token
},
paramName: "file", // The name that will be used to transfer the file
maxFilesize: 2, // MB
addRemoveLinks: true,
maxFiles: 1,
init: function() {
this.on("maxfilesexceeded", function() {
if (this.files[1]!=null){
this.removeFile(this.files[0]);
}
});
},
accept: function(file, done) {
}
});
Or with second method that uses the dropzone autodiscover feature, if your dropzone element has the id #dropzoneFileUpload do it like this:
Dropzone.options.dropzoneFileUpload = {
url: "/admin/upload",
params: {
_token: token
},
paramName: "file", // The name that will be used to transfer the file
maxFilesize: 2, // MB
addRemoveLinks: true,
maxFiles: 1,
init: function() {
this.on("maxfilesexceeded", function() {
if (this.files[1]!=null){
this.removeFile(this.files[0]);
}
});
},
accept: function(file, done) {
}
};

Related

Dropzone is uploading all files to server for each file drop/selection

I'm trying to integrate dropzone in a laravel 8 application. Whenever I drop any image in dropzone, it is uploading all the files of dropzone, not skipping already uploaded file. For example, if I drop "image-1.jpg" in dropzone, it uploads the file instantly (which is desired). But after that, if I select "image-2.jpg", it re-uploads "image-1.jpg" first (which isn't desired, I want to prevent this re-upload), then it uploads "image-2.jpg". Also, I want to prevent folder(containing multiple image) upload and have tried to prevent it by setting maxFiles to 1, but it's not preventing it.
Javascript
var refType = $('#reference-type');
electronic = $('.electronic'),
image = $('.image'),
description = $('.description'),
url = "{{ route('user.bank.files') }}",
submitRef = $('#submit-ref');
Dropzone.autoDiscover=false;
var imageDropzone;
var config = {
url: url, headers: {'X-CSRF-TOKEN':'{{ csrf_token() }}'},
uploadMultiple: false,
maxFiles: 1,
maxFilesize: 1,
paramName: 'reference_image',
addRemoveLinks: true,
acceptedFiles: 'image/jpeg, image/png , image/jpg, image/svg',
init: function() {
imageDropzone = this;
},
removedfile: function (file) {
if (file.accepted) {
data = {
action: 'delete',
tnx_id: "{{ the_tnx(data_get($order, 'tnx')) }}"
};
NioApp.Form.toPost(url, data);
}
var _ref;
return (_ref = file.previewElement) != null ? _ref.parentNode.removeChild(file.previewElement) : void 0;
},
sending: function(data, xhr, formData) {
formData.append('action', 'store');
formData.append('tnx_id', "{{ the_tnx(data_get($order, 'tnx')) }}");
},
addedfiles: function () {
if (this.files.length > 1) {
this.removeFile(this.files[1]);
NioApp.Toast("{{ __('You can upload only one :file.', ['file' => 'image']) }}", 'warning');
}
},
error: function(file, errorMessage) {
$.each(imageDropzone.files, function(i, file) {
file.status = Dropzone.QUEUED
});
}
};
$("#reference-image").dropzone({...config});

File upload in dropzone is working even when clickable is false

I am initializing a dropzone element and based on some parameters I should toggle the clickable function so that some users are able to upload the files and some are not.
I am setting the value of the clickable function to false but still the element is clickable.
How can I rectify this error?
function initializeAttachmentsDropzone(){
Dropzone.autoDiscover = false;
// Dropzone stuff
myDropzone = new Dropzone("div#attachments", { url: "/app/attachments/",
paramName: "attachments",
// maxFiles: 10,
uploadMultiple: true,
addRemoveLinks: true,
autoProcessQueue: false,
parallelUploads: 10,
maxFilesize: 2000,
thumbnailHeight: 250,
thumbnailWidth: 250,
dictRemoveFileConfirmation: "Are you sure you want to delete this file?",
accept: function(file, done) {
if (file.size == 0) {
done("Empty files will not be uploaded.");
}
else { done(); }
},
headers: {
"X-CSRFToken": csrftoken
},
init: function(){
this.removeAllFiles();
}
});
myDropzone.on("removedfile", deleteAttachment);
}
function disableFieldsOnUpdate(){
if ($("#page_details").val()){
$(".dev-page").prop("disabled", true);
myDropzone.options.clickable = false;
}
else {
$(".dev-page").prop("disabled", false);
myDropzone.options.clickable = true;
}
}
$(document).ready(function(){
initializeAttachmentsDropzone();
disableFieldsOnUpdate();
});
Here in the browser when I check the value of clickable it is false but even then I am able to click the attachments element. How can I toggle the functionality as per the requirement?
EDIT
The only solution that worked for me is as below:
let isRemoveEnabledInAttachments;
function getRemoveEnabled() {
if ($("#page_details").val()){
isRemoveEnabledInAttachments = false;
}
if ($("#page_details").attr("update_fields") && $("#page_details").attr("has_permission") == "True"){
isRemoveEnabledInAttachments = true;
}
return isRemoveEnabledInAttachments;
}
function initializeAttachmentsDropzone(){
Dropzone.autoDiscover = false;
// Dropzone stuff
myDropzone = new Dropzone("div#attachments", { url: "/app/attachments/",
paramName: "attachments",
// maxFiles: 10,
uploadMultiple: true,
addRemoveLinks: true,
autoProcessQueue: false,
parallelUploads: 10,
maxFilesize: 2000,
thumbnailHeight: 250,
thumbnailWidth: 250,
dictRemoveFileConfirmation: "Are you sure you want to delete this file?",
accept: function(file, done) {
if (file.size == 0) {
done("Empty files will not be uploaded.");
}
else { done(); }
},
headers: {
"X-CSRFToken": csrftoken
},
init: function(){
this.removeAllFiles();
if (isRemoveEnabledInAttachments) {
this.enable();
} else {
this.disable();
}
}
});
myDropzone.on("removedfile", deleteAttachment);
}
But here even if I disable the remove file link is shown.
EDIT 2
I had to add
this.options.addRemoveLinks = false;
inside the init function itself. Even setting it as global variable and using Dropzone.options.myDropzone.options as stated in the comments did not work.
replace your function as below:
function disableFieldsOnUpdate(){
if ($("#page_details").val()){
$(".dz-hidden-input").prop("disabled",true);
}
else
{
$(".dz-hidden-input").prop("disabled",false);
}
}

Dropzone disable upload

I wanna know how to disabled upload if i have 1 file has uploaded. But if i remove file, the ddropzone been enabled to upload.
here's my code on js:
Dropzone.options.attachmenacc = {
maxFiles: 1,
accept: function(file, done) {
},
init: function() {
this.on("maxfilesexceeded", function(file){
alert("No more files please!");
});
},
addRemoveLinks: true,
removedfile: function(file) {
var name = file.name;
$.ajax({
type: 'POST',
url: host+'upload/bank/unfile',
data: "id="+name,
dataType: 'html'
});
var _ref;
return (_ref = file.previewElement) != null ? _ref.parentNode.removeChild(file.previewElement) : void 0;
//console.log();
},
}
Well on my code now, if i add upload file more, it show alert, and these file (expected first file) wont uploaded, but it still show that file was uploaded.

jQuery : Dropzone.js not working inside bootstrap modal

In my bootstrap modal I have a div panel inside of that is my form of dropzone, in every time I tried to click the modal nothing is showing. It's working properly if outside of the modal. I also try the Dropzone.autoDiscover = false; not working either.
<div class="panel-body" id="id_dropzone">
<form action="UploadImages"
class="dropzone"
id="my-awesome-dropzone" enctype="multipart/form-data">
</form>
</div>
The JS
$(document).on('click','#add_newContestant', function(e){
e.preventDefault();
Dropzone.autoDiscover = false;
var myDropzone = new Dropzone("div#id_dropzone", { url: 'UploadImages'});
});
Try this way, here i didn't use Dropzone.autoDiscover = false;
// "myAwesomeDropzone" is the camelized version of the HTML element's ID
Dropzone.options.myAwesomeDropzone = {
paramName: "file", // The name that will be used to transfer the file
maxFilesize: 2, // MB
accept: function(file, done) {
if (file.name == "anything.jpg") {
done("false");
}
else { done(); }
}
};
and
Dropzone.options.myAwesomeDropzone = {
paramName: "file",
maxFilesize: 10,
url: 'UploadImages',
previewsContainer: "#dropzone-previews",
uploadMultiple: true,
parallelUploads: 5,
maxFiles: 20,
init: function () {
var cd;
this.on("success", function (file, response) {
$('.dz-progress').hide();
$('.dz-size').hide();
$('.dz-error-mark').hide();
console.log(response);
console.log(file);
cd = response;
});
.......
Please check this fiddle Dropzone in Modal . I think this will help you

Dropzone.js - How to add new property to file object after uploading file

I'm using dropzone and PHP to upload and delete files. When I load my upload page I create some mockfiles with the following params: name, size, thumbnail and id. This mock is set using pre uploaded data.
So when someone click on remove file button, I call the php method that delete the image.
My problem is when the user uploads an file and tries to delete it without loading the page. When it happens, dropzone file object just can't be altered.
I'm trying:
var dropZone3 = new Dropzone("#file3",{
init: function() {
this.on('success', function (file) {
console.log(file);
file['test'] = 'test';
file.test = 'test';
console.log(file);
})
},
paramName: 'file3',
autoProcessQueue:true,
uploadMultiple: false,
parallelUploads: 1,
maxFiles: 3,
maxFilesize: 5,
addRemoveLinks: true
My problem is that the first console.log and the second one inside init on success function shows me the same file.
Anyone knows how to fix it?
Thank you in advance.
It's possible to add properties directly to file object (dropzone v4.3.0)
var dropZone = new Dropzone(document.querySelector('.js-dropzone'), {
url: '/file/upload'
});
dropZone.on('success', function (file, response) {
var res = JSON.parse(response);
if (res.result == true) {
file.test = 'test';
file.id = res.id;
}
});
Don't think you can add a property to the file object when is already uploaded, but you can add it before on the accept property:
var dropZone3 = new Dropzone("#file3", {
url: "upload.php",
init: function () {
this.on('success', function (file) {
console.log(file);
})
},
accept: function(file, done) {
file.test = "test";
return done();
},
paramName: 'file3',
autoProcessQueue: true,
uploadMultiple: false,
parallelUploads: 1,
maxFiles: 3,
maxFilesize: 5,
addRemoveLinks: true
});

Categories

Resources