0

I am trying to write and read the session data, but it is not working somehow. Due to long data, I have got a fetch request every 20sec to get the processed row numbers from the loop. But it returns ZERO all the time.

I can get read the row number from Session, when it is inside the WRITE method but it is always ZERO inside the READ Method. Isn't Session data global and readable from any method?

var ExcelCount = HttpContext.Session.GetInt32("ProductExcelCount") ?? 0;

//CONTROLLER 
//WRITE
[HttpPost]
public IActionResult DownloadExcel([FromBody] filter)
{
    HttpContext.Session.SetInt32("ProductExcelCount", 0);
    //......
    //Code incremates progressed row number
    HttpContext.Session.SetInt32("ProductExcelCount", rowNo);
    //.....
    var FileName = $"Template.xlsx";
    return File(content, contentType, FileName);
}

//READ 
public JsonResult DownloadProgress()
{
    var ExcelCount = HttpContext.Session.GetInt32("ProductExcelCount") ?? 0;
    return Json(new { downloadData = new { ExcelCount } });
}

//JavaScript Read Session
"downloadData": function() {
    getFileFromTheServer();
    setInterval(this.downloadProgress, 20000);
},
"downloadProgress": function() {
    getProgressFromTheServer();
},
Mustafa Özçetin
  • 1,893
  • 1
  • 14
  • 16
AliAzra
  • 889
  • 1
  • 9
  • 28

1 Answers1

0

What you are trying to do might not work because I believe that values stored in the HTTP session data store cannot be accessed by another request until the request that sets them has finished.

See this Stack Overflow answer for how to implement a progress bar.

YungDeiza
  • 3,128
  • 1
  • 7
  • 32