Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically uploading a file

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?

like image 311
Randomblue Avatar asked Jan 20 '12 10:01

Randomblue


2 Answers

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>
like image 82
pedro.dz Avatar answered Oct 13 '22 22:10

pedro.dz


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

like image 27
Sam Greenhalgh Avatar answered Oct 13 '22 22:10

Sam Greenhalgh