ASP.NET 2.0 PageComplete事件问题和文件下载
我试图在整个 .aspx 页面显示后执行一个操作。 “action”是一个使用 Response 对象向用户发送文件的函数。
更多详细信息:
我试图从侧边栏复制页面上链接的行为。 IE 我在主页上有一个用于“导出”操作的链接,并且它工作正常 - 因为该页面在用户单击它之前就已经显示了。 但是,当用户在侧边栏上单击链接时,它应该将他们带回此主页,然后在显示后发送文件。
我做了一些研究,认为使用 PageComplete 事件非常适合此目的,因此我创建了事件处理程序,并将对导出代码的调用(从侧边栏加载时从查询字符串中删除)放入 PageComplete 中事件处理程序。 但它的行为方式是一样的——弹出浏览器下载框,并且页面在之前或之后都不会加载。
如果有助于理解我在这里所做的事情,那就是用于将列表发送给用户的代码片段。
Response.Clear();
Response.BufferOutput = true;
Response.ContentType = "application/ms-excel";
Response.AppendHeader("content-disposition", "attachment;filename=MyList.xls");
Response.Write(listManager.ExportLists(mycode));
Response.End();
我更喜欢使用页面事件来加载页面的方法,而不是修改这个逻辑。 但是,如果有一种干净且更简单的方法来发送文件,并且它允许加载页面然后发送文件,那也很好。
除了 PageComplete 之外,我还可以使用其他页面事件吗?或者我是否可能遗漏了某些内容?
编辑:抱歉冗长。 我意识到我无法改变 HTTP 请求的工作方式 - 我只是在寻找一个可接受的解决方案,以实现或多或少相同的结果。 似乎要采取的方法是在几秒钟后强制刷新页面(从而确保它在执行文件下载代码之前加载)——所以我正在寻找一种方法来做到这一点作为第一个答案建议-刷新下载。 (如果有一种无需等待即可刷新的方法,也不必延迟)
为什么这段代码不起作用?
private void Page_LoadComplete(object sender, System.EventArgs e)
{
if (Request.QueryString["action"] != null)
{
if (Request.QueryString["action"] == "export")
{
Response.Redirect("MyHome.aspx?action=exportnow", false);
}
if (Request.QueryString["action"] == "exportnow")
{
ExportMasterList();
}
}
}
它应该做什么:页面加载完成后,执行 Response.Redirect,使用不同的查询字符串重新加载自身。 当再次到达Page LoadComplete事件时,第二次将触发写出文件的函数。
它实际上做了什么:显然重复了同样的问题两次......它又回到了同样的问题,如何在页面加载后执行操作,或者等到页面完全加载完成,然后触发刷新以执行行动? ASP.NET 是否无法在用户不单击某些内容的情况下自行执行某些操作?
如果是这样的话,那么 2 秒后自动刷新也是可以接受的......但我不知道该怎么做。
I am trying to place an action to happen after an entire .aspx page displays. The "action" is a function that uses the Response object to send a file to the user.
More detailed information:
I am trying to replicate the behavior of a link on the page, from a sidebar. I.E. I have a link on the main page for the Export action, and it works fine -- since the page is already displayed before the user clicks it. But when the user is on a sidebar, clicks the link, it should take them back to this main page and then send the file after it displays.
I did some research and thought that using the PageComplete event would be well-suited for this, so I created my event handler and put the call to the export code (it keys off of a query string when loaded from the sidebar) inside my PageComplete event handler. But it behaves just the same way - the browser download box pops up and the page is never loaded before or after.
If it helps to understand what I'm doing here is a snippet of the code used to send the list to the user.
Response.Clear();
Response.BufferOutput = true;
Response.ContentType = "application/ms-excel";
Response.AppendHeader("content-disposition", "attachment;filename=MyList.xls");
Response.Write(listManager.ExportLists(mycode));
Response.End();
I would prefer a way to use a page event to load the page, rather than tinkering with this logic. But, if there is a clean and easier way to get the file sent, and it allows for loading the page then sending the file, that would be fine too.
Is there another Page event I can use besides PageComplete, or is it possible I am missing something?
EDIT: Sorry about the verbosity. I realize that I can't change the way HTTP requests work - I'm only looking for an acceptable solution that achieves more or less the same results. It seems like the way to go is to force a refresh of the page after a couple of seconds (thus ensuring that it loads before the file download code is executed) -- so I am looking for a way to do this as the first answer suggests - refresh to a download. (It doesn't have to be delayed either, if there's a way to refresh with no waiting)
Why doesn't this code work?
private void Page_LoadComplete(object sender, System.EventArgs e)
{
if (Request.QueryString["action"] != null)
{
if (Request.QueryString["action"] == "export")
{
Response.Redirect("MyHome.aspx?action=exportnow", false);
}
if (Request.QueryString["action"] == "exportnow")
{
ExportMasterList();
}
}
}
What it's supposed to do: after page loading is complete, do a Response.Redirect, reloading itself with a different query string. When it reaches the Page LoadComplete event again, the second time it will trigger the function which writes out the file.
What it actually does: Apparently repeats the same problem twice... it goes back to the same problem, how do you execute an action after the page loads, or wait until the page completely finishes loading, then trigger a refresh which will execute the action? Is there no way for ASP.NET to do something by itself without the user clicking on something?
If that's the case, then an auto-refresh after 2 seconds would also be acceptable... but I'm not sure how to do that.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
服务器只能返回一个对象给用户,一个文件下载或者一个页面。 您可以管理的最好方法是将用户返回到刷新以下载文件的页面。
The server can only return one object to the user, a file download or a page. The best you can manage is to return the user to a page that refreshes to a file download.