Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within HTML Pages with CSS and JavaScript
Search Coderanch
Advance search
Google search
Register / Login
Win a copy of
Helidon Revealed: A Practical Guide to Oracle’s Microservices Framework
this week in the
Java in General
forum!
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Tim Cooke
paul wheaton
Liutauras Vilda
Ron McLeod
Sheriffs:
Jeanne Boyarsky
Devaka Cooray
Paul Clapham
Saloon Keepers:
Scott Selikoff
Tim Holloway
Piet Souris
Mikalai Zaikin
Frits Walraven
Bartenders:
Stephan van Hulst
Carey Brown
Forum:
HTML Pages with CSS and JavaScript
Why angularJs file upload is failing to send data to spring controller at backend?
Yogesh Bhat
Greenhorn
Posts: 3
posted 9 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
I'm trying to upload a file from front end to save in the database. But Spring controller is failing to receive the file.
HTML
<html> <title>FILE_OLPV</title> <head> <script src = "http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script> </head> <body ng-app = "myApp"> <div ng-controller = "myCtrl"> <input type = "file" file-model = "myFile"/> <button ng-click = "uploadFile()">upload me</button> </div> <script type="text/javascript" src="js/abc.js"></script> </body> </html>
JS
var myApp = angular.module('myApp', []); myApp.directive('fileModel', ['$parse', function ($parse) { return { restrict: 'A', link: function(scope, element, attrs) { var model = $parse(attrs.fileModel); var modelSetter = model.assign; element.bind('change', function(){ scope.$apply(function(){ modelSetter(scope, element[0].files[0]); }); }); } }; }]); myApp.service('fileUpload', ['$http', function ($http) { this.uploadFileToUrl = function(file, uploadUrl){ var fd = new FormData(); fd.append('file', file); $http.post(uploadUrl, fd, { transformRequest: angular.identity, headers: {'Content-Type': undefined} }) .success(function(){ }) .error(function(){ }); } }]); myApp.controller('myCtrl', ['$scope', 'fileUpload', function($scope, fileUpload){ $scope.uploadFile = function(){ var file = $scope.myFile; console.log('file is ' ); console.dir(file); var uploadUrl = 'fileUpload'; fileUpload.uploadFileToUrl(file, uploadUrl); }; }]);
Spring Controller
@RequestMapping(value="/fileUpload", method=RequestMethod.POST) public @ResponseBody String upload(@RequestBody MultipartFile file ) throws IOException { System.out.println("HEEEEEEEEEEEEEEEEEEEE"); MultipartFile file1 = file; System.out.println(file1); System.out.println("Inside for loop"); }
Content of the file in Spring controller is null. Any idea on this?
That new kid is a freak. Show him this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
Routing not working as expected in Angular.js
AngularJS : Parsing JSON Object inside AngularJS controller
Spring MVC/Angular JS / AJax --- how to fix a view change?
Angular JS : Syntax for passing the parameters to the factory for REST call
AngularJS Updating Total in Dynamic Table
More...