admin管理员组

文章数量:1405765

I'm letting the user select files and then trying to programmatically uploading them after they click the upload button using the jquery fileupload script

The HTML looks like this:

<input name="my_image[]" id="my_file" type="file" multiple="multiple">

The jquery call looks like this:

$('#start-upload').click(function(e) {

    var filesList = $('#my_file')[0].files;
    //var filesList = $('#my_file').prop("files");

    var url = 'photos/index.php';
    $('#my_file').fileupload('send', {
        files: filesList,
        url: url,
        dataType: 'json',
        start: function(e, data) {
            console.log("Upload started");
        },
        done: function (e, data) {
            console.log("Upload plete");
        }  
    });
});

This is the error I get:

Uncaught Error: cannot call methods on fileupload prior to initialization; attempted to call method 'send' 

Any idea what I'm doing?

I'm letting the user select files and then trying to programmatically uploading them after they click the upload button using the jquery fileupload script

The HTML looks like this:

<input name="my_image[]" id="my_file" type="file" multiple="multiple">

The jquery call looks like this:

$('#start-upload').click(function(e) {

    var filesList = $('#my_file')[0].files;
    //var filesList = $('#my_file').prop("files");

    var url = 'photos/index.php';
    $('#my_file').fileupload('send', {
        files: filesList,
        url: url,
        dataType: 'json',
        start: function(e, data) {
            console.log("Upload started");
        },
        done: function (e, data) {
            console.log("Upload plete");
        }  
    });
});

This is the error I get:

Uncaught Error: cannot call methods on fileupload prior to initialization; attempted to call method 'send' 

Any idea what I'm doing?

Share Improve this question asked Jan 6, 2014 at 21:35 PaulPaul 11.8k32 gold badges92 silver badges145 bronze badges 4
  • 4 The error message seems pretty self explanatory to me. The very first section on the page you link to shows how to initialize the plugin. – gilly3 Commented Jan 6, 2014 at 21:37
  • I must be missing the obvious... – Paul Commented Jan 6, 2014 at 21:40
  • I guess you have to initialize the fileupload with only a JS object as argument before you can call other methods on it (like send in this case). The link gilly3 posted shows an example of how to initialize it correctly – stex Commented Jan 6, 2014 at 21:52
  • 1 I don't think what you are missing is that obvious, @Paul. I am trying to do a programmatic upload, and getting the same error. The doc does not advise that we have to initialize the plugin before calling send, where I assume that passing all required options in the send call would be enough. – ProfK Commented Dec 14, 2014 at 11:12
Add a ment  | 

2 Answers 2

Reset to default 3

It wasn't initialized. Ensure you first have something like:

$(document).ready(function(){
   $('#my_file').fileupload({ url: 'your_url' ...} );
});

Cheers

To expand on Edgar's answer, this is how I set things up to programmatically submit:

//Initialize
$('#file').fileupload({
  url: 'someUrl'
});

//Trigger the submit

$('#file').fileupload({
  fileInput: $('#file')
});

本文标签: javascriptgetting error when trying to use jquery fileUploadStack Overflow