Sergey Shafiev
Sergey Shafiev

Reputation: 4375

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. 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.

  1. 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.

  1. 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?

Upvotes: 0

Views: 9167

Answers (1)

Sergey Shafiev
Sergey Shafiev

Reputation: 4375

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.

Upvotes: 5

Related Questions