Want to implement a simple file upload with multipart form in AngularJS? A real working solution with no other dependencies than angularjs.
HTML:
1 2 | <input type="file" name="file" onchange="angular.element(this). scope().uploadFile(this.files)"/> |
controller:
1 2 3 4 5 6 7 8 9 10 11 12 | $scope.uploadFile = function(files) { var fd = new FormData(); //Take the first selected file fd.append("file", files[0]); $http.post(uploadUrl, fd, { withCredentials: true, headers: {'Content-Type': undefined }, transformRequest: angular.identity }).success( ...all right!... ).error( ..damn!... ); }; |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.