使用通用处理程序下载(Excel 导出),无需物理写入文件且无需会话变量?
我有一个 GridView,想要在 ASP.NET Web 应用程序页面上并将数据导出到 Excel。
如果没有 UpdatePanels,我可以完美地使用以下代码:
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();
}
}
有了 UpdatePanels,这不再可能,所以我认为我必须使用通用处理程序 (*.ashx)。
是否可以在不使用会话变量的情况下将数据传递给通用处理程序,或者这是执行此操作的唯一方法?
我也不想将文件物理写入硬盘,因为它只被用户使用一次。
有什么建议吗?
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 UpdatePanels 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以创建一个接受查询字符串的处理程序(ashx),在查询字符串中您可以放置下载文件的文件名。当您希望确保文件的安全性(并非所有文件都可供所有用户使用)时,您必须在查询字符串中做一些额外的工作,或者在会话中放置一些有关哪些文件可供下载的信息。
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.