How to Use jquery-fileupload to receive data in symfony

Source: Internet
Author: User
It uses the symfony framework and jquery to upload images, but the background cannot accept data. Code and error message: {code ...} {code ...} the background code is as follows: {code ...} the error message is: Notice: Undefinedindex: files []. What is the problem? Thank you. It uses the symfony framework and jquery to upload images, but the background cannot accept data. The code and error message are as follows:

$(document).ready(function(){    $("#fileupload").fileupload({        dataType: 'json',        done: function (e, data) {            $.each(data.result.files, function (index, file) {                $('

').text(file.name).appendTo(document.body); }); }, progressall: function (e, data) { var progress = parseInt(data.loaded / data.total * 100, 10); $('#progress .bar').css( 'width', progress + '%' ); }, done: function (e, data) { $.each(data.result, function (index, file) { $('

').text(file.name + ' uploaded').appendTo($("body")); }); } }); });

The background code is as follows:

$imgPurpose = trim(stripslashes($_POST["files[]"]));

The error message is as follows:
Notice: Undefined index: files []
What is going on? Thank you.

Reply content:

It uses the symfony framework and jquery to upload images, but the background cannot accept data. The code and error message are as follows:

$(document).ready(function(){    $("#fileupload").fileupload({        dataType: 'json',        done: function (e, data) {            $.each(data.result.files, function (index, file) {                $('

').text(file.name).appendTo(document.body); }); }, progressall: function (e, data) { var progress = parseInt(data.loaded / data.total * 100, 10); $('#progress .bar').css( 'width', progress + '%' ); }, done: function (e, data) { $.each(data.result, function (index, file) { $('

').text(file.name + ' uploaded').appendTo($("body")); }); } }); });

The background code is as follows:

$imgPurpose = trim(stripslashes($_POST["files[]"]));

The error message is as follows:
Notice: Undefined index: files []
What is going on? Thank you.

A simple answer to your question is as follows:

Premise: You have never used thisjQueryPlug-ins, so only considerBackground code

First, in the Form Control'snameWhen the attribute value is an array (add[]), When using PHP to obtain the content submitted by the control, it should be omitted[]So your code should be as follows:

$_POST["files"]);

2. Use$_POSTGlobal variable accessfilesThe result is a PHP array. Therefore, the string function cannot be used directly for processing (the string function is not discussed here for the time being)

Third, you are usingSymfonyFramework, should not be accessed directly$_POSTAnd other global variables!SymfonyOfHttpFoundationThe component has abstracted these global variablesOOPInterface, you should useSymfonyProvided interface. As shown below: (assume that your Controller method is as follows)

//...use Symfony\Component\HttpFoundation\Request;// ...public function uploadAction(Request $request){    $imgPurpose = $request->get('files');}// ...

Finally,SymfonyOfHttpFoundationThe component has powerful functions. The following are links to its official documentation.SymfonyThe framework is very helpful.

  1. Symfony and Http Basics

  2. HttpFoundation component documentation

Note: andSymfonyFor related questions, addSymfonyLabelSymfonyThe answer authors are more interested in the question. (A prompt is displayed when you enter the content, as shown in figure)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.