ASP page to open/download a file

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Matt

    ASP page to open/download a file

    I want to write a ASP page to open/download a file

    In fileview.jsp, I have the file browse, and when user click submit button,
    fileview2.asp should open the file.

    fileview.asp
    ============
    <FORM ACTION="filevie w2.asp" method="POST">
    <P><input type="FILE" name="filename" >
    <P><input type="submit">

    fileview2.asp
    ============
    <%
    String filename = Request.Form("f ilename")
    Response.Redire ct = filename
    %>

    However, this is not working. any ideas? please advise what I am missing.
    thanks!!


  • Ray Costanzo [MVP]

    #2
    Re: ASP page to open/download a file

    With what you seem to be decribing, the action that you want to take place
    are:

    1. User load page with form.
    2. User browses for and selects a file.
    3. User submits form.
    4. File is uploaded to server.
    5. Server SAVES file to file system.
    6. Browser is redirected to this saved file.

    First of all, "String filename = Request.Form("f ilename")" isn't valid code.
    "filename = Request.Form("f ilename")" would be okay, but that'd just return
    the file path that was in the <input type=file> box.

    To get the actual file uploaded and saved to the server, you need to use an
    upload component.¹ You'd want to use that component to save the file to the
    file system and then redirect the user to it.

    <sidenote>Why do you want to do this, anyway? It's kinda odd. If I want to
    open a file that exists on my file system, my natural reaction would not be
    to upload the file to a web site so that it can then turn around and give it
    right back to me. But, to each his own.</sidenote>

    For handling file uploads, see here:

    Before anything server-side will work for you, make note of the ENCTYPE
    value of the form tag.

    --------------------------------

    Now I'm beginning to thing that you're trying to redirect the user to the
    file on his machine. Is this true? If so, I also find this odd, but that's
    fine. :] If that's the case, you can do something like this:

    <%
    if request.form("f ilename") <> "" Then response.redire ct
    request.form("f ilename")
    %>

    <form method="post" action="default .asp">
    <input type="file" name="filename" />
    <input type="submit" />
    </form>


    Ray at home

    ¹ You can process file uploads without a component and just use pure
    VB/Jscripting, but advise against this.


    "Matt" <mattloude@hotm ail.com> wrote in message
    news:u0tKCgikEH A.896@TK2MSFTNG P12.phx.gbl...[color=blue]
    > I want to write a ASP page to open/download a file
    >
    > In fileview.jsp, I have the file browse, and when user click submit[/color]
    button,[color=blue]
    > fileview2.asp should open the file.
    >
    > fileview.asp
    > ============
    > <FORM ACTION="filevie w2.asp" method="POST">
    > <P><input type="FILE" name="filename" >
    > <P><input type="submit">
    >
    > fileview2.asp
    > ============
    > <%
    > String filename = Request.Form("f ilename")
    > Response.Redire ct = filename
    > %>
    >
    > However, this is not working. any ideas? please advise what I am missing.
    > thanks!!
    >
    >[/color]


    Comment

    • Steven Burn

      #3
      Re: ASP page to open/download a file

      Where is the file you are trying to access?

      If it's on your server..... check to make sure the path is correct before
      re-directing

      If it's on the client machine, you've no hope of using redirecting as it
      needs to be uploaded to your server first.

      --

      Regards

      Steven Burn
      Ur I.T. Mate Group


      Keeping it FREE!


      "Matt" <mattloude@hotm ail.com> wrote in message
      news:u0tKCgikEH A.896@TK2MSFTNG P12.phx.gbl...[color=blue]
      > I want to write a ASP page to open/download a file
      >
      > In fileview.jsp, I have the file browse, and when user click submit[/color]
      button,[color=blue]
      > fileview2.asp should open the file.
      >
      > fileview.asp
      > ============
      > <FORM ACTION="filevie w2.asp" method="POST">
      > <P><input type="FILE" name="filename" >
      > <P><input type="submit">
      >
      > fileview2.asp
      > ============
      > <%
      > String filename = Request.Form("f ilename")
      > Response.Redire ct = filename
      > %>
      >
      > However, this is not working. any ideas? please advise what I am missing.
      > thanks!!
      >
      >[/color]


      Comment

      • Ray Costanzo [MVP]

        #4
        Re: ASP page to open/download a file

        ..Redirect actually sends an instruction to the browser in the header of the
        response, so you can tell a browser to redirect to a file that is local to
        the machine. :]

        Ray at home

        "Steven Burn" <pvt@noyb.com > wrote in message
        news:%232dcrmik EHA.3016@tk2msf tngp13.phx.gbl. ..[color=blue]
        > Where is the file you are trying to access?
        >
        > If it's on your server..... check to make sure the path is correct before
        > re-directing
        >
        > If it's on the client machine, you've no hope of using redirecting as it
        > needs to be uploaded to your server first.
        >
        > --
        >
        > Regards
        >
        > Steven Bu[/color]


        Comment

        • Steven Burn

          #5
          Re: ASP page to open/download a file

          hehe, woops.... (wasn't aware of that one (though to be honest, I've never
          had a reason to try it so shouldn't have said there's no hope in the first
          place :o\ )).

          Ty for the correction ;o)

          --

          Regards

          Steven Burn
          Ur I.T. Mate Group


          Keeping it FREE!


          "Ray Costanzo [MVP]" <my first name at lane 34 dot commercial> wrote in
          message news:uxWoytikEH A.3896@TK2MSFTN GP15.phx.gbl...[color=blue]
          > .Redirect actually sends an instruction to the browser in the header of[/color]
          the[color=blue]
          > response, so you can tell a browser to redirect to a file that is local to
          > the machine. :]
          >
          > Ray at home
          >
          > "Steven Burn" <pvt@noyb.com > wrote in message
          > news:%232dcrmik EHA.3016@tk2msf tngp13.phx.gbl. ..[color=green]
          > > Where is the file you are trying to access?
          > >
          > > If it's on your server..... check to make sure the path is correct[/color][/color]
          before[color=blue][color=green]
          > > re-directing
          > >
          > > If it's on the client machine, you've no hope of using redirecting as it
          > > needs to be uploaded to your server first.
          > >
          > > --
          > >
          > > Regards
          > >
          > > Steven Bu[/color]
          >
          >[/color]


          Comment

          • Bob Barrows [MVP]

            #6
            Re: ASP page to open/download a file

            Really? Can you give an example of this? I just made a quick try and
            received a "page not found" error. If this is true, its' a huge security
            hole isn't it? What's to prevent a hacker from redirecting to an executable
            on the user''s machine (if it exists).

            Bob

            Ray Costanzo [MVP] wrote:[color=blue]
            > .Redirect actually sends an instruction to the browser in the header
            > of the response, so you can tell a browser to redirect to a file that
            > is local to the machine. :]
            >
            > Ray at home
            >
            > "Steven Burn" <pvt@noyb.com > wrote in message
            > news:%232dcrmik EHA.3016@tk2msf tngp13.phx.gbl. ..[color=green]
            >> Where is the file you are trying to access?
            >>
            >> If it's on your server..... check to make sure the path is correct
            >> before re-directing
            >>
            >> If it's on the client machine, you've no hope of using redirecting
            >> as it needs to be uploaded to your server first.
            >>
            >> --
            >>
            >> Regards
            >>
            >> Steven Bu[/color][/color]

            --
            Microsoft MVP - ASP/ASP.NET
            Please reply to the newsgroup. This email account is my spam trap so I
            don't check it very often. If you must reply off-line, then remove the
            "NO SPAM"


            Comment

            • Ray Costanzo [MVP]

              #7
              Re: ASP page to open/download a file

              Put this in a page and load it, as an example that'll let you specify what
              local file to be redirected to.

              <%
              if request.form("f ilename") <> "" Then response.redire ct
              request.form("f ilename")
              %>

              <form method="post" action="default .asp">
              <input type="file" name="filename" />
              <input type="submit" />
              </form>

              If you browse to an executable, you'll get prompted to download.
              Response.Redire ct "C:\windows\not epad.exe"

              Ray at home

              "Bob Barrows [MVP]" <reb01501@NOyah oo.SPAMcom> wrote in message
              news:epdsK3nkEH A.3016@tk2msftn gp13.phx.gbl...[color=blue]
              > Really? Can you give an example of this? I just made a quick try and
              > received a "page not found" error. If this is true, its' a huge security
              > hole isn't it? What's to prevent a hacker from redirecting to an[/color]
              executable[color=blue]
              > on the user''s machine (if it exists).
              >
              > Bob[/color]


              Comment

              • Bob Barrows [MVP]

                #8
                Re: ASP page to open/download a file

                Hmm, surprisingly, nothing happens. I thought I would get an error, but the
                page simply reloads when I click Submit. I used Response.Write to verify
                that the page was submitting and it was.

                Here's the code:

                <%
                if request.form("f ilename") <> "" Then
                Response.Write request.form("f ilename") & "<BR>"
                response.redire ct request.form("f ilename")
                end if
                %>
                <html>
                <BODY>
                <form method="post" action="default .asp" id=form1 name=form1>
                <input type="file" name="filename" style="WIDTH: 500px; HEIGHT: 22px"
                size=28>
                <input type="submit" value="Submit Query">
                </FORM></BODY></HTML>

                If I comment out the redirect, the response.write occurs.
                When I uncomment it, it seems like the default.asp page reloads

                Bob Barrows


                Ray Costanzo [MVP] wrote:[color=blue]
                > Put this in a page and load it, as an example that'll let you specify
                > what local file to be redirected to.
                >
                > <%
                > if request.form("f ilename") <> "" Then response.redire ct
                > request.form("f ilename")
                > %>
                >
                > <form method="post" action="default .asp">
                > <input type="file" name="filename" />
                > <input type="submit" />
                > </form>
                >
                > If you browse to an executable, you'll get prompted to download.
                > Response.Redire ct "C:\windows\not epad.exe"
                >
                > Ray at home
                >
                > "Bob Barrows [MVP]" <reb01501@NOyah oo.SPAMcom> wrote in message
                > news:epdsK3nkEH A.3016@tk2msftn gp13.phx.gbl...[color=green]
                >> Really? Can you give an example of this? I just made a quick try and
                >> received a "page not found" error. If this is true, its' a huge
                >> security hole isn't it? What's to prevent a hacker from redirecting
                >> to an executable on the user''s machine (if it exists).
                >>
                >> Bob[/color][/color]

                --
                Microsoft MVP - ASP/ASP.NET
                Please reply to the newsgroup. This email account is my spam trap so I
                don't check it very often. If you must reply off-line, then remove the
                "NO SPAM"


                Comment

                • Steven Burn

                  #9
                  Re: ASP page to open/download a file

                  I have to same problem Bob........ :o\

                  --

                  Regards

                  Steven Burn
                  Ur I.T. Mate Group


                  Keeping it FREE!


                  "Bob Barrows [MVP]" <reb01501@NOyah oo.SPAMcom> wrote in message
                  news:eD2AYhpkEH A.3756@TK2MSFTN GP11.phx.gbl...[color=blue]
                  > Hmm, surprisingly, nothing happens. I thought I would get an error, but[/color]
                  the[color=blue]
                  > page simply reloads when I click Submit. I used Response.Write to verify
                  > that the page was submitting and it was.
                  >
                  > Here's the code:
                  >
                  > <%
                  > if request.form("f ilename") <> "" Then
                  > Response.Write request.form("f ilename") & "<BR>"
                  > response.redire ct request.form("f ilename")
                  > end if
                  > %>
                  > <html>
                  > <BODY>
                  > <form method="post" action="default .asp" id=form1 name=form1>
                  > <input type="file" name="filename" style="WIDTH: 500px; HEIGHT: 22px"
                  > size=28>
                  > <input type="submit" value="Submit Query">
                  > </FORM></BODY></HTML>
                  >
                  > If I comment out the redirect, the response.write occurs.
                  > When I uncomment it, it seems like the default.asp page reloads
                  >
                  > Bob Barrows
                  >
                  >
                  > Ray Costanzo [MVP] wrote:[color=green]
                  > > Put this in a page and load it, as an example that'll let you specify
                  > > what local file to be redirected to.
                  > >
                  > > <%
                  > > if request.form("f ilename") <> "" Then response.redire ct
                  > > request.form("f ilename")
                  > > %>
                  > >
                  > > <form method="post" action="default .asp">
                  > > <input type="file" name="filename" />
                  > > <input type="submit" />
                  > > </form>
                  > >
                  > > If you browse to an executable, you'll get prompted to download.
                  > > Response.Redire ct "C:\windows\not epad.exe"
                  > >
                  > > Ray at home
                  > >
                  > > "Bob Barrows [MVP]" <reb01501@NOyah oo.SPAMcom> wrote in message
                  > > news:epdsK3nkEH A.3016@tk2msftn gp13.phx.gbl...[color=darkred]
                  > >> Really? Can you give an example of this? I just made a quick try and
                  > >> received a "page not found" error. If this is true, its' a huge
                  > >> security hole isn't it? What's to prevent a hacker from redirecting
                  > >> to an executable on the user''s machine (if it exists).
                  > >>
                  > >> Bob[/color][/color]
                  >
                  > --
                  > Microsoft MVP - ASP/ASP.NET
                  > Please reply to the newsgroup. This email account is my spam trap so I
                  > don't check it very often. If you must reply off-line, then remove the
                  > "NO SPAM"
                  >
                  >[/color]


                  Comment

                  • Ray Costanzo [MVP]

                    #10
                    Re: ASP page to open/download a file

                    So, your page also redirected me to my local file. I was testing it at
                    http://test/localredirect/bob.asp. I then went to
                    http://test.mydomain.local/localredirect/bob.asp, and I got the same results
                    you did. So, hmm, I guess IE will redirect to a local file if done so from
                    the intranet zone but not from the Internet zone! Interesting...

                    Ray at home

                    "Bob Barrows [MVP]" <reb01501@NOyah oo.SPAMcom> wrote in message
                    news:eD2AYhpkEH A.3756@TK2MSFTN GP11.phx.gbl...[color=blue]
                    > Hmm, surprisingly, nothing happens. I thought I would get an error, but[/color]
                    the[color=blue]
                    > page simply reloads when I click Submit. I used Response.Write to verify
                    > that the page was submitting and it was.
                    >
                    > Here's the code:
                    >
                    > <%
                    > if request.form("f ilename") <> "" Then
                    > Response.Write request.form("f ilename") & "<BR>"
                    > response.redire ct request.form("f ilename")
                    > end if
                    > %>
                    > <html>
                    > <BODY>
                    > <form method="post" action="default .asp" id=form1 name=form1>
                    > <input type="file" name="filename" style="WIDTH: 500px; HEIGHT: 22px"
                    > size=28>
                    > <input type="submit" value="Submit Query">
                    > </FORM></BODY></HTML>
                    >
                    > If I comment out the redirect, the response.write occurs.
                    > When I uncomment it, it seems like the default.asp page reloads
                    >
                    > Bob Barrows[/color]


                    Comment

                    • Bob Barrows [MVP]

                      #11
                      Re: ASP page to open/download a file

                      Strange. I'm doing it on my localhost, which is shown in the browser as
                      "Local Intranet".

                      I'm using XP SP2 so maybe that security hole has been closed.

                      Bob

                      Ray Costanzo [MVP] wrote:[color=blue]
                      > So, your page also redirected me to my local file. I was testing it
                      > at http://test/localredirect/bob.asp. I then went to
                      > http://test.mydomain.local/localredirect/bob.asp, and I got the same
                      > results you did. So, hmm, I guess IE will redirect to a local file
                      > if done so from the intranet zone but not from the Internet zone!
                      > Interesting...
                      >
                      > Ray at home
                      >
                      > "Bob Barrows [MVP]" <reb01501@NOyah oo.SPAMcom> wrote in message
                      > news:eD2AYhpkEH A.3756@TK2MSFTN GP11.phx.gbl...[color=green]
                      >> Hmm, surprisingly, nothing happens. I thought I would get an error,
                      >> but the page simply reloads when I click Submit. I used
                      >> Response.Write to verify that the page was submitting and it was.
                      >>
                      >> Here's the code:
                      >>
                      >> <%
                      >> if request.form("f ilename") <> "" Then
                      >> Response.Write request.form("f ilename") & "<BR>"
                      >> response.redire ct request.form("f ilename")
                      >> end if
                      >> %>
                      >> <html>
                      >> <BODY>
                      >> <form method="post" action="default .asp" id=form1 name=form1>
                      >> <input type="file" name="filename" style="WIDTH: 500px; HEIGHT: 22px"
                      >> size=28>
                      >> <input type="submit" value="Submit Query">
                      >> </FORM></BODY></HTML>
                      >>
                      >> If I comment out the redirect, the response.write occurs.
                      >> When I uncomment it, it seems like the default.asp page reloads
                      >>
                      >> Bob Barrows[/color][/color]

                      --
                      Microsoft MVP - ASP/ASP.NET
                      Please reply to the newsgroup. This email account is my spam trap so I
                      don't check it very often. If you must reply off-line, then remove the
                      "NO SPAM"


                      Comment

                      • Ray Costanzo [MVP]

                        #12
                        Re: ASP page to open/download a file

                        I'm now at work where I have gotten around to installing SP2, and same
                        thing, it does not redirect me to my local file, Internet, intranet, or
                        trusted sites zone.

                        On a W2K Advanced Server SP 4, redirecting to a local file works. And
                        surprisingly, it also redirects on Windows Server 2003.

                        Ray at work

                        "Bob Barrows [MVP]" <reb01501@NOyah oo.SPAMcom> wrote in message
                        news:%23axDtypk EHA.2500@TK2MSF TNGP09.phx.gbl. ..[color=blue]
                        > Strange. I'm doing it on my localhost, which is shown in the browser as
                        > "Local Intranet".
                        >
                        > I'm using XP SP2 so maybe that security hole has been closed.
                        >
                        > Bob
                        >
                        > Ray Costanzo [MVP] wrote:[color=green]
                        >> So, your page also redirected me to my local file. I was testing it
                        >> at http://test/localredirect/bob.asp. I then went to
                        >> http://test.mydomain.local/localredirect/bob.asp, and I got the same
                        >> results you did. So, hmm, I guess IE will redirect to a local file
                        >> if done so from the intranet zone but not from the Internet zone!
                        >> Interesting...
                        >>
                        >> Ray at home
                        >>
                        >> "Bob Barrows [MVP]" <reb01501@NOyah oo.SPAMcom> wrote in message
                        >> news:eD2AYhpkEH A.3756@TK2MSFTN GP11.phx.gbl...[/color][/color]


                        Comment

                        • Bob Barrows [MVP]

                          #13
                          Re: ASP page to open/download a file

                          You're right: on my W2K work machine, it did browse to the local file.
                          Incredible.

                          Bob

                          Bob Barrows
                          Ray Costanzo [MVP] wrote:[color=blue]
                          > I'm now at work where I have gotten around to installing SP2, and same
                          > thing, it does not redirect me to my local file, Internet, intranet,
                          > or trusted sites zone.
                          >
                          > On a W2K Advanced Server SP 4, redirecting to a local file works. And
                          > surprisingly, it also redirects on Windows Server 2003.
                          >
                          > Ray at work
                          >
                          > "Bob Barrows [MVP]" <reb01501@NOyah oo.SPAMcom> wrote in message
                          > news:%23axDtypk EHA.2500@TK2MSF TNGP09.phx.gbl. ..[color=green]
                          >> Strange. I'm doing it on my localhost, which is shown in the browser
                          >> as "Local Intranet".
                          >>
                          >> I'm using XP SP2 so maybe that security hole has been closed.
                          >>
                          >> Bob
                          >>
                          >> Ray Costanzo [MVP] wrote:[color=darkred]
                          >>> So, your page also redirected me to my local file. I was testing it
                          >>> at http://test/localredirect/bob.asp. I then went to
                          >>> http://test.mydomain.local/localredirect/bob.asp, and I got the same
                          >>> results you did. So, hmm, I guess IE will redirect to a local file
                          >>> if done so from the intranet zone but not from the Internet zone!
                          >>> Interesting...
                          >>>
                          >>> Ray at home
                          >>>
                          >>> "Bob Barrows [MVP]" <reb01501@NOyah oo.SPAMcom> wrote in message
                          >>> news:eD2AYhpkEH A.3756@TK2MSFTN GP11.phx.gbl...[/color][/color][/color]

                          --
                          Microsoft MVP - ASP/ASP.NET
                          Please reply to the newsgroup. This email account is my spam trap so I
                          don't check it very often. If you must reply off-line, then remove the
                          "NO SPAM"


                          Comment

                          Working...