简体   繁体   English

以编程方式从多个输入字段上载多个文件-Blueimp jQuery FileUpload

[英]Uploading multiple files from multiple input fields programatically - blueimp jquery fileupload

I'm trying to use the blueimp/jQuery-File-Upload plugin to store images in a server via a PHP uploads handler. 我正在尝试使用blueimp / jQuery-File-Upload插件通过PHP上传处理程序将图像存储在服务器中。 I've been following this posts in order to make it work: 我一直在关注此帖子,以使其正常工作:

The file upload form I'm using, has multiple dynamically added file input fields. 我正在使用的文件上传表单具有多个动态添加的文件输入字段。 As an example, after adding 3 fields: 例如,添加3个字段后:

<form id="entry_form" class="entry-form" role="form">
    <div class="entry">
        ...
        <input type="file" class="file-upload" name="files0[]" multiple>
        ...
    </div>
    <div class="entry">
        ...
        <input type="file" class="file-upload" name="files1[]" multiple>
        ...
    </div>
    <div class="entry">
        ...
        <input type="file" class="file-upload" name="files2[]" multiple>
        ...
    </div>
</form>

<div class="col-xs-6 col-sm-4">
    <button id="save_btn" class="btn btn-lg btn-block">Save</button>
</div>

I can successfully upload files from this fields using the following JS code: 我可以使用以下JS代码从该字段成功上传文件:

var imageUpload = {
    init: function (selector, context, options) {

        selector = selector || '.file-upload';
        context = context || $('.entry');

        var filesList = [];
        var url = site_url + '/doUpload';

        $(selector).fileupload(options || {
            url: url,
            type: 'POST',
            dataType: 'json',
            autoUpload: false,
            singleFileUploads: false,
            formData: {},

            add: function (e, data) {
                for (var i = 0; i < data.files.length; i++) {
                    filesList.push(data.files[i]);
                }

                return false;
            }
        }).on('change', function () {
            $(this).fileupload('add', {
                fileInput: $(selector)
            });
        });

        $('#save_btn').click(function (e) {
            e.preventDefault();

            $(selector).fileupload('send', {files: filesList});
        });
    }
};

As you can see in the 'add:' event callback and the 'send' method near the end, I'm sending all files in one POST to the server, this is the intended functionality. 正如您在“ add:”事件回调和“ send”方法接近尾声中看到的那样,我将所有POST文件发送到服务器,这是预期的功能。 My problem is that the $_FILES server side variable is reaching the server in the following way: 我的问题是$ _FILES服务器端变量以以下方式到达服务器:

$_FILES array[1]        
    [files0]    array[1]        
        [name]  array[1]        
            [0] string  "Collapse.png"  
            [1] string  "Expand.png"    
            [2] string  "Map.jpg"

In this way, I can't relate which image was added to which input. 这样,我无法关联将哪个图像添加到哪个输入。 So, what I'd need to get to the server is something like this: 所以,我需要到达服务器的是这样的:

$_FILES array[3]        
    [files0]    array[1]        
        [name]  array[1]        
            [0] string  "Collapse.png"  
    [files1]    array[1]        
        [name]  array[1]        
            [0] string  "Expand.png"    
    [files2]    array[1]        
        [name]  array[1]        
            [0] string  "Map.jpg"

I have been looking for a solution to this for a while and haven't reached the desired result. 一段时间以来,我一直在寻找解决方案,但尚未达到期望的结果。 Can you help me please? 你能帮我吗?

Thanks! 谢谢!

It seems you have to manually add the array keys, 看来您必须手动添加阵列键,

Something roughly like... 大概像...

        add: function (e, data) {
            for (var i = 0; i < data.files.length; i++) {
                filesList['files'+ i].push(data.files[i]);
            }

Solved after reading this post: Uploading multiple files asynchronously by blueimp jquery-fileupload 阅读此文章后已解决: 通过blueimp jquery-fileupload异步上传多个文件

All it was needed was to save the input field name into a 'paramNames' variable to send it alongside the 'filesList' variable. 只需将输入字段名称保存到“ paramNames”变量中,然后将其与“ filesList”变量一起发送即可。

Updated working code: 更新的工作代码:

var imageUpload = {
init: function (selector, context, options) {

    selector = selector || '.file-upload';
    context = context || $('.entry_form');

    var filesList = [],
        paramNames = [];
    var url = site_url + '/doUpload';

    $(selector, context).fileupload(options || {
        url: url,
        type: 'POST',
        dataType: 'json',
        autoUpload: false,
        singleFileUploads: false,
        add: function (e, data) {
            for (var i = 0; i < data.files.length; i++) {
                filesList.push(data.files[i]);
                paramNames.push(e.delegatedEvent.target.name);
            }

            return false;
        },
        change: function (e, data) {

        },
        done: function (e, data) {

        }

    });
    $('#save_btn').click(function (e) {
        e.preventDefault();

        $(selector, context).fileupload('send', {files:filesList, paramName: paramNames});
    });
}
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM