For the past 3 days, I've been grappling with uploading files via AJAX to my Symfony2 application using AngularJS.
I'm utilizing my Symfony app as an API to deliver data to my Angular front end. While I can successfully post text and other data, I've hit a roadblock when it comes to posting files from the front end to the back end.
With the help of the chrome plugin DHC, I managed to post a picture and retrieve the object from the $request variable, but Angular seemed to be a dead end in this scenario.
I experimented with various solutions like dropzoneJS, ng-file-upload services, and more, but none seemed to do the trick. So, I reverted to using a basic hidden iframe, yet even that didn't work effectively. It appears that the request is being converted to a GET by the time it reaches the back end, despite being sent as a POST.
Feeling stuck and frustrated, I decided to start over with my PHP controller code.
Here's a glimpse of my current code:
Angular view :
<div class="uploadTest">
<form id="file_upload_form" method="post" enctype="multipart/form-data" action="/submission" ng-submit="submit()">
<input name="file" id="file" size="27" type="file">
<input type="submit" name="action" value="Upload">
<iframe id="upload_target" name="upload_target" src="" style="width:580px;height:580px;border:2px solid red;"></iframe>
</form>
</div>
Angular controller :
angular.module('mondialatorApp').controller('UploadCtrl', ['$scope', function ($scope){
$scope.submit = function(){
document.getElementById('file_upload_form').target = 'upload_target';
}
}]);
My PHP controller (which was empty due to lack of progress):
class SubmissionController extends Controller{
public function addAction(Request $request){
return new Response('done');
}
}
My routing file :
mondialator_submission_add:
path: submission/
defaults: {_controller: MDMondialatorBundle:Submission:add}
Lastly, examining the content of $request variable:
object(Symfony\Component\HttpFoundation\Request)[7]
// Content of $request goes here
I've scoured through numerous forums and websites without finding a solution. The issue seems elusive, and I'm at a loss for what to try next. In the words of Leia, "You are my only hope."