By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,795 Members | 1,761 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,795 IT Pros & Developers. It's quick & easy.

Redirect after file download - VB.NET - ASP.NET

P: 56
I have a form which allows the user to download a simple text file. They click on the download button and it brings up the "Save As" dialog. Once they save the file, I want them to be redirected to another page.

Here is the code I'm using:

Expand|Select|Wrap|Line Numbers
  1.             Response.ContentType = "text/plain"
  2.             Response.AppendHeader("Content-Disposition", "attachment; filename=Security.PCC")
  3.             Response.TransmitFile(PassGen.Path & "Security.pcc")
  4.             Response.End()
  5.             Response.Redirect("password.aspx?pw=" & password)
  6.  
  7.  
I've tried switching around the .End and .Redirect statements. Either it redirects, but the file doesn't download, or it downloads but doesn't redirect. What am I missing?
Mar 12 '08 #1
Share this Question
Share on Google+
9 Replies


jeffstl
Expert 100+
P: 432
Take out the response.end

That stops all execution on the spot.

Plus with the redirect you dont need it at all..
Mar 12 '08 #2

P: 56
Thanks. But when I try that, the "save' dialog never shows. It justs goes directory to the redirect url. I tried adding the extra parameter to the redirect as well

Expand|Select|Wrap|Line Numbers
  1.                 Response.ContentType = "text/plain"
  2.                 Response.AppendHeader("Content-Disposition", "attachment; filename=Security.PCC")
  3.                 Response.TransmitFile(PassGen.Path & "Security.pcc")
  4.                 Response.Redirect("password.aspx?pw=" & password, True)
  5.  
Mar 12 '08 #3

jeffstl
Expert 100+
P: 432
Thanks. But when I try that, the "save' dialog never shows. It justs goes directory to the redirect url. I tried adding the extra parameter to the redirect as well

Expand|Select|Wrap|Line Numbers
  1.                 Response.ContentType = "text/plain"
  2.                 Response.AppendHeader("Content-Disposition", "attachment; filename=Security.PCC")
  3.                 Response.TransmitFile(PassGen.Path & "Security.pcc")
  4.                 Response.Redirect("password.aspx?pw=" & password, True)
  5.  

Hmm. I have never actually done a transmitFile before so I guess Im not sure.

It sounds like you need a buffer or something?

Or you need to somehow be able to somehow have a timer for the redirect....I will tell you I did a quick scan on google after I saw your response and it doesn't look like you are alone in this problem ;-)
Mar 12 '08 #4

jhardman
Expert 2.5K+
P: 3,405
I think you are seeing a basic internet deficiency. I would suggest opening the file in a new window, and have the same function which opens the file redirect the page. It would be easier to do in javascript, I think.

Jared
Mar 17 '08 #5

P: 56
Thanks Jared. I had tried that. It worked, but not as efficiently as I wanted to. I almost have it working now. The whole reason I wanted to redirect was to display a password for the user. I'm trying now just to post the password to the same page as the download.

It seems like it should be working, but its not. Basically, I have this control ('PassGen') that creates a text file and also a password for that file. I want to display the password to the user and have them be able to download the file.

Expand|Select|Wrap|Line Numbers
  1. Private Sub generate_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  2.  
  3.         If Not String.IsNullOrEmpty(Session("password")) Then
  4.             lblPassword.Text = "Your password is            " & Session("password") 'display password
  5.             Session("password") = String.Empty 'reset the password
  6.         Else
  7.             PassGen.Path = System.AppDomain.CurrentDomain.BaseDirectory() 'set path to create file
  8.             password = PassGen.CreatePasswordFile 'create text file and password
  9.             Session("password") = password 'set session password
  10.         End If
  11.  
  12.     End Sub
  13.  
  14.     Protected Sub btnDownload_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDownload.Click
  15.  
  16.         If System.IO.File.Exists(filename) Then
  17.             Response.ContentType = "text/plain"
  18.             Response.AppendHeader("Content-Disposition", "attachment; filename=Security.PCC")
  19.             Response.TransmitFile(PassGen.Path & "Security.pcc")
  20.             Response.End()
  21.         End If
  22.  
  23.     End Sub
  24.  
  25.  
Basically if the password is blank, then the file hasn't been generated, so I go ahead and generate it. And when they click on the download button, the generate_load is called again, which should then display the message. When I step through it, it hits that line of code but never displays the password, which I don't understand. Is there a better way to do this?
Mar 18 '08 #6

jhardman
Expert 2.5K+
P: 3,405
Matt,

Looking at your code, it is apparent that this is not ASP classic. Give me some details (what version and language you are using, what IDE) and I will try to recommend an expert to help.

Jared
Mar 18 '08 #7

P: 56
I'm using VS 2005. I'm more of a VB.NET guy, so I'm working mostly with the VB behind the ASP
Mar 19 '08 #8

jhardman
Expert 2.5K+
P: 3,405
I'm using VS 2005. I'm more of a VB.NET guy, so I'm working mostly with the VB behind the ASP
OK, I'm moving this post to the .NET forum, the ASP forum focuses on "classic" ASP and even though experts crossover, we try to keep the threads separate. If someone here doesn't pick up on it, try sending a PM to me or to Frinavale (the leader of the .NET forum) - I think this would be close to her specialty.

Jared
Mar 19 '08 #9

P: 56
Jared,

Thank you very much for doing that. But I think I figured out a way to get it to work the way I wanted. And I also found that the reason the label caption wasn't being updated had something to do with the download function. When I commented out the download code, the label updated fine. Here is what I'm doing now:

Expand|Select|Wrap|Line Numbers
  1.  If Not Page.IsPostBack Then
  2.             If System.IO.File.Exists(filename) Then
  3.                 System.IO.File.Delete(filename)
  4.             End If
  5.             PassGen.Path = System.AppDomain.CurrentDomain.BaseDirectory() 'set path to create file
  6.             password = PassGen.CreatePasswordFile 'create text file and password
  7.             lblPassword.Text = "Your password is " & vbTab & password '& ".  It came from the path: " & filename
  8.         End If
  9.  
  10.     End Sub
  11.  
  12.     Protected Sub btnDownload_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDownload.Click
  13.  
  14.         If System.IO.File.Exists(filename) Then
  15.             Response.ContentType = "text/plain"
  16.             Response.AddHeader("Refresh", 0.1)
  17.             Response.AppendHeader("Content-Disposition", "attachment; filename=Security.PCC")
  18.             Response.TransmitFile(PassGen.Path & "Security.pcc")
  19.             Response.End()
  20.         End If
  21.  
  22.     End Sub
  23.  
  24.  
Basically I just display the password first on form load and then have them download. Ideally I wanted to show the password after they hit the download button, but its not that big of a deal. Thanks again for your help :)
Mar 20 '08 #10

Post your reply

Sign in to post your reply or Sign up for a free account.