LAYOUT PAGE
<html ng-app="myFormApp">
<head>
<meta name="viewport" content="width=device-width" />
<title>@ViewBag.Title</title>
<script src="~/Scripts/Demo/angular.js"></script>
@Styles.Render("~/Content/css")
@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/bootstrap")
<style>
.layout-header {
background-color: #0090c3;
height: 75px;
margin-bottom: 1%;
}
</style>
</head>
<body>
<div class="layout-header">
</div>
<div>
@RenderBody()
</div>
</body>
</html>
app.directive('fileUpload', function () {
return {
scope: true, //create a new scope
link: function (scope, el, attrs) {
el.bind('change', function (event) {
var files = event.target.files;
//iterate files since 'multiple' may be specified on the element
for (var i = 0; i < files.length; i++) {
//emit event upward
scope.$emit("fileSelected", { file: files[i] });
}
});
}
};
});
INDEX.CSHTML
<div ng-controller="CTRL_Demo">
<input type="hidden" ng-model="CTRL_Demo.Id"/>
<input type="text" ng-model="CTRL_Demo.FirstName" />
<input type="text" ng-model="CTRL_Demo.LastName"/>
<input type="file" name="file" file-upload />
<button ng-click="SaveEmployee()" class="btn btn-primary">SAVE</button>
</div>
ANGULAR CONTROLLER CODE
var app = angular.module('myFormApp', []);
app.controller('CTRL_Demo', function ($scope, $http) {
$scope.CTRL_Demo = {};
$scope.CTRL_Demo.Id = 101;
$scope.files = [];
$scope.$on("fileSelected", function (event, args) {
$scope.$apply(function () {
$scope.files.push(args.file);
});
});
$scope.GetUserList = function () {
$http.get("/User/GetEmployee")
.then(function (response) {
console.log(response.data);
});
}
$scope.GetUserList();
$scope.SaveEmployee = function () {
var formData = new FormData();
formData.append("Id", $scope.CTRL_Demo.Id);
formData.append("FirstName", $scope.CTRL_Demo.FirstName);
formData.append("LastName", $scope.CTRL_Demo.LastName);
for (var i = 0; i < $scope.files.length; i++) {
formData.append("file" + i, $scope.files[i]);
}
$http({
method: 'POST',
url: "/User/SaveEmployee",
headers: { 'Content-Type': undefined },
processData: false,
contentType: false,
data: formData,
transformRequest: angular.identity,
}).success(function (data, status, headers, config) {
alert("success!");
}).error(function (data, status, headers, config) {
alert("failed!");
});
}
$scope.uploadFile = function (files) {
var fd = new FormData();
fd.append("file", files[0]);
$http.post('@Url', fd, {
withCredentials: true,
headers: { 'Content-Type': undefined },
transformRequest: angular.identity
}).success(function () {
}).error(function () {
});
};
});
MVC CONTROLLER CODE
[HttpPost]
public JsonResult SaveEmployee(UserViewModel model)
{
string _fileName = "";
if (Request.Files.Count > 0)
{
var _empid = int.Parse(Request.Form["Id"]);
var _file = Request.Files[0];
var _fName = Request.Files["file0"].FileName;
var _dotIndex = _fName.LastIndexOf('.');
var _ext = _fName.Substring(_dotIndex);
var _configpath = RequestHelpers.GetConfigurationValue("ImageUpload");
_fileName = _empid + "_IDPROOF" + _ext;
var _dirPath = Server.MapPath(_configpath);
var _filePath = Server.MapPath(_configpath) + _fileName;
if (System.IO.Directory.Exists(_dirPath))
{
if (System.IO.File.Exists(_filePath))
{
System.IO.File.Delete(_filePath);
}
_file.SaveAs(_filePath);
}
else
{
System.IO.Directory.CreateDirectory(_dirPath);
_file.SaveAs(_filePath);
}
}
return Json(_IUser_Repository.GetUser(),JsonRequestBehavior.AllowGet);
}