新的AjaxUpload由其他一些按钮启动

时间:2012-04-17 09:50:24

标签: jquery file-upload

我正在使用valums.com/ajax-upload插件。问题是,一旦我选择文件就开始上传,我想通过点击其他按钮开始上传,这样用户可以根据需要更改值。

HTML:

<div id="upload" ><span>Upload File<span></div>
<input type="button" name="submit" id="submit" value="Submit" />
<span id="status" ></span><ul id="files" ></ul>

jQuery的:

$(function(){
    var btnUpload = $('#upload');
    var status = $('#status');
    new AjaxUpload(btnUpload, {
        action: 'upload-file.php',
        name: 'uploadfile',  
        autoSubmit: false,
        onSubmit: function(file, ext){
            // if (! (ext && /^(pdf)$/.test(ext))){ 
            if (!(ext && /^(jpg|png|jpeg|gif|pdf)$/.test(ext))) { 
                // extension is not allowed 
                status.text('Only JPG, PNG or GIF files are allowed');
                return false;
            }

            upload.setData({'example_key': 'value'});
        },
        onComplete: function(file, response) {
            //On completion clear the status
            status.text('');

            //Add uploaded file to list
            if (response === "success") {
                $('<li></li>').appendTo('#files').html('<img src="./uploads/'+file+'" alt="" /><br />'+file).addClass('success');
            } 
            else {
                $('<li></li>').appendTo('#files').text(file).addClass('error');
            }
        }
    });

    $("#submit").click(); ////dont know how to do it.
});

1 个答案:

答案 0 :(得分:3)

uploader = new AjaxUpload(...);
uploader.submit();