Plupload, dynamically change url

Hugo picture Hugo · Oct 6, 2011 · Viewed 7.7k times · Source

I have an upload form with plupload and a checkbox with boolean value after the plupload div.

I want to change the value of the url in plupload if the checkbox is checked.

Here is my code

<div id="uploader">
    <p>You browser doesn't have Flash, Silverlight, Gears, BrowserPlus or HTML5 support.</p>
</div>
<input id="compressFiles" type="checkbox" name="compressFiles" style="margin:10px 0 0 10px;" value="compress" checked="checked" /> 

$(function() {

$("#compressFiles").change(function(){
    if( $("#compressFiles").is(':checked') ){
            compress = 'compress';
       }
       else{
        compress = 'no';
       }
})

$("#uploader").plupload({

    runtimes : 'gears,flash,html5,html4,browserplus,silverlight',
    url: 'uploadHandler.php?compressFiles=' + compress,
    max_file_size : '1000mb',
    max_file_count: 20, // user can add no more then 20 files at a time
    unique_names : true,
    dragdrop : true,
    multiple_queues : true,

    // Addeb by LG - problem with FF
    filters: [
        {title: "All", extensions: "*"}
    ],

    // Rename files by clicking on their titles
    rename: true,

    // Sort files
    sortable: true,

    // Flash settings
    flash_swf_url : 'js/plupload.flash.swf',

    // Silverlight settings
    silverlight_xap_url : 'js/plupload.silverlight.xap',
    init : {
        FilesAdded: function(up) {
             if( $("#compressFiles").is(':checked') ){
                compress = "no"
             }
             else{
                 compress = "no"
             }
    }
    }

});

// Client side form validation
$('form').submit(function(e) {
    var uploader = $('#uploader').plupload('getUploader');

    // Validate number of uploaded files
    if (uploader.total.uploaded == 0) {
        // Files in queue upload them first
        if (uploader.files.length > 0) {
            // When all files are uploaded submit form
            uploader.bind('UploadProgress', function() {
                if (uploader.total.uploaded == uploader.files.length){ alert("coucou");
                    $('form').submit();}
            });

            uploader.start();
        } else
            alert('You must at least upload one file.');

        e.preventDefault();
    }
});

});

The value of url variable is defined first time page load with compress value. I tried 1000 thinhs but impossible to refresh the compress value in the url when checkbox change.

I hope my problem is clear, dont speak english very good.

Thanks for help

Answer

funwhilelost picture funwhilelost · Oct 20, 2011

Simply bind to the "BeforeUpload" event and you can change the uploader.settings to fit your needs.

this.uploader.bind('BeforeUpload', function(uploader, file) {
  if($("#compressFiles").is(':checked')) {
    uploader.settings.url = "uploadHandler.php?compressFiles=compress";
  } else {
    uploader.settings.url = "uploadHandler.php?compressFiles=no";
  }
});