通过jquery上传多个文件

时间:2012-05-29 05:44:25

标签: javascript jquery

这是我上传多个文件的jquery代码。输入文件动态生成,所以我调用这个输入文件生成的FileUploader函数。但我必须点击两次才能上传文件。任何想法都表示赞赏。

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 != '') {enter code here
                                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);
                        }
                    });
                }


代码有效但只有问题是我必须单击两次才能上传文件。

1 个答案:

答案 0 :(得分:1)

问题不在于文件上传部分,而是在初始化部分中看起来像。如果动态创建文件上载控件,请确保在标记中绑定后将初始化上载器。