Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Redirecting after downloading files using Response.WriteFile

Posted on 2007-12-05
16
Medium Priority
?
2,541 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 2000 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
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.…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

670 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