Hi guys,
I have an ASP.NET Core MVC application. I am trying to upload an excel and in the controller, I am inserting these data into a database. It works on FF and IE but in Chrome it keeps waiting and waiting without an error. I checked the developer tool on chrome but no error. Any ideas why it is not working?
Screenshot
Here is the ImportExcel.cshtml
Here is controller portion:
I have an ASP.NET Core MVC application. I am trying to upload an excel and in the controller, I am inserting these data into a database. It works on FF and IE but in Chrome it keeps waiting and waiting without an error. I checked the developer tool on chrome but no error. Any ideas why it is not working?
Screenshot
Here is the ImportExcel.cshtml
ImportExcel.cshtml:
@{
ViewData["Title"] = "ImportExcel";
}
<h2 class="font-weight-bolder">Import Games </h2>
<!DOCTYPE html>
<html lang="">
<head>
<meta name="viewport" content="width=device-width"/>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.css"/>
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.js"></script>
</head>
<body>
<form method="post"
asp-action="Upload"
asp-controller="GameBanks"
enctype="multipart/form-data">
<div class="input-group">
<div class="custom-file">
<input type="file" class="custom-file-input" name="postedFile" id="postedFile" aria-describedby="inputGroupFileAddon04">
<label class="custom-file-label" for="postedFile">Choose file</label>
</div>
<div class="input-group-append">
<button class="btn btn-outline-secondary" type="submit" id="inputGroupFileAddon04">Upload</button>
</div>
</div>
</form>
@section scripts {
<script>
$(document).ready(function() {
$('#postedFile').on('change',
function() {
//get the file name
var fileName = $(this).val();
//replace the "Choose a file" label
$(this).next('.custom-file-label').html(fileName);
});
$("#inputGroupFileAddon04").click(function() {
// disable button
$(this).prop("disabled", true);
// add spinner to button
$(this).html(
'<span class="spinner-border spinner-border-sm" role="status" aria-hidden="true"></span> Importing...'
);
});
});
</script>
}
</body>
</html>
Controller:
[HttpGet]
[Authorize]
public IActionResult ImportExcel()
{
return View();
}
[HttpPost]
[Authorize]
public async Task<IActionResult> Upload(IFormFile postedFile)
{
if (postedFile == null || postedFile.Length == 0)
{
return RedirectToAction("ImportExcel");
}
try
{
var timestamp = DateTime.UtcNow;
//Get file
var newfile = new FileInfo(postedFile.FileName);
var fileExtension = newfile.Extension;
//Check if file is an Excel File
if (fileExtension.Contains(".xls"))
{
using (MemoryStream ms = new MemoryStream())
{
await postedFile.CopyToAsync(ms);
using (ExcelPackage package = new ExcelPackage(ms))
{
ExcelWorksheet workSheet = package.Workbook.Worksheets["Game"];
int totalRows = workSheet.Dimension.Rows;
List<GameBanks> customerList = new List<GameBanks>();
for (int i = 2; i <= totalRows; i++)
{
customerList.Add(new GameBanks
{
ProductDescription = workSheet.Cells[i, 1].Value.ToString(),
ProductCode = workSheet.Cells[i, 2].Value.ToString(),
UnitPrice = Convert.ToDouble(workSheet.Cells[i, 3].Value),
Quantity = Convert.ToInt16(workSheet.Cells[i, 4].Value),
Version = workSheet.Cells[i, 5].Value.ToString(),
Currency = workSheet.Cells[i, 6].Value.ToString(),
TotalPrice = Convert.ToDouble(workSheet.Cells[i, 7].Value),
Status = Convert.ToInt16(workSheet.Cells[i, 8].Value),
Used = Convert.ToInt16(workSheet.Cells[i, 9].Value),
RequestDateTime = DateTime.Now,
Signature = User.Identity.Name
});
customerList[i - 2].coupons = new GameBankPin
{
expiryDate = Convert.ToDateTime(workSheet.Cells[i, 10].Value),
Serial = workSheet.Cells[i, 11].Value.ToString(),
Pin = workSheet.Cells[i, 12].Value.ToString()
};
}
//NLOG
NLog(logger, User.Identity.Name, timestamp, totalRows - 1);
_context.GameBanks.AddRange(customerList);
//_context.GameBankPins.AddRange(pins);
await _context.SaveChangesAsync();
}
}
}
}
catch (Exception e)
{
logger2.Error(e.Message);
throw;
}
return RedirectToAction("Index");
}
Last edited by a moderator: