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

4,689 views
Skip to first unread message

Primoz Rome

unread,
Feb 2, 2013, 11:45:30 AM2/2/13
to jquery-f...@googlegroups.com
I am integrating fileupload to my app. I want to use bootstrap UI version, with PHP. I use all includes from example app included in download package ... I initialize my script like:

    /* UPLOAD */
    $('#fileupload').fileupload({
        maxFileSize: 100000000,
        acceptFileTypes: '/^image\/(gif|jpeg|png)$/',
        previewSourceFileTypes: '/^image\/(gif|jpeg|png)$/',
        url: 'upload.php'
    });
    $.ajax({
        url: 'upload.php',
        dataType: 'json',
        context: $('#fileupload')[0]
    }).done(function (result) {
        console.log(this);
        $(this).fileupload('option', 'done')
            .call(this, null, {result: result});
    });

Right after AJAX call to upload.php is executed I get the above error 'Error: cannot call methods on fileupload prior to initialization; attempted to call method 'option''. I have spent hours figuring out what this would be but no luck...

Thanks,
Primoz

Zach Wilson

unread,
Feb 2, 2013, 12:48:00 PM2/2/13
to jquery-f...@googlegroups.com
Try and use the handlers provided with the plugin and bind the submit() to your upload button in the 'add' hander. Something like this might be more effective:

<div class="fileupload fileupload-new" data-provides="fileupload">
    <span class="btn-small btn-file">
        <span class="fileupload-new">Choose an image</span>
        <input type="file" id="fileupload" name="media[]" data-url="upload.php" multiple/>
    </span>
    <span class="fileupload-preview small clearfix"></span>
    <a href="#" class="close fileupload-exists" data-dismiss="fileupload" style="float: none">×</a>
</div>
<button style="display: inline-block;" class="btn smaller" id="media-upload-btn">Upload</button>

    $('#fileupload').fileupload({
        maxFileSize: 100000000, 
        acceptFileTypes: '/^image\/(gif|jpeg|png)$/',
        previewSourceFileTypes: '/^image\/(gif|jpeg|png)$/',
            add: function (e, data) {
                data.context = null;

                data.context = $('#media-upload-btn').click(function (e) {
                    e.preventDefault();
                    data.submit();
                });
            },
           done:  function (e, data) {
            if(data && data.result) {
            data.fileInput.context = [];
// do something with your result here
            }
    });
Reply all
Reply to author
Forward
0 new messages