2

我正在使用 Symfony2,我想使用 BlueImp 插件,基本设置

实际上,我有一个表格,您可以在其中加载 3 张图片和一首歌曲(用于艺术家注册)。

首先,我尝试为一张图片实现插件。但是当我在我的输入“图片 1”中选择一个文件时,什么也没有发生。有什么问题?

<html>
 <head>
    <script type="text/javascript"  src="{{ asset('bundles/mybundle/js/jquery-file-upload/js/vendor/jquery.ui.widget.js') }}"></script>
    <script type="text/javascript"  src="{{ asset('bundles/mybundle/js/jquery-file-upload/js/jquery.iframe-transport.js') }}"></script>
    <script type="text/javascript"  src="{{ asset('bundles/mybundle/js/jquery-file-upload/js/jquery.fileupload.js') }}"></script>
  </head>


 <form id="myForm" action="{{ path('MyBundle_artist_new') }}" {{ form_enctype(form) }} method="post" novalidate>
     <div class="field">
        {{ form_label(form.picture1 "Picture 1") }}
        {{ form_widget(form.picture1) }}
      </div>
      <input type="submit" value="Register"/>
</form>

<script type="text/javascript">

$(document).ready(function(){

    $('#myform_picture1').fileupload(
    {
        dataType: 'json',
        url:'{{path('MyBundle_ajax_upload_picture') }}',
        done: function (e, data) {
            $.each(data.result.files, function (index, file) {
                $('<p/>').text(file.name).appendTo(document.body);
            });
        }
    });
  });
</script>

这是上传处理程序(url {{path('MyBundle_ajax_upload_picture') }} 的操作:

<?php

    public function ajaxUploadPicture()
    {
        $request = $this->get('request');
        $uploaded_file = $request->files->get('myform_picture1');

        if ($uploaded_file) 
        {
            $picture1 = $this->processImage($uploaded_file);
            $picture1->setPath('pictures/artist/' . $picture1);
            $em->persist($picture1);
            $em->flush();

            $response = 'success';
        }

        else $response= 'error';

        $response = new Response(json_encode(array('response'=>$response)));
        $response->headers->set('Content-Type', 'application/json');
        return $response;
    }

    public static function processImage(UploadedFile $uploaded_file)
    {
        $path = 'pictures/artist/';
        $uploaded_file_info = pathinfo($uploaded_file->getClientOriginalName());
        $filename = uniqid() . "." .$uploaded_file_info['extension'];

        $uploaded_file->move($path, $filename);

        return $filename;
    }
4

1 回答 1

1

您应该检查从 ajax 请求中获得的内容:

$('#myform_picture1').fileupload(
{
    dataType: 'json',
    url:'{{path('MyBundle_ajax_upload_picture') }}',
    done: function (e, data) {
        $.each(data.result.files, function (index, file) {
            $('<p/>').text(file.name).appendTo(document.body);
        });
    },
    always: function (e, data) {
       console.log(data);
    }
});

我不确定这一行$uploaded_file = $request->files->get('myform_picture1'); ,您确定您的文件输入名称是myform_picture1

请试试$uploaded_file = $request->files->get('myform[picture1]', array(), true);

于 2013-02-14T11:11:55.143 回答