I have 3 tables in database
Employee
EId EName
1 mahesh
Items
Id EId Name Country FileName
1 1 aaaa India F:\upload\App_Data\Functionalities.xlsx
2 2 bbbb India F:\upload\App_Data\Functionalities1.xlsx
Products
PId EId PName PFileName
1 1 mmmm F:\upload\App_Data\Functionalities1.xlsx
2 1 aaaa F:\upload\App_Data\Functionalities.xlsx
Currently my requirement is that based upon EId i am going to save N no of item values to Items table
I am not getting how to save upload files to relevant tables(2 tables)
So far I have done only1 table
EX:In customers array I have uplaoded 2 fields then save that array Upload files to Items table
In the same way I have uploaded 2 files on products then save that array values to Product table
I am not getting
Could you please help me
@{
Layout = null;
}
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Index</title>
</head>
<body>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" />
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.9/angular.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/danialfarid-angular-file-upload/12.2.13/ng-file-upload.min.js"></script>
<script type="text/javascript">
var app = angular.module('MyApp', ['ngFileUpload'])
app.controller('MyController', function ($scope, $window) {
$scope.Customers = [];
$scope.Products = [];
$scope.Add = function () {
var newrowNo = $scope.Customers.length + 1;
$scope.Customers.push({ Name: "" + newrowNo });
};
$scope.Add1 = function () {
var newrowNo = $scope.Products.length + 1;
$scope.Products.push({ PName:"" + newrowNo });
};
$scope.SelectedFiles = [];
$scope.UploadFiles = function (files) {
if (files.length > 0) {
$scope.SelectedFile = files;
$scope.SelectedFiles.push(files);
}
};
$scope.Save = function () {
var orders = {};
orders.EName = $scope.EName;
var details = new Array();
var details1 = new Array();
for (var i = 0; i < $scope.Customers.length; i++) {
var detail = {};
detail.Name = $scope.Customers[i].Name;
detail.Country = $scope.Customers[i].Country;
details.push(detail);
}
for (var i = 0; i < $scope.Products.length; i++) {
var detail = {};
detail.PName = $scope.Products[i].PName;
details1.push(detail);
}
orders.details = details;
var formData = new FormData();
var files = $scope.SelectedFiles;
if (files != undefined) {
for (var i = 0; i < files.length; i++) {
formData.append(files[i][0].name, files[i][0]);
}
}
formData.append("orders", JSON.stringify(orders));
$.ajax({
url: '/Home/SaveOrder',
type: "POST",
contentType: false,
processData: false,
data: formData,
success: function (result) {
alert(result);
$scope.Customers = [];
$scope.refresh();
},
error: function (err) {
$scope.Message = err.Message;
}
});
};
});
</script>
<div ng-app="MyApp" ng-controller="MyController">
<div class="row">
<div class="col-md-12">
<div class="panel panel-default">
<table class="table table-condensed">
<tr class="active">
<td>Employee Name</td>
<td><input type="text" class="form-control" ng-model="EName" /></td>
</table>
</div>
</div>
</div>
<table cellpadding="0" cellspacing="0">
<tr>
<th>Name</th>
<th>Country</th>
<th></th>
</tr>
<tbody ng-repeat="m in Customers">
<tr>
<td>
<input type="text" ng-model="m.Name" />
</td>
<td><input type="text" ng-model="m.Country" /></td>
<td><input type="file" ngf-select="UploadFiles($files)" /></td>
<td><input type="button" ng-click="Remove($index)" value="Remove" /></td>
</tr>
</tbody>
<tfoot>
<tr>
<td><input type="button" ng-click="Add()" value="Add" /></td>
</tr>
</tfoot>
</table>
<table cellpadding="0" cellspacing="0">
<tr>
<th>PName</th>
<th>UploadFile</th>
<th></th>
</tr>
<tbody ng-repeat="m in Products">
<tr>
<td>
<input type="text" ng-model="m.PName" />
</td>
<td><input type="file" ngf-select="UploadFiles($files)" /></td>
</tr>
</tbody>
<tfoot>
<tr>
<td><input type="button" ng-click="Add1()" value="Add" /></td>
</tr>
</tfoot>
</table>
<div style="padding: 10px 0;">
<input id="submit" type="button" value="Save" name="add" ng-click="Save()" class="btn btn-success" />
</div>
</div>
</body>
</html>
[HttpPost]
public ActionResult SaveOrder()
{
string fileName;
string result = "Error! Order Is Not Complete!";
{
try
{
// Get Order details from Request object.
Employee orders = JsonConvert.DeserializeObject<Employee>(Request.Form["orders"]);
// Do other task.
Employee order = new Employee();
order.EName = orders.EName;
// Insert to Employee Table.
db.Employees.Add(order);
db.SaveChanges();
int orderId = order.EId;
// Get uploaded files from Request object.
if (Request.Files.Count > 0)
{
for (int i = 0; i < Request.Files.Count; i++)
{
HttpPostedFileBase postedFile = Request.Files[i];
if (Request.Browser.Browser.ToUpper() == "IE" || Request.Browser.Browser.ToUpper() == "INTERNETEXPLORER")
{
string[] file = postedFile.FileName.Split(new char[] { '\\' });
fileName = file[file.Length - 1];
}
else
{
fileName = postedFile.FileName;
}
fileName = Path.Combine(Server.MapPath("~/App_Data/"), fileName);
postedFile.SaveAs(fileName);
MultipleUpload item1 = orders.details[i];
item1.EId = orderId;
item1.FileName = fileName;