Hi mahesh213,
Check this example. Now please take its reference and correct your code.
First you need to save the uploaded file in project folder.
Then read the dbf file using OleDb and fill the DataTable using OleDbDataAdapter.
Then loop through the DataTable rows insert the record in database.
After finishing the process delete saved dbf file from folder.
Controller
public class HomeController : Controller
{
// GET: /Home/
public ActionResult Index()
{
return View();
}
public JsonResult AddState()
{
string fileName = "";
if (Request.Files.Count > 0)
{
HttpPostedFileBase postedFile = Request.Files[0];
fileName = postedFile.FileName;
// Save dbf file to Files folder.
postedFile.SaveAs(Path.Combine(Server.MapPath("~/Files/"), fileName));
}
DataTable dt = new DataTable();
OleDbConnection con = new OleDbConnection(@"Provider=VFPOLEDB.1;Data Source=" + Path.Combine(Server.MapPath("~/Files/"), fileName));
con.Open();
if (con.State == ConnectionState.Open)
{
string query = "SELECT * FROM Customers"; // dbf table name
OleDbCommand cmd = new OleDbCommand(query, con);
OleDbDataAdapter oda = new OleDbDataAdapter(cmd);
oda.Fill(dt);
con.Close();
if (dt.Rows.Count > 0)
{
//Insert records to database table.
CustomersEntities entities = new CustomersEntities();
foreach (DataRow row in dt.Rows)
{
entities.Customers.Add(new Customer
{
Name = row["Name"].ToString(),
Country = row["Country"].ToString()
});
}
entities.SaveChanges();
}
}
// Delete saved dbf file.
System.IO.File.Delete(Path.Combine(Server.MapPath("~/Files/"), fileName));
return Json("Import dbf file Success!", JsonRequestBehavior.AllowGet);
}
}
View
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Index</title>
<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://cdnjs.cloudflare.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.8/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("myCntrl", ['$scope', '$http', 'myService', function ($scope, $http, myService) {
$scope.UploadFiles = function (files) {
$scope.SelectedFiles = files;
};
$scope.AddUpdateEmployee = function () {
var formData = new FormData();
var files = $scope.SelectedFiles;
if (files != undefined) {
for (var i = 0; i < files.length; i++) {
formData.append(files[i].name, files[i]);
}
}
var details = {};
details.Name = $scope.Name;
formData.append("details", JSON.stringify(details));
var getData = myService.AddSt(formData);
getData.then(function (tc) {
alert(tc);
});
}
} ]);
app.service("myService", function ($http) {
this.AddSt = function (employee) {
var response = $.ajax({
url: '/Home/AddState',
type: "POST",
contentType: false,
processData: false,
data: employee
});
return response;
}
});
</script>
</head>
<body ng-app="myApp" ng-controller="myCntrl">
<div class="container">
<div>
<div id="wrapper" class="clearfix">
<form name="userForm">
<div class="form-horizontal">
<div class="form-row">
<div class="col-md-4">
<label for="COI_Name">
Upload a file
</label>
<input type="file" ngf-select="UploadFiles($files)" />
</div>
</div>
<div>
</div>
<div class="form-group">
</div>
<div class="form-group" style="width: 120%; text-align: center; padding: 10px;">
<div class="col-md-offset-2 col-md-5">
<p>
<button ng-model="IsVisible" ng-click="AddUpdateEmployee()">
<span class="glyphicon glyphicon-ok"></span>Submitt
</button>
</p>
</div>
</div>
</div>
</form>
</div>
</div>
</div>
</body>
</html>