最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Programmatically uploading a file - Stack Overflow

programmeradmin5浏览0评论

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?

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?

Share Improve this question edited Oct 2, 2014 at 10:30 Tom Fenech 74.6k13 gold badges116 silver badges151 bronze badges asked Jan 20, 2012 at 10:54 RandomblueRandomblue 116k150 gold badges362 silver badges557 bronze badges 4
  • 1 You certainly cannot upload any file from the user's computer like that. Where would that lead. – kapa Commented Jan 20, 2012 at 11:05
  • @bazmegakapa: Please see here, the "Example: Non-HTTP synchronous request" – Randomblue Commented Jan 20, 2012 at 11:07
  • Yes, that could work from a locally running script to fetch a file. But you cannot use it to upload files to a remote server. – kapa Commented Jan 20, 2012 at 11:10
  • @bazmegakapa: Hum. So why is the API provided? – Randomblue Commented Jan 20, 2012 at 11:11
Add a comment  | 

3 Answers 3

Reset to default 9

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>

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

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

发布评论

评论列表(0)

  1. 暂无评论