Solved

Redirecting after downloading files using Response.WriteFile

Posted on 2007-12-05
16
2,472 Views
Last Modified: 2011-09-20
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
0
Comment
Question by:nicksbell
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 3
  • +3
16 Comments
 
LVL 12

Expert Comment

by:Munawar Hussain
ID: 20410890
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
 
LVL 29

Expert Comment

by:Göran Andersson
ID: 20410990
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
 
LVL 2

Author Comment

by:nicksbell
ID: 20411275
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 29

Expert Comment

by:Göran Andersson
ID: 20413241
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
 
LVL 9

Accepted Solution

by:
codeclay earned 500 total points
ID: 20413324
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
 
LVL 2

Author Closing Comment

by:nicksbell
ID: 31412828
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
 
LVL 29

Expert Comment

by:Göran Andersson
ID: 20419371
I was surprised not to see a split on this question...
0
 
LVL 51

Expert Comment

by:Keith Alabaster
ID: 20436647
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
 
LVL 9

Expert Comment

by:codeclay
ID: 20436659
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
 
LVL 51

Expert Comment

by:Keith Alabaster
ID: 20436669
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
 
LVL 2

Author Comment

by:nicksbell
ID: 20436678
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
 
LVL 29

Expert Comment

by:Göran Andersson
ID: 20436730
> 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
 
LVL 1

Expert Comment

by:Vee_Mod
ID: 20436732
Review by Zone Advisors and Page Editors requested.


Vee_Mod
Experts Exchange Moderator
0
 
LVL 1

Expert Comment

by:Vee_Mod
ID: 20436930
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
 
LVL 51

Expert Comment

by:Keith Alabaster
ID: 20436937
Thanks Vee
0
 
LVL 29

Expert Comment

by:Göran Andersson
ID: 20436970
Posted a new question in the Experts Lounge, as requested.
http://www.experts-exchange.com/Other/Expert_Lounge/Q_23011525.html
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Suggested Courses

632 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question