在 C# 中调用 Response.End() 后重定向到另一个页面
我正在将 gridview 导出到 Excel,在 Web 应用程序中使用 .Net 4.0,在页面加载时需要生成文件,然后将页面重定向到调用页面。我遇到了问题,因为我导出到 Excel 的代码如下:
gvSummary.Style.Add("font-size", ".6em");
Response.Clear();
string attachment = "attachment; filename=filename.xls";
Response.ClearContent();
Response.AddHeader("content-disposition", attachment);
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
gvSummary.GridLines = GridLines.Horizontal;
gvSummary.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
我知道如果我将 Response.Redirect() 放在 .End() 之前,我将被重定向,但文件永远不会生成,并且如果我将.End() 之后的 Response.Redirect() 我得到了文件,但没有重定向。
上面编写的代码在生成文件时效果很好,但是在生成文件后,我仍然无法看到加载动画,因为我无法跳出页面。有什么想法吗?
I am exporting a gridview to excel, using .Net 4.0 in a web application, on page load and need for the file to be generated and then the page to be redirected to the calling page. I am running into issues because my code to export to excel is as follows:
gvSummary.Style.Add("font-size", ".6em");
Response.Clear();
string attachment = "attachment; filename=filename.xls";
Response.ClearContent();
Response.AddHeader("content-disposition", attachment);
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
gvSummary.GridLines = GridLines.Horizontal;
gvSummary.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
I know that if I put the Response.Redirect() before the .End(), I will get redirected but the file is never generated, and if I put the Response.Redirect() after the .End() I get the file but no redirection.
The code written above works just fine in generating the file, however when after the file is generated I am still stuck seeing my Loading animation because I can not break out of the page. Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我建议添加一个重定向标头。像这样的:
其中 3 是延迟时间,index.html 是您需要重定向到的 url
I suggest to add a redirect header. Something like this:
Where 3 is time of the delay and index.html is url you need to redirect to
问题是 Response.End() 将页面的最后几位发送到客户端以完成请求。在您的情况下,请求包含该文件。
一种可能的解决方案是打开一个新页面进行下载并将重定向保留在当前页面中。
The problem is Response.End() send the last bits of the page to the client in order to complete the request. In your case, the request contains the file.
One possible solution is be to open a new page for the download and keep the redirect in the current page.
我将
target="_blank"
添加到将我带到该页面的链接中。因此,当它在新窗口中打开时,它将 Excel 工作表发送到浏览器,并关闭页面,因为在 Response.End(); 之后没有其他事情可做,希望这会有所帮助。为我工作。
I added the
target="_blank"
to the link that takes me to the page. So when it opens in a new window, it send the excel sheet to the browser, and closes the page as it has nothing else to do after theResponse.End();
Hope this helps. Worked for me.
您无法同时打开这两个文件,因此您可能需要尝试在新窗口中设置指向生成的 xls 文件的链接或将其设置为 iframe 的源
You can't do both open so you might want to try setting the link to the generated xls file in a new window or as source to a iframe
在 JavaScript 中:
按钮:
In JavaScript:
button:
我尝试通过 ScriptManager 打开一个单独的页面,该页面将在原始页面中进行重定向时下载我的文件。
所以我在主页中添加了以下代码:
然后在 CommonDownload 页面的 Page_Load 方法中添加了下载代码。我通过会话传递了 CommonDownload 页面中所需的一些信息。
I tried opening a separate page through ScriptManager that will download my file while doing the redirect in my original page.
So I added below code in my main page:
And then added the download code in the Page_Load method of my CommonDownload page. I passed some of the info I needed in the CommonDownload page through through the session.
OnClientClick调用该方法会自动刷新。
OnClientClick call this method and it will automatically be refreshed.