Question

I'm using the jQuery file upload plugin which has an API to programmatically upload files. The documentation writes:

$('#fileupload').fileupload('add', {files: filesList});

The problem is that I don't know what filesList should be. I have tried the following unsuccessfully:

$('#fileupload').fileupload('add', {files: ['/Users/bob/Desktop/test.png']});

What should filesList be exactly?

Was it helpful?

Solution

To quote the documentation:

The second argument must be an object with an array (or array-like list) of File or Blob objects as files property.

You can get file objects using the files property of a file type input or the HTML5 File API.

For more detail regarding working with the FileAPI and file inputs see: MDC - Using files from web applications

OTHER TIPS

ridiculous example :) that works !

<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<div id="fileupload"></div>
<input class="zz" type="file" name="files[]"  multiple><br />
<input class="zz" type="file" name="files[]"  multiple><br />
<input class="zz" type="file" name="files[]"  multiple><br />
<input class="zz" type="file" name="files[]"  multiple><br /><br /><br /><br />
<input id="envoi_fax" type="submit" class="btn btn-primary start"> <i class="icon-upload icon-white"></i><span>Start upload</span>

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
<script src="js/vendor/jquery.ui.widget.js"></script>
<script src="js/jquery.iframe-transport.js"></script>
<script src="js/jquery.fileupload.js"></script>
<script src="js/jquery.fileupload-fp.js"></script>
<script src="js/jquery.fileupload-ui.js"></script>
<script> 
$('document').ready(function () {
    var mycars = new Array();

    $('#fileupload').fileupload({
        url:'server/php/',
        dataType: 'json',
        singleFileUploads: false,
        done: function (e, data) {
            $.each(data.result, function (index, file) {
                $('<p/>').text(file.name).appendTo(document.body);
            });
        }
    });

    $('.zz').bind('change', function (e) {
        var f;
        f = e.target.files || [{name: this.value}];
        mycars.push(f[0]);
    });

    $("#envoi_fax").click(function () {
        $('#fileupload').fileupload('send', {files: mycars});
    });
});
</script>
</body> 
</html>

The documentation tells you

The second argument must be an object with an array (or array-like list) of File
or Blob objects as files property.

while linking File to Mozilla's DOM File object

You should supply an array of these objects

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top