1

I am using the plupload in a mvc site and would like to upload a single file as soon as it is selected. I set autostart of the uploader control to true but this does not activate the upload. I also tried calling uploader.start(); in the 'filesadded' delegate of the uploader but this only works on the second file I try to upload. I am using plupload 1.4.3.2 without the UI widget. Here's my code:

var uploader = new plupload.Uploader({
    runtimes: 'gears,html5,flash,silverlight,browserplus',
    browse_button: 'pickfiles',    
    autostart : true,    
    max_file_size: '10mb',
    url: '/Event/Upload',
    resize: { width: 320, height: 240, quality: 90 },
    flash_swf_url: '/Scripts/pl/plupload.flash.swf',
    silverlight_xap_url: '/Scripts/pl/plupload.silverlight.xap',
    filters: [
    { title: "Image files", extensions: "jpg,gif,png" },
    { title: "Zip files", extensions: "zip" }
]
});
uploader.bind('Init', function (up, params) {
    $('#filelist')[0].innerHTML = "<div>Current runtime: " + params.runtime + "</div>";
});
uploader.bind('Error', function (up, err) {
    $('#filelist').append("<div>Error: " + err.code +
        ", Message: " + err.message +
        (err.file ? ", File: " + err.file.name : "") +
        "</div>"
    );

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

uploader.bind('UploadProgress', function (up, file) {
    $('#' + file.id)[0].getElementsByTagName('b')[0].innerHTML = '<span>' + file.percent + "%</span>";
});

uploader.init();;
Gluip
  • 2,917
  • 4
  • 37
  • 46
  • I managed to get it working by adding calling the up.start function with a timeout from the FilesAdded method like : setTimeout(function () { up.start(); }, 1000); – Gluip Jun 03 '11 at 12:50

3 Answers3

6

In your FilesAdded event change uploader.start(); to setTimeout('uploader.start()', 100);

Ben Hood
  • 76
  • 1
1

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

more information

(I know way to late, but I still like this solution)

Stefan Ladwig
  • 569
  • 9
  • 19
0

Exactly! Just ordering command on that order:

uploader.init();
uploader.bind('FilesAdded', function(up, files) {
    ...
    uploader.start();
});
mihkelo
  • 111
  • 4
  • 2
    Please provide more context to your answer; 'exactly' what? Also, this sentence is unclear; "Just ordering command on that order". – Nathaniel Ford Oct 29 '12 at 18:36