I'm working on a tool in MVC. When I upload a file I need to take some xls variables out from it and insert them to the Database. This could take 30-40 sec. I would like to show some feedback to the user while this long process is executed. So I added a function named GetImportStatus() which should return the prcessed variables to the user. I add this function to the Controller that executes the insertion in Database. So my controller looks like
public class AdminController : Controller
{
int currentIndex;
int totalVariables;
public ActionResult GetAllVariables()
{
//processing code here
totalVariables = Variables.Count;
}
//other stuffs here
public void InserVariablestToDatabase()
{
foreach(string variable in VariableList)
{
Insert(variable);
currentIndex++;
}
}
//other code here
public JsonResult GetImportStatus()
{
return Json((new { Current = currentIndex.ToString(), Total = totalVariables.ToString() }), JsonRequestBehavior.AllowGet);
}
}
My view has this part,
<div id="updateDiv"></div>
<script type="text/javascript">
function updateScreen() {
$.ajax({
type: 'GET',
url: '@Url.Action("GetImportStatus","Admin")',
content: "application/json; charset=utf-8",
success: function (data) {
$("#updateDiv").append("<strong>" + data + "</strong>");
}
});
}
$("#btnUploadFile").click(function ()
{ var id = setInterval(updateScreen, 1000); });
</script>
where "btnUploadFile" is the upload button. When I inspect the .NET panel from firebug, I see the requests but I get no responses until the importing process is finished. See image below
Am I doing something wrong?
Thank You! :)