Reload (or reinitialise) Uploadify in jQuery UI modal dialog box
I use Uploadify within a jQuery UI modal dialog box, and I am using Ajax to submit the data. I can then reuse the modal dialog box to upload more files. I almost hav开发者_StackOverflow社区e it working now but here is my problem.
When the modal box is opened a second time, and I add a file using Uploadify, it appears twice in the queue. If I open the modal box a third time, and add a file, it will appear 3 times in the queue (and so on).
When I close the modal dialog form, I use the following code (which is called from the Close function of the jQuery UI modal dialog box):
function DestroyUploadifyInstance(){
$('#image_nameQueue').remove();
$('#image_nameUploader').remove();
}
When I open the modal dialog form, I call the uploadify code again (this is called from the Open function of the jQuery UI modal dialog box):
function CreateUploadifyInstance(){
$('#image_name').uploadify({
'uploader' : 'uploadify.swf',
'script' : 'uploadify.php',
'cancelImg' : 'cancel.png',
etc
I feel like I am almost there, as the code actually works even though duplicate files are shown (it does not upload each file multiple times, just once).
How can I stop duplicate queue items being displayed? Thanks.
Uploadify binds a handler for the event uploadifySelect to the element you call it on, which adds the queue items. If you don't unbind this in DestroyUploadifyInstance then the chain of event handlers will grow with each call to CreateUploadifyInstance, which is what causes the duplicates.
function DestroyUploadifyInstance()
{
$("#image_name").unbind("uploadifySelect");
$('#image_nameQueue').remove();
swfobject.removeSWF('image_nameUploader');
}
This destroy/reinitialize business is only necessary in IE it seems, due to the way it handles Flash objects when these are hidden, so I would make it browser dependent and stick with a single call to uploadify for FF, Chrome, etc., without "destroying" on each close of the dialog.
you can use uploadifySettings()
var news_id = $(this).attr('news_id');
$("#image_name").uploadifySettings('script', 'http://example.com/upload.php?news=' + news_id);
精彩评论