Solved

Redirecting after downloading files using Response.WriteFile

Posted on 2007-12-05
16
2,278 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
  • 5
  • 3
  • 3
  • +3
16 Comments
 
LVL 12

Expert Comment

by:needo_jee
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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

708 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now