4

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 complete");
        }  
    });
});

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?

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. Commented Jan 6, 2014 at 21:37
  • I must be missing the obvious... 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 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. Commented Dec 14, 2014 at 11:12

2 Answers 2

3

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

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

Cheers

Sign up to request clarification or add additional context in comments.

1 Comment

It doesn't work for me, it starts an upload as soon files are selected.
0

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')
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.