SWFUpload and Dynamic POST
Recently, it came across the need to generate the upload_script string dynamically. This can be useful if you are trying to pass additional arguement along with the file such as a caption that is different for each file. I figured I’d drop it here so maybe someone else who is in the same boat doesn’t have to have such a hard learning curve
First, we define a callback function that dynamically generates our upload string:
var basestring = ‘/rem/upload/uploadFile?’
var cookie = readCookie(‘JSESSIONID’);
var caption = ‘&caption=’ + document.getElementById(file.id+‘caption’).value;
return dwrString+cookie+caption;
}
I should note when I generated my file_queue lines that i added an input field with the file.id+caption as its identifier. Next, we need to edit the SWFUpload-src.js file and add a field to tell if the upload_script variable is function or a string
this.addSetting("flash_target", settings["flash_target"], ""); // Where to output the flash (not used)
this.addSetting("flash_width", settings["flash_width"], "1px"); // Flash width
this.addSetting("flash_height", settings["flash_height"], "1px"); // Flash height
this.addSetting("flash_color", settings["flash_color"], "#000000"); // Flash color
//dynamic generation of upload_script variable
this.addSetting(‘upload_script_is_function’, settings[‘upload_script_is_function’], ”);
…..
sb.append("&uploadQueueCompleteCallback=" + this.getSetting("upload_queue_complete_callback"));
sb.append("&autoUpload=" + this.getSetting("auto_upload"));
sb.append("&allowedFiletypes=" + this.getSetting("allowed_filetypes"));
sb.append("&maximumFilesize=" + this.getSetting("allowed_filesize"));
sb.append("&uploadScriptIsFunction=" + this.getSetting(‘upload_script_is_function’));
…..
Now we need to actually modifiy the SWFUpload action script and add the uploadScriptIsFunction variable:
uploadQueueCompleteCallback = _root.uploadQueueCompleteCallback;
flashLoadedCallback = _root.flashLoadedCallback;
uploadFileStartCallback = _root.uploadFileStartCallback;
uploadScriptIsFunction = _root.uploadScriptIsFunction;
……
And our modification to the upload() function:
if(uploadScriptIsFunction) {
//user says this is dynamically generated
currentFile.upload(String(ExternalInterface.call(uploadScript, getFileObject(currentFileId, currentFile))));
} else {
//statically specified
currentFile.upload(uploadScript);
}
Now when you create your SWFUpload object in Javascript, just specify it like such to take advantage of the new feature:
upload_script: ‘generateUploadString’,
upload_script_is_function: true,
…….
Oh yeah, and we have to rebuild the flash app to, so for me that’s:
Hope it helps someone.
sajax says:
[...] … SAJAX ColdFusion POST Request Method: COOL! SAJAX ColdFusion POST Request Method: COOL! …Hacker for Hire SWFUpload and Dynamic POSTRecently, it came across the need to generate the upload_script string dynamically. This … on Web [...]
3 April 2010, 4:58 am