简体   繁体   中英

Using Symfony2, how to get ajax post data from the request?

I want to upload a file with jquery-file-upload (blueimp) with some data in cross domain.

So in client side I use the code from basic.html file from jqueryfileupload. I just added forceIframeTransport for cross domain (doc here )& formData (for format type doc here ).

var idResp = {name:'id-resp',value: SLjQuery('#media_answer_answer').data('id-resp')};
var dataSup = $('form#answer_process_form').serializeArray();
dataSup.push(idResp);

$(function () {
    'use strict';
    $('#media_answer_answer').fileupload({
        url: "mywebsite.dev/app_dev.php/api/media/questionnaire-test-media/uploads",
        dataType: 'json',
        formData: dataSup,
        forceIframeTransport: true,
        done: function (e, data) {
            console.log('upload ok ', data);
        },
        progressall: function (e, data) {
             console.log(data.loaded/data.total);
        }
    }).prop('disabled', !$.support.fileInput)
      .parent().addClass($.support.fileInput ? undefined : 'disabled')
    ;
    $('#media_answer_answer').fileupload(
        'option',
        'redirect',
        'mywebsite.dev/result.html?%s'
    );
});

When I select a file by the input, into the console, the log of progressall is to 1. So it seems to be sending. Moreover I can see in the network tab in the console the request sent

//in request headers
Content-Length: 43463
Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryvU5EBWuRkyGkSRBS
...
//in request payload
------WebKitFormBoundaryvU5EBWuRkyGkSRBS
Content-Disposition: form-data; name="media_answer[_token]"

e6583ea5230f1f80a218825ed399115925556f0c
------WebKitFormBoundary1JHxUF9xgtcO5fJ2
Content-Disposition: form-data; name="id-resp"

420
------WebKitFormBoundaryvU5EBWuRkyGkSRBS
Content-Disposition: form-data; name="media_answer[answer]"; filename="file-to-upload.jpg"
Content-Type: image/jpeg

------WebKitFormBoundaryvU5EBWuRkyGkSRBS--

//in response headers
...
X-ChromeLogger-Data: eyJ2ZXJzaW9uIjoiNC4wIiwiY29sdW1.... // I think this is the file in base64
...

Now in server side, I use FosRestBundle for routing. In my symfony controller I want to get the data and the file.

//result of request
POST /app_dev.php/api/media/questionnaire-test-media/uploads  HTTP/1.1 
Accept:          text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8 
...
Content-Length:  43463 
Content-Type:    multipart/form-data; boundary=----WebKitFormBoundaryBqSBpxJ6Wyvi2QJH 

So content-length is 43463 so I think there is something, but in the controller $request->getContent() is empty and $request->request->all() too.

Do you know if data are sended ? if yes, how to get data present in payload ? thanks a lot and happy new year

Finally, the logger of symfony doesn't show me anything on $request->request->all() , but in preview tab in console I could see some var_dump on $request->request->all() and it wasn't empty (I want to swear myself), it contains the 2 first data of request payload (content-disposition form-data), but not the last one (Content-Type: image/jpeg).

I had to change the 2nd data to become

------WebKitFormBoundaryvU5EBWuRkyGkSRBS
Content-Disposition: form-data; name="media_answer[idResp]"

420

Then in the controller I can get the data like this :

$r = $request->request->all(); // or using $request->getContent() it's also possible in a different way
$rt = $r['media_answer']['_token']; // e6583ea5230f1f80a218825ed399115925556f0c
$ri = $r['media_answer']['id-resp']; // 420

For the last data : name="media_answer[answer]" filename="file-to-upload.jpg" As I said in the comments above I can get the file like this :

$request->files->get('media_answer[answer]', array(), true)

So for the filename :

$request->files->get('media_answer[answer]', array(), true)->getClientOriginalName()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM