[英]ASP.NET download file using browser and redirect to another page at the same time
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. returnUrl
是服务器上文件的完整路径,因此我曾经通过这种方式通过浏览器下载标准对话框下载文件。 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.
重要说明是我不能使用像download.aspx这样的中间页面,该中间页面仅用于下载,然后重定向到必要的页面。 So, the approaches I used:
因此,我使用的方法是:
1. 1。
Response.Redirect(returnUrl, false);
Response.Redirect("/");
Result: the file does not downloading, immediate redirect happens. 结果:文件未下载,立即重定向发生。
2. 2。
Response.Redirect(returnUrl, false);
Response.Redirect("/", false);
Result: nothing happens. 结果:什么都没有发生。
3. 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();
当删除
Response.End();
redirect happens, but file is not loading. 重定向发生,但文件未加载。
Removed: Response.Redirect("/"); 删除:Response.Redirect(“ /”);
Result: File is downloading, but no refresh/redirect happens. 结果:文件正在下载,但没有刷新/重定向发生。
Response.Write("language='javascript'>window.location.assign('returnUrl','download', '');"); Response.Write(“ language ='javascript'> window.location.assign('returnUrl','download','');”); Response.Redirect("/");
的Response.Redirect( “/”);
Result: File is not loading, redirect happens. 结果:文件未加载,发生重定向。 Replaced
Response.Write
parameters with "<script>alert('test');</script>"
. 用
"<script>alert('test');</script>"
替换了Response.Write
参数。 No alert appears, redirect happens. 没有警报出现,重定向发生。
Do you know, what's the problem? 你知道是什么问题吗?
I've got help on asp.net forum, right here . 我在这里的 asp.net论坛上获得了帮助。 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. 现在问题解决了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.