访问TempData变量

I'm using ajax to make a post request to my server side, I am creating a file, once that file has been created, I need to display it in my browser, I've read that using ajax is not possible to display that file, but I can use my success event in my ajax function to call another method in my server side, I've been trying to store that file as an array of bytes in memory using TempData or even Session variables, and then read them on the other method I'm calling on ajax success(called DisplayFile) , but the TempData variables are always null when the DisplayFile method is called... If you have different suggestions to do this are welcome, I've been thinking in send the array of bytes to JQuery and then make another request passing it as a parameter but it is a very long array of bytes.., I'm not sure why the TempData value is null when DisplayFile method is called from client side...

$.ajax({ url: "/FileManagement/Files",
         type: 'post',
         data: formData,
         success: function(result){
           window.location = "/FileManagement/DisplayFile"
}
})

public JsonResult Files(//parameters...)
{
//...some code
 TempData["BytesArray"] = fileBytes; //fileBytes is an array of bytes
 TempData["FileName"] = fileName;  
}

public ActionResult DisplayFile()
{
var file = TempData["BytesArray"];  //null
var fileName = TempData["FileName"];
}

UPDATE It's working now with Session variables instead of TempData, maybe it's because this controller is inside the Areas folder? This controller is not inside the Controllers folder, maybe it has to do with that, in order for Session to work I had to use the complete path to it like: System.Web.HttpContext.Current.Session["test"] = "asasa"; I'm still trying to figure out why with TempData the value is null when the second method is called...

the way you did was correct , you should be able to access the tempdata in the further get request ,even though it is from ajax.

See this accepted answer , which almost related to what you want

:https://forums.asp.net/t/1911620.aspx?How+to+pass+tempdata+to+controller+from+view+

casting may be a problem for you. try that.

Hope it will be helpful

Thanks Karthik

I finally ended up using Session variables, I'm using a controller inside Areas folder, so maybe because of that TempData isn't working the way I expect, for Session variables in order to work on this controller I have to use the complete path to it, while in a controller inside a Controllers folder I don't need to use that path..

public JsonResult Files(//parameters...)
{
//...some code
  System.Web.HttpContext.Current.Session["FileInBytes"] = bytesArray; //fileBytes is an array of bytes
  System.Web.HttpContext.Current.Session["FileName"] = fileName;;  
}

public ActionResult DisplayFile()
{
   var fileInBytes = Session["FileInBytes"] as byte[];
   var fileName = Session["FileName"] as string;
   Session.Remove("FileInBytes");
   Session.Remove("FileName"); 
}