I am completely new to coding so forgive me if this question is stupid. I am trying to add a file upload button on a html page for the user to upload the file onto the server. The project uses html, angularjs and the api is in c#.
My HTML code is as follows:
<div ng-controller="MyCtrl">
<input type="file" name="file" onchange="angular.element(this).scope().uploadFile(this.files)"/>
</div>
My Angularjs function within the controller is:
$scope.uploadFile = function(files) {
var fd = new FormData();
//Take the first selected file
fd.append('file', files[0]);
$http({
method: 'POST',
url: '/Users/x/Documents/api/FileController.cs',
withCredentials: true,
headers: {'Content-Type': undefined },
transformRequest: angular.identity
}).success(function(){alert('Success')}).error(function(){alert('Error')});
};
In the FileController.cs file in asp.net I have a function:
[HttpPost]
public ActionResult Index(HttpPostedFileBase file) {
if (file.ContentLength > 0) {
var fileName = Path.GetFileName(file.FileName);
var path = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName);
file.SaveAs(path + FileName);
}
return RedirectToAction("Index");
}
The http post keeps showing an error.
My questions are:
- Is the format of this url correct? url: '/Users/x/Documents/api/FileController.cs'
- Can a http post to a .cs file?
- Is there something missing from the function of the function written in c#?
- Is a page load required in c# to receive the post?