Redirecting after downloading files using Response.WriteFile

I have a button on a web page that calls the following code in the aspx.vb file:

Dim strFile As String = "xxxxx.pdf"
Dim path As String = Server.MapPath(strFile)
Dim file As System.IO.FileInfo = New System.IO.FileInfo(path)
Response.Clear()
Response.AddHeader("Content-Disposition", "attachment; filename=" & file.Name)
Response.AddHeader("Content-Length", file.Length.ToString())
Response.ContentType = "application/octet-stream"
Response.WriteFile(file.FullName)

I would like to redirect the user to a 'thank you' page after the download but if I insert a response.redirect statement after the response.writefile statement neither the writefile nor the redirect work.  Can anyone tell me how I can resolve this?

Thanks
LVL 2
nicksbellAsked:
Who is Participating?
 
codeclayConnect With a Mentor Commented:
nicksbell,

Another alternative is to redirect the user to another page when he clicks submit .
That page will have an IFrame as shown below .
========================================================================
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="FileDonloadMain.aspx.vb" Inherits="FileDonloadMain" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
    <script language="javascript">
        function SetIframeSource(){
            var iFrame = document.getElementById("downloadFrame");
            iFrame.src = "FileDownload.aspx";
           
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <iframe id="downloadFrame" runat=server width="1" height="1" frameborder=no >
        </iframe>
        <asp:Button ID="btnDownload" runat="server" Text="Download" /></div>
    </form>
</body>
</html>
======================================================================

This will be your Thank you page . The Iframe src is being set from javascript for demostration purposes . In real time you can set it in the aspx itself like
<iframe id="downloadFrame" runat=server width="1" height="1" frameborder=no  src="FileDownload.aspx">
        </iframe>

The FileDownload.aspx will contain your code in the Page Load . So the file will get downloaded without closing the window , which will contain the "Thank You" message .
0
 
Munawar HussainPrincipal Software EngineerCommented:
Dim strFile As String = "xxxxx.pdf"
Dim path As String = Server.MapPath(strFile)
Dim file As System.IO.FileInfo = New System.IO.FileInfo(path)
Response.Clear()
Response.AddHeader("Content-Disposition", "attachment; filename=" & file.Name)
Response.AddHeader("Content-Length", file.Length.ToString())
Response.ContentType = "application/octet-stream"
Response.WriteFile(file.FullName)
Response.Flush();
Response.Redirect("Thankyou.aspx")
0
 
Göran AnderssonCommented:
You can't do that. One response can only contain one document. You can only send a file or a redirection page, not both.

To both do the download and the redirect, you need to send two requests to the server, but then you don't need the redirect, you can just request the new page directly.

Use Javascript on the page to open a new window for the download, and then request the page for the current window:

window.open('DoTheDownload.aspx', '_blank');
window.location = 'Gratitude.aspx';
0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

 
nicksbellAuthor Commented:
GreenGhost, is it possible to do this from within the aspx.vb code?  At the moment the user fills out a form first then they click a button.  The form details are written to a database and then the code above is called to download the file.  I am not sure exactly where I would put your javascript...could it be run from within the aspx.vb file after my database write code?
0
 
Göran AnderssonCommented:
You could use the Page.ClientScript.RegisterStartupScript method to add a script that would run when the page loads in the browser:

Page.ClientScript.RegisterStartupScript(Page.GetType(), "DL", "window.open('DoTheDownload.aspx', '_blank');window.location = 'Gratitude.aspx';", True)

Another alternative would be to simply put the Javascript that opens the download in the thankyou page, so that you would just redirect to the thankyou page.
0
 
nicksbellAuthor Commented:
codeclay, thanks, that did the trick.

GreenGhost, I tried your solution but the window.open caused issues with popup blocker so was not ideal unfortunately.  But thanks for your replies anyway.
0
 
Göran AnderssonCommented:
I was surprised not to see a split on this question...
0
 
Keith AlabasterEnterprise ArchitectCommented:
If you feel a split was in order, post a comment on the Community Support board and a Moderator or the Zone advisor for this area can review it for you.

Keith Alabaster
Networking Zone Advisor
0
 
codeclayCommented:
I dont see any need of a split here . Green Ghost posted a couple of suggestions .Not a Solutions . Mine was not based on assumptions .I provided the complete code and the user is happy with it .
0
 
Keith AlabasterEnterprise ArchitectCommented:
Understood - It is a fact of life though that sometimes an expert may feel that his contribution has been overlooked. My comment is simply the process that should be followed to get independent arbitration in these circumstances.

Keith

0
 
nicksbellAuthor Commented:
I have to say I agree with codeclay.  I thought the purpose of the point system was to award points to the post containing the solution to my problem.  The suggestion provided by GreenGhost was not a workable solution and is not one that I would recommend to any other solution hunters that may happen across this post.  That is not to say that I don't appreciate his attempts but if people were to award points just for experts replying then how would the user be able to decipher workable from unworkable solutions.
0
 
Göran AnderssonCommented:
> Green Ghost posted a couple of suggestions .Not a Solutions .

That is not true. I suggested three different solutions.

> Mine was not based on assumptions .

Are you implying that mine were?

> The suggestion provided by GreenGhost was not a workable solution and is not one that I would recommend to any other solution hunters that may happen across this post.

Why do you think that it "was not a workable solution"? I suggested three different, fully functional solutions, of which the third one was the one that codeclay implemented. I don't understand how the solution is perfectly valid when codeclay suggests it, ut "not a workable solution" when I suggest it?
0
 
Vee_ModCommented:
Review by Zone Advisors and Page Editors requested.


Vee_Mod
Experts Exchange Moderator
0
 
Vee_ModCommented:
Following a ZAPE review, no changes are recommended.
I suggest any further discussion be done in a new post in the Experts Lounge.
http://www.experts-exchange.com/Other/Expert_Lounge/

Vee_Mod
Experts Exchange Moderator
0
 
Keith AlabasterEnterprise ArchitectCommented:
Thanks Vee
0
 
Göran AnderssonCommented:
Posted a new question in the Experts Lounge, as requested.
http://www.experts-exchange.com/Other/Expert_Lounge/Q_23011525.html
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.