代码之家  ›  专栏  ›  技术社区  ›  Gonzalo Borges

OneUploaderBundle和jQuery文件在symfony 2上上载

  •  2
  • Gonzalo Borges  · 技术社区  · 8 年前

    它工作了,我放的文件实际上被上传到文件夹,我得到了状态栏填充…但我需要一些其他东西:

    1. 不知何故,我需要随文件一起发布(并读取)一个项目id(整数)(或者在将文件复制到输出文件夹时能够设置文件名)
    2. 如果上传出错,我该如何发回错误消息?
    3. 在这个例子(jQuery文件上传器)中,代码返回上传文件的文件名,我的代码没有这样做,我的意思是代码在那里,但它不工作

    我正在发布我的代码。

    HTML代码(这里是我称之为jQuery文件上传的部分

    <tr>
                        <td>{{ clienteCorporativo.nombreComercial|upper }}</td>
                        <td>{% if clienteCorporativo.afiliadosUploads|length == 0 %}Nunca{% else %}{{ (clienteCorporativo.afiliadosUploads|last).fecha|date('d/mmm/y') }}{% endif %}</td>
                        <td>
                            {% if clienteCorporativo.formatoExcelAfiliados == null %}
                                <span class="btn btn-success fileinput-button">
                                    <i class="glyphicon glyphicon-upload"></i>&nbsp;&nbsp;&nbsp;
                                    <span>Seleccionar Excel</span>
                                    <input id="fileupload_{{ clienteCorporativo.id }}" class="fileupload" data-id="{{ clienteCorporativo.id }}" type="file" name="files[]" multiple>
                                </span>
                                {#<span style="color: #8c8c8c"><span class="glyphicon glyphicon-upload"></span>&nbsp; Seleccionar Excel &nbsp;&nbsp;&nbsp;</span>#}
                            {% else %}
                                <input id="fileupload_{{ clienteCorporativo.id }}" class="fileupload" type="file" name="files[]" data-url="{{ oneup_uploader_endpoint('gallery') }}" />
                                {#<a role="button" data-toggle="modal" data-target="#myModal" data-operation="loadOne" data-id="{{ clienteCorporativo.id }}"><span class="glyphicon glyphicon-upload"></span>&nbsp; Seleccionar Excel</a> &nbsp;&nbsp;&nbsp;#}
                            {% endif %}
                            <a role="button" data-toggle="modal" data-target="#myModal" data-operation="defineFormat" data-id="{{ clienteCorporativo.id }}"><span class="glyphicon glyphicon-list-alt"></span>&nbsp; Definir Formato</a> &nbsp;&nbsp;&nbsp;
                            {% if clienteCorporativo.afiliadosUploads|length == 0 %}
                                <span style="color: #8c8c8c"><span class="glyphicon glyphicon-repeat"></span>&nbsp; Revertir Última Carga &nbsp;&nbsp;&nbsp;</span>
                            {% else %}
                                <a role="button" data-toggle="modal" data-target="#myModal" data-operation="undoLast" data-id="{{ clienteCorporativo.id }}"><span class="glyphicon glyphicon-repeat"></span>&nbsp; Revertir Última Carga</a> &nbsp;&nbsp;&nbsp;
                            {% endif %}
                        </td>
                        <td>
                            <div id="progress_{{ clienteCorporativo.id }}" class="progress text-center">
                                <div class="progress-bar progress-bar-success">
                                    <span id="files_{{ clienteCorporativo.id }}"></span>
                                </div>
                            </div>
                        </td>
                    </tr>
    

    <script>
        /*jslint unparam: true */
        /*global window, $ */
        var idFile = 0;
        $(function () {
            'use strict';
            // Change this to the location of your server-side upload handler:
            $('.fileupload').fileupload({
                url: '{{ oneup_uploader_endpoint('xlsfile') }}',
                dataType: 'json',
                done: function (e, data) {
                    var eventTrigger = $(this)
                    idFile = eventTrigger.data('id')
                    $.each(data.result.files, function (index, file) {
                        $('#files_'+idFile).html(file.name);
                    });
                },
                progressall: function (e, data) {
                    var eventTrigger = $(this)
                    idFile = eventTrigger.data('id')
                    var progress = parseInt(data.loaded / data.total * 100, 10);
                    $('#progress_'+idFile+' .progress-bar').css(
                            'width',
                            progress + '%'
                    );
                },
                formData: [ {"id":idFile} ]
            }).prop('disabled', !$.support.fileInput)
                    .parent().addClass($.support.fileInput ? undefined : 'disabled');
            //
        });
    </script>
    

    其他文件(AppKernel.php、routing.yml、services.yml,config.yml和UploadListener.php)就像OneUploaderbundle文档所说的那样(我已经更改了一些内容,然后回滚,因为我没有得到预期的结果)。我想我吃得更多,我可以吞下这个。。。

    1 回复  |  直到 8 年前
        1
  •  1
  •   tlorens    7 年前

    您的表单字段将随文件上传一起发布。或者你可以使用 form_data: {} 您的 $('#myFile').fileUploader()

    $('#myFile').fileupload({
        dataType: 'json',
        formData: {
            'file_id': 1
        },
    

    您必须在UploadListener中创建自己的返回响应。然后在前端(Javascript)上解析结果。

    $response = $event->getResponse();
    $response['success'] = true;
    $response['files'] = [
        'file' => [
            'name' => $event->getFile()->getPathname()
        ]
    ];
    return $response;