Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plupload Automatically start upload when files added

When files are added i want to start the upload process automatically. I called the start function at the end of FilesAdded but it doesn't start the upload.

uploader.bind('FilesAdded', function(up, files) {
      var str = "";
      for (var i in files) {
        str += '<div id="' + files[i].id + '">' + files[i].name + ' (' + plupload.formatSize(files[i].size) + ') <b></b></div>';
      }
      $('#filelist').html(str);
      up.refresh();
      up.start();
    });

Here is my creation code

var uploader = new plupload.Uploader({
      runtimes: 'html5,flash,silverlight',
      autostart : true,
      url: '<%= images_path %>',
      max_file_size: '10mb',
      multipart: true,
      browse_button: "pickfiles",
      container: "the-uploader",
      drop_element : "drop-area",  
      multipart_params: {
        '_http_accept': 'application/javascript',
        '<%=request_forgery_protection_token%>': '<%=form_authenticity_token%>',
        '<%=request.session_options[:key]%>': '<%=request.session_options[:id]%>'
      },
      filters: [
        {title: "Images", extensions: "avi,jpg,jpeg,png,zip"}
      ],
    });
like image 900
Abid Avatar asked Nov 15 '11 13:11

Abid


5 Answers

Adding up.start() in your FilesAdded bind should start the upload when a file is added. I've gone down the route of calling my uploader like so (I had problems doing it the way you are trying to call it):

$(function() {
    // Setup html5 version
    $("#html5_uploader").pluploadQueue({
      // General settings
      runtimes : 'html5',
      url : 'upload.php',
      max_file_size : '10mb',
      chunk_size : '1mb',
      unique_names : true,
      dragdrop : true,
      multiple_queues : false,
      multi_selection : false,
      max_file_count : 1,

      // Specify what files to browse for
      filters : [
        {title : "Text files", extensions : "txt"}
      ],

      init : {
        FilesAdded: function(up, files) {
          up.start();
        },
        UploadComplete: function(up, files) {
          $.each(files, function(i, file) {
            // Do stuff with the file. There will only be one file as it uploaded straight after adding!
          });
        }
      }
    });
  });
like image 146
ingh.am Avatar answered Sep 30 '22 09:09

ingh.am


"Make sure you bind it after the init since it binds default handlers."

So your code:

uploader.bind('FilesAdded', function(up, files) {...});

after your

uploader.init();

more information

like image 34
Stefan Ladwig Avatar answered Sep 30 '22 10:09

Stefan Ladwig


For me it didn't work your version but did work :

FilesAdded: function(up, files) {
   setTimeout(function () { up.start(); }, 100);
 },

So set a timer after 100 ms to execute the start. I am using the jquery ui version while testing and got this error :

g("#" + l.id).position() is null

/js/plupload/js/jquery.ui.plupload/jquery.ui.plupload.js
like image 24
Gafitescu Daniel Avatar answered Sep 30 '22 10:09

Gafitescu Daniel


I was also wanting same for me and found below way to do it.

A new way to automatically start file uploading after adding file is just need to set true to autostart property like below

$("#uploader").plupload({
    .....
    autostart: true,
    .....
});
like image 32
NullPointer Avatar answered Sep 30 '22 09:09

NullPointer


Just trigger the Start Upload button by this way

FilesAdded: function(up, files) {
   $('#fileupload_start').click();
},

This will upload the file without waiting for 100 ms.

like image 41
Azam Alvi Avatar answered Sep 30 '22 10:09

Azam Alvi