gpt4 book ai didi

ajax - Blueimp jQuery 文件上传和 symfony2 : issue with custom upload handler

转载 作者:行者123 更新时间:2023-12-01 04:55:08 24 4
gpt4 key购买 nike

我正在使用 Symfony2,我想使用 BlueImp 插件,basic setup .

实际上,我有一个表单,您可以在其中加载 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;
}

最佳答案

您应该检查从 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);

关于ajax - Blueimp jQuery 文件上传和 symfony2 : issue with custom upload handler,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14872115/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com