j查询文件上传 - 在表单提交时上传文件


jQuery File Upload - upload file on form submit

我有一个带有Jquery文件上传的表单。形式是:

<form action="" method="post" name="job_offer_reply" id="job_offer_reply" enctype="multipart/form-data">
    <textarea name="description" id="description" class="form-control" rows="3"></textarea>
    <div id="drop">
        <?php _e('Drop You CV Here', 'ja'); ?>
        <a><?php _e('OR Browse', 'ja'); ?></a>
        <input type="file" name="upl" multiple/>
    </div>    
    <input id="submit" type="submit" class="btn btn-primary" name="submit" value="Submit">
</form>

文件上传初始化函数(缩短)为:

$(function(){
    $('#drop a').click(function(){
        // Simulate a click on the file input button
        // to show the file browser dialog
        $(this).parent().find('input').click();
    });

    // Initialize the jQuery File Upload plugin
    $('#job_offer_reply').fileupload({
        formData: {
            action: 'ja_upload_cv',
        },
        dropZone: $('#drop'),
        add: function (e, data) {
            // Automatically upload the file once it is added to the queue
            var jqXHR = data.submit();
        },
        done:function(e, data){
            console.log(data);
        },
        fail:function(e, data){
            // Something has gone wrong!
            data.context.addClass('error');
        }
    });
});

此代码在选择/删除文件后立即上传文件。当我删除

var jqXHR = data.submit();

即使在表单提交后,也不会提交它。

单击提交按钮时,如何上传文件(将其放在 $_FILES 数组中)?

只需修改add回调即可使用事件侦听器:

 add: function (e, data) {
    $('#submit').click(function(){
        var jqXHR = data.submit();
    };
 },