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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Matt Nunnally
    New Member
    • Jul 2007
    • 56

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

    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:

    Code:
                Response.ContentType = "text/plain"
                Response.AppendHeader("Content-Disposition", "attachment; filename=Security.PCC")
                Response.TransmitFile(PassGen.Path & "Security.pcc")
                Response.End()
                Response.Redirect("password.aspx?pw=" & password)
    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?
  • jeffstl
    Recognized Expert Contributor
    • Feb 2008
    • 432

    #2
    Take out the response.end

    That stops all execution on the spot.

    Plus with the redirect you dont need it at all..

    Comment

    • Matt Nunnally
      New Member
      • Jul 2007
      • 56

      #3
      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

      Code:
                      Response.ContentType = "text/plain"
                      Response.AppendHeader("Content-Disposition", "attachment; filename=Security.PCC")
                      Response.TransmitFile(PassGen.Path & "Security.pcc")
                      Response.Redirect("password.aspx?pw=" & password, True)

      Comment

      • jeffstl
        Recognized Expert Contributor
        • Feb 2008
        • 432

        #4
        Originally posted by Matt Nunnally
        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

        Code:
                        Response.ContentType = "text/plain"
                        Response.AppendHeader("Content-Disposition", "attachment; filename=Security.PCC")
                        Response.TransmitFile(PassGen.Path & "Security.pcc")
                        Response.Redirect("password.aspx?pw=" & password, True)

        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 ;-)

        Comment

        • jhardman
          Recognized Expert Specialist
          • Jan 2007
          • 3405

          #5
          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

          Comment

          • Matt Nunnally
            New Member
            • Jul 2007
            • 56

            #6
            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.

            Code:
            Private Sub generate_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            
                    If Not String.IsNullOrEmpty(Session("password")) Then
                        lblPassword.Text = "Your password is            " & Session("password") 'display password
                        Session("password") = String.Empty 'reset the password
                    Else
                        PassGen.Path = System.AppDomain.CurrentDomain.BaseDirectory() 'set path to create file
                        password = PassGen.CreatePasswordFile 'create text file and password
                        Session("password") = password 'set session password
                    End If
            
                End Sub
            
                Protected Sub btnDownload_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDownload.Click
            
                    If System.IO.File.Exists(filename) Then
                        Response.ContentType = "text/plain"
                        Response.AppendHeader("Content-Disposition", "attachment; filename=Security.PCC")
                        Response.TransmitFile(PassGen.Path & "Security.pcc")
                        Response.End()
                    End If
            
                End Sub
            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?

            Comment

            • jhardman
              Recognized Expert Specialist
              • Jan 2007
              • 3405

              #7
              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

              Comment

              • Matt Nunnally
                New Member
                • Jul 2007
                • 56

                #8
                I'm using VS 2005. I'm more of a VB.NET guy, so I'm working mostly with the VB behind the ASP

                Comment

                • jhardman
                  Recognized Expert Specialist
                  • Jan 2007
                  • 3405

                  #9
                  Originally posted by Matt Nunnally
                  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

                  Comment

                  • Matt Nunnally
                    New Member
                    • Jul 2007
                    • 56

                    #10
                    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:

                    Code:
                     If Not Page.IsPostBack Then
                                If System.IO.File.Exists(filename) Then
                                    System.IO.File.Delete(filename)
                                End If
                                PassGen.Path = System.AppDomain.CurrentDomain.BaseDirectory() 'set path to create file
                                password = PassGen.CreatePasswordFile 'create text file and password
                                lblPassword.Text = "Your password is " & vbTab & password '& ".  It came from the path: " & filename
                            End If
                    
                        End Sub
                    
                        Protected Sub btnDownload_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDownload.Click
                    
                            If System.IO.File.Exists(filename) Then
                                Response.ContentType = "text/plain"
                                Response.AddHeader("Refresh", 0.1)
                                Response.AppendHeader("Content-Disposition", "attachment; filename=Security.PCC")
                                Response.TransmitFile(PassGen.Path & "Security.pcc")
                                Response.End()
                            End If
                    
                        End Sub
                    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 :)

                    Comment

                    Working...