views:

85

answers:

2

I am using Uploadify for a real time application and so far its working fine except this one issue. I have 6 Browse buttons for uploading 6 files (with multi - 'true' for each) and I have a submit button as well on my page (JSP).

If the user selects a file on any of these Browse buttons, there is a slight delay before the progress bar is displayed after the file is selected. In the meanwhile if the user clicks the Submit button, the form gets submitted even before the progress bar is displayed and NO file gets uploaded. I have looked at the available methods but not able to find a solution still.

I highly appreciate and look forward to any help in this matter.

Thanks.

Please find my code below:

    $("#vehShortTestAttachment1").uploadify({
        'uploader'  : '../pts/swf/uploadify.swf',
        'script'    : url,
        'cancelImg' : '../pts/images/cancel.png',
        'wmode'     : 'transparent',
        'hideButton': 'true',
        'width'     : 67,
        'height'    : 20,
        'multi'     : true,
        'sizeLimit' : 20971520,
        'fileDesc'  : 'jpg, gif, doc, ppt, jpeg, txt, pdf',
        'fileExt'   : '*.jpg;*.gif;*.doc;*.ppt;*.jpeg;*.txt;*.pdf',
        'onCancel': function () {
            $('#attachments-div-validation').html("");
            isFileBig = false;  
        },
        'onSelectOnce': function (event, queueID, fileObj) {        
            $("#attachments-submit-case-button").attr("disabled", true); 
        },
        'onSelect': function (event, queueID, fileObj) {        

                $("#attachments-div-validation").html(div_validation_red_start + "<B>You can select other files (or) Submit the Case now.</B>" + div_validation_red_end);
            $("#attachments-div-validation").show();

            if (fileObj.size > 20971520) 
            {               
                $('#attachments-div-validation').html(div_validation_red_start + "Size of the file: " + fileObj.name + " exceeds 20MB and this file can not be uploaded. <br>Please click on the X button on the progress bar for this file to cancel the upload. <br>Please click on BROWSE button again to upload another file." + div_validation_red_end);
                $('#attachments-div-validation').show();    
                isFileBig = true;                                       
            }
        },
        'onComplete': function(event, queueID, fileObj, response, data)
        {                               
            if(response == 'OK') {
                $('input[name=fileUploadStatus]').val(response);
                $("#vehShortTestAttachment1").uploadifySettings('script', url);
            }
            else {
                $('input[name=fileUploadStatus]').val(response);
                $('#vehShortTestAttachment1').uploadifyCancel(queueID);
                $('#vehShortTestAttachment1').uploadifyClearQueue();                    
            }
        },
        'onAllComplete':function(event, data) 
        {                       
            $("#attachments-submit-case-button").attr("disabled", false); 

            if(!isFileBig)
                submitFormDetails();
        }
    });
+1  A: 

You could check, if there are upload-items present when the user tries to subbmit the form

$("form").submit(function(evt) {
    if ($(".uploadifyQueueItem").children().length > 0) {
        evt.preventDefault();
        alert("There are still files to upload...");
    }
    //...
});

Edit: Just checked the documentation

Why not disable the Submit-Button, when a file gets selected and enable it on onAllComplete.

$("#fileInput").uploadify({
    //...
    onSelectOnce: function() {
        $("#btn_submit").attr("disabled", true);
    },
    onAllComplete: function() {
        $("#btn_submit").attr("disabled", false);
    }
});
john_doe
excellent one @John_Doe :D
TheVillageIdiot
Neither of the above methods worked. The upload queue has nothing when the user clicks the submit button even before the progress bar is displayed. onSelectOnce also did not work. Any other ideas? How can I prevent the user to submit the form?
Radhika
Could you please provide some more details? "onSelectOnce also did not work" isn't really helpful. The event fires, when a file gets selected. So this event occurs way before the upload starts. That's the earliest chance to react.
john_doe
I meant even though I disabled the button as:
Radhika
$("#btn_submit").attr("disabled", true); on onSelect still I could submit the form in the meanwhile before the progress bar was displayed
Radhika
Is the submit-button disabled? Is there another way to submit the form? Could you provide us an example to get a look on the code?
john_doe
I am sorry there was a delay in posting my code.
Radhika
I am sorry there was a delay from my side in posting the code. I have edited my question and posted the code using onSelectOnce which is not working. Also, I can not use if ($(".uploadifyQueueItem").children().length > 0) { as nothing is there in the queue if the user click on the submit button even before the progress bar is displayed.
Radhika
I look forward to your help further. Thanks.
Radhika
I'm sorry. I haven't known the plugin till the first reading of your question... I could only try to "interpret" the documentation and thats all I found... Do they have to submit a file? If its required to select a file, why not disable the submit-button right of the beginning and enable it in the onSelect if the required number of files is selected. If the upload is optional put all in a hidden div. When displaying the div disable the submit and enable it on onSelect as stated above.
john_doe
Whether I use hidden div or enable/disable Submit button, onSelect is not useful as this event only gets fired after progress bar is displayed. I need to pause the submission even before the progress bar is displayed. There is a slight delay when the user selects a file till the progress bar display and in the meantime user can submit the form. I tried the onInit too as posted in the other answer but it does not work in my scenario as file selection is NOT mandatory in my case. Any other sugesstions please?
Radhika
A: 

Just an extract, but this is approximately what I'm using...

    onInit           : function ( ) {
        $('#uploadbtn').attr('disabled', true);
    },
    onSelect         : function (a, b, c, d, e) {
        $('#uploadbtn').attr('disabled', false);
    },

I'm not sure why you have 6 browse buttons.. but I don't think it matters in this case.

aland
Thanks for the solution. But onInit is only useful when file selection is mandatory for form submission. In my case, file selection is optional and if the user selects file, it should be submitted along with the form. In the above code, uploadbtn is disabled all the time until the user selects a file. Any other ideas?
Radhika
ah ok, I didn't understand from your original question - but the button should be active by default and disabled only between the time some clicks 'browse' and the file has finished being set in the queue.
aland
no unfortunately I see the button disabled all the time (if I disable it on onInit) until the user selects a file. This way the submit button will be enabled only on selection of at least one file and file selection will be therefore mandatory and thats not our requirement. File selection is optional in my case. Any other suggestions? I need to get this working as this is a realtime application and there is a high possibility that user selects a file and immediately clicks the Submit button without waiting for the progress bar to get displayed and attachment will never be uploaded.Please help!
Radhika
I think the problem with onInit is thats its triggered when the script is loaded initially and the flash file is rendered on the screen. That happens as soon as the page is loaded. So submit button is disabled and is only enabled when a file is selected. RonnieSan - Can you please in solving this issue?
Radhika