Download files using ajax and Symfony2

I have a problem downloading a file from a form to a Symfony2 controller using ajax.

This is my client side form:

var uploadFile = function() { var content = "<form id='uploadFile' enctype='multipart/form-data' action='' method='post'>" + "<input id='file' type='file'/>" + "</form>"; $("#upload-dialog").html(content); $("#upload-dialog").dialog({ resizable: false, title: 'Dodaj załączniki do umowy', height: 300, width: 450, buttons: [ { text: 'Wyślij', click: function() { var formData = new FormData(); formData.append('file', document.getElementById('file').files[0]); $.ajax({ url: Routing.generate('employees_upload_attachment'), data: formData, enctype: 'multipart/form-data', processData: false, contentType: false, success: function() { }, error: function() { } }); } } ] }); 

};

and this is my controller

  public function uploadAttachmentAction(Request $request) { $fileBag = $request->files; var_dump($fileBag); } 

When I try to show the downloaded file, I get an empty FileBag object:

object (Symfony \ Component \ HttpFoundation \ FileBag) # 12 (1) {
["parameters": protected] => array (0) {}}

What could be wrong?

EDIT: I solved it. I added jquery ajax options

cache: false, enter: "POST"

and everything is fine :)

+6
source share
1 answer

Adding ajax options:

 cache: false, type: 'POST' 

Solved problem.

Note. The answer is based on a comment not of mine, because it appears in the "no response" section.

0
source

All Articles