Click here to Skip to main content
15,940,550 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I want to upload file using MVC with angularJS.
But HttpPostedFileBase file in MVC controller shows null.but javascript contains value of File.

Please Help.

What I have tried:

In HTML:
<input type="file" onchange="angular.element(this).scope().setFile(this)" />


Javascript AngularJS:
Javascript Code:
$scope.setFile = function (element) {
$scope.$apply(function ($scope) {
$scope.theFile = element.files[0];
});
};

urlCollectionService.saveInterventionExpenseDetails( $scope.theFile).then(function (data) {}

In urlCollectionService.js
this.saveInterventionExpenseDetails = function ( File) {

return serviceHandler.postService(domain + "/EnterData/SaveInterventionExpenseDetails", File);
}
In UrlcollectionService:
File contains All file values like: modified date, name, size etc...


In MVC Contoller:
public JsonResult SaveInterventionExpenseDetails(HttpPostedFileBase file )
{}

In MVC controller "file" contains Null.
Posted
Updated 2-Mar-17 2:00am
v3
Comments
Nathan Minier 2-Mar-17 9:42am    
Are you wrapping your files in a JavaScript FormData object before sending them?
Member 11449483 3-Mar-17 0:09am    
This is only a single file in a time.

And all the code scenario is define below so please help me ...
Nathan Minier 3-Mar-17 6:58am    
That doesn't matter. If you're not wrapping it in FormData before you post it, HttpPostedFileBase won't be available.
Member 11449483 3-Mar-17 7:12am    
Please give me some hints or code so that i can go ahead.

Thanks for reply.
Nathan Minier 3-Mar-17 7:19am    
this.saveInterventionExpenseDetails = function (File) {
var formData = new FormData(); //should be available in an html5 compliant context
formData.append('file0', File, File.name);

return serviceHandler.postService(domain + "/EnterData/SaveInterventionExpenseDetails", formData);
}

https://developer.mozilla.org/en-US/docs/Web/API/FormData

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900