3

I'm trying to dynamically update the MaxFiles property each time a new image is uploaded/deleted. By using the following code its not allowing any image to upload instead of limitize it to maxFiles. And it is not taking the value of the variable maxFile, but when i remove maxFile variable And put a number then it works fine. got source code idea from this Answer.

!function ($) {
"use strict";
var Onyx = Onyx || {};


Onyx = {
    init: function() {
        var self = this,
            obj;

        for (obj in self) {
            if ( self.hasOwnProperty(obj)) {
                var _method =  self[obj];
                if ( _method.selector !== undefined && _method.init !== undefined ) {
                    if ( $(_method.selector).length > 0 ) {
                        _method.init();
                    }
                }
            }
        }
    },

    userFilesDropzone: {
        selector: 'form.dropzone',
        init: function() {
            var base = this,
                container = $(base.selector);

            base.initFileUploader(base, 'form.dropzone');
        },
        initFileUploader: function(base, target) {

            var maxFile = $('.dropzone').attr('data-count');

            var onyxDropzone = new Dropzone(target, {
                url: ($(target).attr("action")) ? $(target).attr("action") : "data.php", // Check that our form has an action attr and if not, set one here
                maxFiles: maxFile, 
                maxFilesize: 5,
                acceptedFiles: ".JPG,.PNG,.JPEG",
            //  previewTemplate: previewTemplate,
            //  previewsContainer: "#previews",
                clickable: true,
                uploadMultiple: false,

            });

            onyxDropzone.on("success", function(file, response) {
                let parsedResponse = JSON.parse(response);
                file.upload_ticket = parsedResponse.file_link;
                var imagecount = $('.dropzone').attr('data-count');
                    imagecount = imagecount - 1;
                    $('.dropzone').attr('data-count', imagecount);
            });
        },
    }
  }
}// JavaScript Document

function openImagePopup(id = null) {
   $(".upload-images").show();
    $.ajax({
        url: 'fetch.php',
        type: 'post',
        data: {id: id},
        dataType: 'json',
        success:function(response) {
            var imagecount = response.counts;
            $('.dropzone').attr('data-count', imagecount);
    }
 });
}  

HTML

<form action="data.php" class="dropzone files-container" data-count="">
   <div class="fallback">
       <input name="file" type="file" multiple />
   </div>
   <input type="hidden" id="imageId" name="imageId">
</form>
lipon
  • 25
  • 14

2 Answers2

2

UPDATED ANSWER

Once instanciated, the Dropzone plugin will remains with the same options unless you change the instance inner options directly.

To change options of a Dropzone, you can do this with the following line:

$('.dropzone')[0].dropzone.options.maxFiles = newValue;

$('.dropzone')[0] returns the first dropzone DOM element

.dropzone.options return the underlying plugin instance options of the Dropzone. You can now change any options directly on this object.

In you case, you will have to change the function that initiate the popup like follow

function openImagePopup(id = null) {
   $(".upload-images").show();
    $.ajax({
        url: 'fetch.php',
        type: 'post',
        data: {id: id},
        dataType: 'json',
        success:function(response) {
            var imagecount = response.counts;
            $('.dropzone')[0].dropzone.options.maxFiles = imagecount;
    }
 });
}

And change the dropzone onSuccess event like this:

onyxDropzone.on("success", function(file, response) {
    let parsedResponse = JSON.parse(response);
    file.upload_ticket = parsedResponse.file_link;
    var imagecount = $('.dropzone')[0].dropzone.options.maxFiles - 1;
    $('.dropzone')[0].dropzone.options.maxFiles = imagecount;
});

As you can see, You can also remove the data-count="" attribute on you element and reuse the value from the plugin instance options.maxFiles

  • still have the issue. its working with on click function but not working with `dropzone` while uploading file. its not allowing any file to upload. I think `maxfile` inside dropzone function is still 0. – lipon Oct 24 '19 at 15:37
  • Did you tried to `console.log(maxFile)` to see what value is passed to the Dropzone plugin? – Jonathan Larouche Oct 24 '19 at 15:57
  • its value is 0 with `console.log(maxFile)` – lipon Oct 24 '19 at 16:03
  • Should it be that way? do you set the data-count attribute after instanciating the Dropzone plugin? – Jonathan Larouche Oct 24 '19 at 17:07
  • yes on line 33 inside dropzone plugin, before I have used it out side of plugin also, with same result. – lipon Oct 24 '19 at 17:12
  • Ok, I might have miss understood your usecase, When you show the upload popup, you want to change the maxFile count to whatever fetch.php tells you, and after any upload you want to reduce the maxFile by 1. Is that what you need? If so, changing the attribute won't have any effect on the Dropzone plugin, you will have to re-init the plugin with new maxFile every time. – Jonathan Larouche Oct 24 '19 at 17:18
  • yes exactly, and how to re-init the plugin with new maxFile every time – lipon Oct 24 '19 at 17:22
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/201392/discussion-between-jonathan-larouche-and-lipon). – Jonathan Larouche Oct 24 '19 at 17:58
0

After spending a couple of hours of trials and errors I realized using the maxFiles setting from Dropzone is not exactly what is expected in many cases. That setting will only limit uploading files through the explorer / drag&drop, but after reload more files can be uploaded. It also does not reflect any failures to the upload on the serrver side (e.g. file size too big).

Changing the value of the maxFiles setting of an already initialized Dropzone from outside ot it is impossible. For example reseting the number of allowed files after removing some images with ajax will not work.

To really control the number of files that can be uploaded to the server the counting must take place on the server. Then in the Dropzone, in the success function, we should handle the ajax response:

success: function (file, response) {
    var response_data = jQuery.parseJSON(response);
    if(!response_data.success) {
        $(file.previewElement).addClass('dz-error');
        $(file.previewElement).addClass('dz- complete');
        $(file.previewElement).find('.dz-error-message').text(response_data.error);
    }
}

The response is the feedback information provided by the script assigned to the action attribute of the Dropzone <form>, e.g. <form action="/uploader">.

gaba
  • 29
  • 3