ASP.NET 使用浏览器下载文件并同时重定向到另一个页面 [英] ASP.NET download file using browser and redirect to another page at the same time
问题描述
前段时间我在授权例程中有以下代码:
protected void Login1_LoggedIn(object sender, EventArgs e){string returnUrl = Request.QueryString["ReturnUrl"];if (!string.IsNullOrEmpty(returnUrl)){//从服务器下载文件Response.Redirect(returnUrl);}}
returnUrl
是服务器上文件的完整路径,所以我以前通过浏览器下载标准对话框下载文件.但是现在我需要在授权后立即将用户重定向到另一个页面,同时下载文件.所以现在我需要类似的东西
<代码>{//从服务器下载文件Response.Redirect(returnUrl);Response.Redirect("/");}
我找不到正确的方法.我尝试了很多方法,但没有任何帮助.重要的一点是我不能使用像 download.aspx 这样的中间页面,它只用于下载然后重定向到必要的页面.所以,我使用的方法:
1.
Response.Redirect(returnUrl, false);Response.Redirect("/");
结果:文件没有下载,立即重定向发生.
2.
Response.Redirect(returnUrl, false);Response.Redirect("/", false);
结果:什么都没有发生.
3.
Response.Clear();Response.ClearHeaders();Response.ClearContent();Response.ContentType = "应用程序/八位字节流";Response.AddHeader("Content-Disposition", "attachment; filename=" + returnUrl);Response.TransmitFile(Server.MapPath("~" + returnUrl));Response.End();Response.Redirect("/");
结果:文件已下载,但没有重定向.当 remove Response.End();
重定向发生,但文件未加载.
- 具有以下更改的先前变体:添加:Response.AddHeader("Refresh", "3; URL=/home.aspx");
已删除:Response.Redirect("/");
结果:文件正在下载,但没有发生刷新/重定向.
- 使用 javascript 下载文件:
Response.Write("language='javascript'>window.location.assign('returnUrl','download','');");Response.Redirect("/");
结果:文件未加载,发生重定向.用 "<script>alert('test');</script>"
替换了 Response.Write
参数.没有警报出现,重定向发生.
你知道有什么问题吗?
我在 asp.net 论坛上得到了帮助,对 这里.我已将以下代码放置在必须重定向授权用户的页面上:
<%--我们使用 iframe 和下面的脚本供非授权用户通过直接链接下载文件--%><script runat="服务器">protected void Page_Load(object sender, EventArgs e){iframeFile.Attributes["src"] = Request.QueryString["file"];}<asp:Content ContentPlaceHolderID="masterContent" runat="server"><div><iframe id="iframeFile" runat="server" style="display: none;"></iframe>
在登录页面的代码隐藏中,我编写了以下用于下载文件的片段:
string returnUrl = Request.QueryString["ReturnUrl"];if (!string.IsNullOrEmpty(returnUrl)){Response.Redirect("/?file=" + Server.UrlEncode(returnUrl));}
现在问题解决了.
Some time ago I had the following code in authorization routine:
protected void Login1_LoggedIn(object sender, EventArgs e)
{
string returnUrl = Request.QueryString["ReturnUrl"];
if (!string.IsNullOrEmpty(returnUrl))
{
// download file from server
Response.Redirect(returnUrl);
}
}
returnUrl
is a full path to the file on the server, so I used to download the file via browser download standard dialog this way.
But now I need to redirect user to another page right after authorization, at the same time when downloading file. So now I need something like
{
// download file from server
Response.Redirect(returnUrl);
Response.Redirect("/");
}
I cannot find the way I can do that correctly. I tried a lot of approaches, but nothing helped. The important note is I cannot use the intermediate page like download.aspx that is used only for downloading and then redirects to the necessary page. So, the approaches I used:
1.
Response.Redirect(returnUrl, false);
Response.Redirect("/");
Result: the file does not downloading, immediate redirect happens.
2.
Response.Redirect(returnUrl, false);
Response.Redirect("/", false);
Result: nothing happens.
3.
Response.Clear();
Response.ClearHeaders();
Response.ClearContent();
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment; filename=" + returnUrl);
Response.TransmitFile(Server.MapPath("~" + returnUrl));
Response.End();
Response.Redirect("/");
Result: File is downloaded, but no redirect. When remove Response.End();
redirect happens, but file is not loading.
- The previous variant with following changes: Added: Response.AddHeader("Refresh", "3; URL=/home.aspx");
Removed: Response.Redirect("/");
Result: File is downloading, but no refresh/redirect happens.
- Used javascript for downloading file:
Response.Write("language='javascript'>window.location.assign('returnUrl','download', '');"); Response.Redirect("/");
Result: File is not loading, redirect happens.
Replaced Response.Write
parameters with "<script>alert('test');</script>"
. No alert appears, redirect happens.
Do you know, what's the problem?
I've got help on asp.net forum, right here. I have placed the following code on the page the authorized user must be redirected:
<%--We use iframe and script below for downloading files by not authorized users via direct links--%>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
iframeFile.Attributes["src"] = Request.QueryString["file"];
}
</script>
<asp:Content ContentPlaceHolderID="masterContent" runat="server">
<div>
<iframe id="iframeFile" runat="server" style="display: none;"></iframe>
</div>
In code-behind of sign in page I've written the following fragment for downloading files:
string returnUrl = Request.QueryString["ReturnUrl"];
if (!string.IsNullOrEmpty(returnUrl))
{
Response.Redirect("/?file=" + Server.UrlEncode(returnUrl));
}
Now the problem is solved.
这篇关于ASP.NET 使用浏览器下载文件并同时重定向到另一个页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!