下载(Excel导出)与通用处理器没有物理写入文件,无会话变量可能吗? [英] Download (Excel Export) with Generic Handler without physically writing a file and without session variable possible?

查看:143
本文介绍了下载(Excel导出)与通用处理器没有物理写入文件,无会话变量可能吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个GridView,并希望ASP.NET Web应用程序页面上,并且要导出数据到Excel。

I have a GridView and want to on an ASP.NET Web Application page and want to export the data to excel.

如果没有的UpdatePanel我用下面的code完美工作:

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();
            }
        }

使用的UpdatePanel,这是不再可能,所以我觉得我必须使用一个通用处理器(* ashx的)。

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?

我也不想phyiscally写,因为这只是使用一次的用户硬盘上的文件。

I also don't want to phyiscally write the file on hdd because it's only used one time by the user.

有什么建议?

推荐答案

您可以做一个处理程序(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.

这篇关于下载(Excel导出)与通用处理器没有物理写入文件,无会话变量可能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆