Click here to Skip to main content
15,894,017 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
This is my jquery code to upload multiple file. Input file generated dynamically so i am calling this FileUploader function where these input file generated. But i have to click twice to upload file. Any ideas are appreciated. :)

C#
FileUploader: function($dis) {
                    var fileName = '';
                    var $htm = $($dis.parents('div.sfFormInput').find('div.cssClassUploadFiles'));
                    var $ht = $($dis.parent('div.uploader'));
                    var extension = new RegExp($ht.attr('extension'), "i");

                    var upload = new AjaxUpload($('#' + $dis.attr('id') + ''), {
                        action: Path + "UploadHandler.ashx",
                        name: "myfile[]",
                        multiple: true,
                        data: {},
                        autoSubmit: true,
                        responseType: "json",
                        onChange: function(file, ext) {
                        },
                        onSubmit: function(file, ext) {
                            if ($ht.attr('almul') == "false" && $('div.cssClassUploadFiles').children('div').length > 0) {
                                csscody.alert('<h1>Alert Message</h1><p>You can upload only one file at  a time!</p>');
                                return false;
                            }
                            if (ext != "exe" && extension != '') {
                                if (ext && extension.test(ext)) {
                                    this.setData({
                                        'MaxFileSize': $ht.attr('filesize')
                                    });
                                } else {
                                    csscody.alert('<h1>Alert Message</h1><p>Not a valid file!</p>');
                                    return false;
                                }
                            }
                        },
                        onComplete: function(file, response) {
                            var html = '';
                            var filePath = Path + "/UploadedFiles/" + file;
                            if (file.split('.')[1] == "jpg" || file.split('.')[1] == "JPEG" || file.split('.')[1] == "gif" || file.split('.')[1] == "bmp" || file.split('.')[1] == "png")
                                html = '<div title="' + Path + "UploadedFiles/" + file + '" ><img height="10%" width="10%" src="' + filePath + '"/><a class="sfDeleteFile"><img src="../Modules/FormBuilder/images/closelabel.png" /></a></div>';
                            else
                                html = '<div title="' + Path + "UploadedFiles/" + file + '" >' + file + '  <a class="sfDeleteFile"><img src="../Modules/FormBuilder/images/closelabel.png" /></a></div>';
                            $htm.append(html);
                        }
                    });
                }
Posted

1 solution

If you Google[^] for it then you will know that this is a common issue faced by a lot of people.

Have a look at these discussions:
jQuery AjaxUpload, have to click button twice?[^]
Jquery form upload plugin: how to reduce it to one click?[^]
AjaxUpload: why have to click twice?[^]


Based on above discussions, it looks like, the reason why you needed to click several times, is because AjaxUpload() does NOT initiate a file upload. Instead, it attaches a click listener to the element you pass to it as a parameter. So, when you clicked the first time, the listener was created. The second time, the file upload was initiated. So, try:
JavaScript
$(function(){
         AjaxUpload("#upload_files",{
        // your stuff here
      });
 }

Thus, when the page is loaded, the AjaxUpload plugin adds a listener to the #upload_files element, that then in turn triggers the file upload.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900