I'm having a terrible time trying to pass a file from a view to a controller using Ajax with CakePHP 2.10.7.
My view has the following input:
<div id="staff-bg-image-container">
<!-- Image uploading box exists here. -->
</div>
<input type="file" id="background_image_file" name="background_image_file" style="display:none;" accept="image/png, image/jpeg">
And my JavaScript looks like this:
$('#staff-bg-image-container').on('click', () => $('#background_image_file').trigger('click') );
$('#background_image_file').on('change', upload_background);
function upload_background()
{
var file = this.files[0];
data = new FormData();
data.append('file', file);
$.ajax({
url: '/staff/ajaxUploadBackgroundImage',
type: 'POST',
data: data,
contentType: false,
processData: false,
cache: false,
success: function(res){
console.log(res);
},
});
}
But when I send this request to the controller the $this->request->data
array is empty. I have tried doing this using XMLHttpRequest instead of jQuery as well, but the result was the same. Why is CakePHP not recognizing the file?
Edit: I can access the file using the $_FILES globals array. However, according to the CakePHP documentation it should be present in the request data array. I would appreciate if you could tell me why it is not.