Download (Excel Export) with Generic Handler without physically writing a file and without session variable possible?
I have a GridView and want to on an ASP.NET Web Application page and want to export the data to excel.
Without UpdatePanels I worked perfectly with the following code:
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.AddHeader(
"content-disposition", string.Format("attachment; filename={0}", fileName));
HttpContext.Current.Response.ContentType = "application/msexcel";
using (StringWriter sw = new StringWriter())
{
using (HtmlTextWriter htw = new HtmlTextWriter(sw))
{
// The GridView stuff.
// [...]
// render the htmlwriter into the response
HttpContext.Current.Response.Write(sw.ToString());
HttpContext.Current.Response.End();
}
}
With Upd开发者_JAVA百科atePanels this is no longer possible so I think I have to use a Generic Handler (*.ashx).
Is it somehow possible to pass the data to the generic handler without using session variables or is this the only way of doing this?
I also don't want to phyiscally write the file on hdd because it's only used one time by the user.
Any suggestions?
You can make a handler (ashx) which accepts a querystring, in the querystring you can put the filename of the download file. When you want to have security on the files (not all file are available for all users), you have to do some extra work in the querystring or put some info in the session on which files are available for download.
精彩评论