多个jQuery文件上载小部件在同一页面上

时间:2018-01-29 21:23:26

标签: javascript jquery ajax upload

我正在使用Jquery File Upload插件。我试图整合3 widgets File Upload on the same page。每个小部件都接受特定格式:图像(png,jpg,gif),文档(PDF,DOC)和视频(mov,mpeg)。文件将位于特定文件夹中:

  • 图片 - >文件夹' / server / php / imgs /'
  • 文件 - >文件夹' / server / php / docs /'
  • 视频 - > ' /服务器/ PHP /视频/'

首先,我在表单中更改了类的id。我把data-id命名为文件夹。

<form class="fileupload" data-id="imgs" method="POST" enctype="multipart/form-data"
    data-upload-template-id="template-upload-1"
    data-download-template-id="template-download-1">
    <!-- ... -->
</form>
<form class="fileupload" data-id="docs" method="POST" enctype="multipart/form-data"
    data-upload-template-id="template-upload-2"
    data-download-template-id="template-download-2">
    <!-- ... -->
</form>
<form class="fileupload" data-id="videos" method="POST" enctype="multipart/form-data"
    data-upload-template-id="template-upload-3"
    data-download-template-id="template-download-3">
    <!-- ... -->
</form>

在main.js文件中,我这样做了:

$(function () {
    'use strict';

    // Initialize the jQuery File Upload widget:
    $('.fileupload').each(function () {

        $(this).fileupload({
            dropZone: $(this)
        });

        $(this).fileupload({
            // Uncomment the following to send cross-domain cookies:
            //xhrFields: {withCredentials: true},
            url: 'server/php/'+$(this).attr('data-id')+'/'
        });

        // Load existing files:
        $(this).addClass('fileupload-processing');
        $.ajax({
            // Uncomment the following to send cross-domain cookies:
            //xhrFields: {withCredentials: true},
            url: $(this).fileupload('option', 'url'),
            dataType: 'json',
            context: $(this)[0]
        }).always(function () {
            $(this).removeClass('fileupload-processing');
        }).done(function (result) {
            $(this).fileupload('option', 'done')
                .call(this, $.Event('done'), {result: result});
        });

    });

    // Enable iframe cross-domain access via redirect option:
    $('.fileupload').fileupload(
        'option',
        'redirect',
        window.location.href.replace(
            /\/[^\/]*$/,
            '/cors/result.html?%s'
        )
    );

});

在index.php中,我保留了原始代码:

require('UploadHandler.php');
$upload_handler = new UploadHandler();

但代码无效!哪里错了? 我如何对每个人接受上传的特定格式?

0 个答案:

没有答案