Search code examples
jqueryfile-uploaduploadify

Uploadify is not passing through the scriptData parameters


I'm using the Uploadify script on my site and I'm trying to set the scriptData parameter based on some form fields. This is the HTML/JS:

<script type="text/javascript">

    function UploadFile() {
        $('#file_upload').uploadifySettings({
            scriptData: $('#uploadForm').serializeObject()
        });

        $('#file_upload').uploadifyUpload();
    }

    $(document).ready(function () {
        $('#file_upload').uploadify({
            'uploader': '/Scripts/uploadify/uploadify.swf',
            'script': '/File/Upload',
            'cancelImg': '/Scripts/uploadify/cancel.png',
            'folder': '/uploads',
            'fileExt': '*.doc, *.pdf',
            'buttonText': 'Select File',
            'auto': false,
            'onSelect': function (event, ID, fileObj) {
                $('#uploadForm #FileName').val(fileObj.name);           
            }
        });
    });
</script>
<form id="uploadForm">
<div><label for="Description">Description</label> <input id="Description" name="Description" type="text" value="" /></div>
<div><label for="FileName">File Name</label> <input id="FileName" name="FileName" type="text" value="" /></div>    <input id="file_upload" name="file_upload" type="file" />
<button onclick="UploadFile();" type="button">Upload</button>
</form>

serializeObject is just using the serializeobject jQuery plugin to turn the form values into a json object

It uploads the file fine, but nothing in scriptData gets sent. I've checked in fiddler & the only form values are the ones from the uploadify script: folder, fileext, Filedata & Upload.


Solution

  • Aaargh, so I just didn't read the doco properly, I was doing this to set the scriptData:

    $('#file_upload').uploadifySettings({
        scriptData: $('#uploadForm').serializeObject()
    });
    

    When what I needed to be doing was

    $('#file_upload').uploadifySettings('scriptData', $('#uploadForm').serializeObject());
    

    It now works