yogiben / meteor-autoform-file

Upload and manage files with #autoForm
MIT License
92 stars 101 forks source link

Multi upload with autoform and dropzone #77

Closed mauriciord closed 8 years ago

mauriciord commented 8 years ago

I'm using collectionFS, meteor-autoform and Dropzone, i have a collection with the name: Loteamentos, look some part of the Loteamentos(Schema):


    ...,
      fotos: {
       type: [String],
       optional: true,
       label: "Fotos",
       autoform: {
        type: 'hidden'
       }
      },...

On a template with >quickForm, look like this:


    <!-- //NovoLoteamento.html -->
        {{>quickForm collection="Loteamentos" doc=defChangeValues id="inserirLoteamentoForm" type="insert" class="novo-loteamento-form" buttonContent="Novo Loteamento"}}

And in my js i've made this:


    Session.setDefault('arrayFotos', []);

    Template.NovoLoteamento.helpers({
      defChangeValues: function() {
        return {
          fotos: Session.get('arrayFotos')
        };
      }
    });

    Template.NovoLoteamento.rendered = function(){

      if (Meteor.isClient){
        var arrayOfImageIds = [];
        Dropzone.autoDiscover = false;
        // Adds file uploading and adds the imageID of the file uploaded
        // to the arrayOfImageIds object.
        var dropzone = new Dropzone("form#dropzone", {
            accept: function(file, done){
                Images.insert(file, function(err, fileObj){
                    if(err){
                      alert("Error");
                    } else {
                      alert(fileObj._id + " inserido !");
                      // gets the ID of the image that was uploaded
                      var imageId = fileObj._id;
                      // do something with this image ID, like save it somewhere
                      arrayOfImageIds.push(imageId);
                    };
                });
            }
        });
        Session.set("arrayFotos", arrayOfImageIds);
      };
    };

Why am i doing this ? Because i want to get the arrayOfImageIds and insert into fotos: [String] in Schema. It's better solution ? Because i didn't find a way to put dropzone on autoform. I just want to make images multi upload inside the Loteamentos collection !