I'm currently collecting ideas how to solve the following issue: I'm generating Report in realtime and returning it to the browser using the File
method.
public ActionResult GenerateReport()
{
var report = ... // Don't care, we get an object containing an Id and a byte array
var reportId = report.Id; // this is actually important
return File(report.Data, "..."); // Return data with some content type, filename etc.
}
When the action is executed, the browser will prompt the file download. But I'd also somehow like to transfer the new Id
to the Browser which I need for processing.
Do you have any idea how I could solve this using the common JavaScript (jQuery) and Web/ASP.NET/Ajax or whatever techniques?
Use cookies!
Add a cookie in your response and then have looping jquery code that looks for it. In that cookie you can add the id and stop the loop once it has been found. Then just delete it again.
For example I use the ActionFilter below to detect when a file has been processed for download, using the File actionresult just like you.
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
var key = filterContext.HttpContext.Request.QueryString["downloading"].ToString();
if (key != null)
{
var cookie = new HttpCookie(key);
cookie.Path = "/";
filterContext.HttpContext.Response.Cookies.Add(cookie);
}
}
I'm currently collecting ideas how to solve the following issue: I'm generating Report in realtime and returning it to the browser using the File
method.
public ActionResult GenerateReport()
{
var report = ... // Don't care, we get an object containing an Id and a byte array
var reportId = report.Id; // this is actually important
return File(report.Data, "..."); // Return data with some content type, filename etc.
}
When the action is executed, the browser will prompt the file download. But I'd also somehow like to transfer the new Id
to the Browser which I need for processing.
Do you have any idea how I could solve this using the common JavaScript (jQuery) and Web/ASP.NET/Ajax or whatever techniques?
Use cookies!
Add a cookie in your response and then have looping jquery code that looks for it. In that cookie you can add the id and stop the loop once it has been found. Then just delete it again.
For example I use the ActionFilter below to detect when a file has been processed for download, using the File actionresult just like you.
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
var key = filterContext.HttpContext.Request.QueryString["downloading"].ToString();
if (key != null)
{
var cookie = new HttpCookie(key);
cookie.Path = "/";
filterContext.HttpContext.Response.Cookies.Add(cookie);
}
}
0 commentaires:
Enregistrer un commentaire