Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 14 of 14
  1. #1
    Regular Coder
    Join Date
    Jun 2002
    Location
    Northern NJ
    Posts
    404
    Thanks
    0
    Thanked 1 Time in 1 Post

    forcing download

    If I use output.csv it forces the download but if I change the name to memorial.csv it doesn't. Can somebody please tell me how to force the download without naming everything output? Thank you, all help is appreciated.
    I would rather be a lion for a day than a lamb that lives forever.

  • #2
    New Coder
    Join Date
    Feb 2003
    Location
    WV
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I'm not sure if this is what you're asking or not, but here is some code I have found useful:

    <%
    '--------------------------------------------
    Response.Buffer = True
    Dim strFilePath, strFileSize, strFileName

    Const adTypeBinary = 1

    strFilePath = "C:\ whatever the path is "
    strFileSize = ... the size of file .. optional
    strFileName = the file na,e

    Response.Clear

    '8*******************************8
    ' Requires MDAC 2.5 to be stable
    ' I recommend MDAC 2.6 or 2.7
    '8*******************************8
    Set objStream = Server.CreateObject("ADODB.Stream")
    objStream.Open
    objStream.Type = adTypeBinary
    objStream.LoadFromFile strFilePath

    strFileType = lcase(Right(strFileName, 4))

    ' Feel Free to Add Your Own Content-Types Here
    Select Case strFileType
    Case ".asf"
    ContentType = "video/x-ms-asf"
    Case ".avi"
    ContentType = "video/avi"
    Case ".doc"
    ContentType = "application/msword"
    Case ".zip"
    ContentType = "application/zip"
    Case ".xls"
    ContentType = "application/vnd.ms-excel"
    Case ".gif"
    ContentType = "image/gif"
    Case ".jpg", "jpeg"
    ContentType = "image/jpeg"
    Case ".wav"
    ContentType = "audio/wav"
    Case ".mp3"
    ContentType = "audio/mpeg3"
    Case ".mpg", "mpeg"
    ContentType = "video/mpeg"
    Case ".rtf"
    ContentType = "application/rtf"
    Case ".htm", "html"
    ContentType = "text/html"
    Case ".asp"
    ContentType = "text/asp"
    Case Else
    'Handle All Other Files
    ContentType = "application/octet-stream"
    End Select


    Response.AddHeader "Content-Disposition", "attachment; filename= strFileName
    Response.AddHeader "Content-Length", strFileSize
    ' In a Perfect World, Your Client would also have UTF-8 as the default
    ' In Their Browser
    Response.Charset = "UTF-8"
    Response.ContentType = ContentType

    Response.BinaryWrite objStream.Read
    Response.Flush

    objStream.Close
    Set objStream = Nothing

    %>

  • #3
    Regular Coder
    Join Date
    Jun 2002
    Location
    Northern NJ
    Posts
    404
    Thanks
    0
    Thanked 1 Time in 1 Post
    Thanks, I think I figured out my problem.
    I would rather be a lion for a day than a lamb that lives forever.

  • #4
    Senior Coder
    Join Date
    Jun 2002
    Location
    41° 8' 52" N -95° 53' 31" W
    Posts
    3,660
    Thanks
    0
    Thanked 0 Times in 0 Posts
    That looks like the same code I used, it was the first thing I found on a search of google, lol.

    Works great, though.
    Former ASP Forum Moderator - I'm back!

    If you can teach yourself how to learn, you can learn anything. ;)

  • #5
    Regular Coder
    Join Date
    Sep 2002
    Location
    Adelaide, Australia
    Posts
    472
    Thanks
    3
    Thanked 0 Times in 0 Posts
    (sorry a bit delayed here )
    how would you make the above script have a query... i.e. download.asp?file=whatever.jpg ?
    i tried doing it with request but it didn't work

    it also gives me an error saying that the file could not be open. ?

    ?ta
    Last edited by reubenb; 06-23-2004 at 11:00 AM.

  • #6
    Senior Coder
    Join Date
    Jun 2002
    Location
    41° 8' 52" N -95° 53' 31" W
    Posts
    3,660
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hehe, one question at a time... first of all, to get the filename from the querystring you'd use:

    Code:
    <%
    Dim filename
    filename = Request.QueryString("file")
    Response.Write("filename: " & filename & "<br />" & vbCrLf) 'To make sure you have the right value
    %>
    Regarding it not being able to open the file, make sure that you have the correct path (writing out the full path to debug... also you might want to look up Server.MapPath() on google) and that you have permissions to the directory the file is in.

    Hope this helps.
    Former ASP Forum Moderator - I'm back!

    If you can teach yourself how to learn, you can learn anything. ;)

  • #7
    Regular Coder
    Join Date
    Sep 2002
    Location
    Bugaha, NE
    Posts
    330
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I'm creating a file download page for our site....and I'm actually using a set of code very similar to what code_gate recommended. We're having an issue though, with me copyinig a file into the stream from the file server, before spitting it back out to the user.

    For some reason, we can't get the rights set up correctly to allow the server access to the file system on the other server. It seems to me that if it can reference it on the same network, or by drive letter, it should work....but it won't.
    This is in spanish when you're not looking.

  • #8
    Regular Coder
    Join Date
    Sep 2002
    Location
    Adelaide, Australia
    Posts
    472
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Hi,
    the below is my code and it doens't seem to work properly?
    what am i doing wrong?
    also.. how do i make it so if the file is not found it gives them a http status of 404?

    Code:
    <%
    Response.Buffer = True
    Dim strFilePath, strFileSize, strFileName, filename
    
    Const adTypeBinary = 1
    
    filename = Request.QueryString("filename")
    strFilePath = "D:\Client Webs\xyz.com\" & filename
    'strFileSize = 
    strFileName = filename
    
    Response.Clear
    
    Set objStream = Server.CreateObject("ADODB.Stream")
    objStream.Open
    objStream.Type = adTypeBinary
    objStream.LoadFromFile strFilePath
    
    strFileType = lcase(Right(strFileName, 4))
    
    Select Case strFileType
    Case ".asf"
    ContentType = "video/x-ms-asf"
    Case ".avi"
    ContentType = "video/avi"
    Case ".doc"
    ContentType = "application/msword"
    Case ".zip"
    ContentType = "application/zip"
    Case ".xls"
    ContentType = "application/vnd.ms-excel"
    Case ".gif"
    ContentType = "image/gif"
    Case ".jpg", "jpeg"
    ContentType = "image/jpeg"
    Case ".wav"
    ContentType = "audio/wav"
    Case ".mp3"
    ContentType = "audio/mpeg3"
    Case ".mpg", "mpeg"
    ContentType = "video/mpeg"
    Case ".rtf"
    ContentType = "application/rtf"
    Case ".htm", "html"
    ContentType = "text/html"
    Case ".asp"
    ContentType = "text/asp"
    Case Else
    'Handle All Other Files
    ContentType = "application/octet-stream"
    End Select
    
    
    Response.AddHeader "Content-Disposition", "attachment; filename= strFileName"
    Response.AddHeader "Content-Length", strFileSize
    Response.Charset = "UTF-8"
    Response.ContentType = ContentType
    
    Response.BinaryWrite objStream.Read
    Response.Flush
    
    objStream.Close
    Set objStream = Nothing
    
    %>

  • #9
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,037
    Thanks
    0
    Thanked 250 Times in 246 Posts
    Try changing this line:

    objStream.LoadFromFile strFilePath

    to:
    Code:
    'enable error handling
    on error resume next
    objStream.LoadFromFile strFilePath
    if err.number<>0 then
       objStream.Close
       Set objStrem=nothing
       Reponse.Status="404 Not Found";
       Response.End
    end if
    'disable error handling
    on error goto 0

  • #10
    Regular Coder
    Join Date
    Sep 2002
    Location
    Adelaide, Australia
    Posts
    472
    Thanks
    3
    Thanked 0 Times in 0 Posts
    doesn't work at all now...
    Last edited by reubenb; 07-03-2004 at 08:57 AM.

  • #11
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,037
    Thanks
    0
    Thanked 250 Times in 246 Posts
    I had typos.
    Code:
    if err.number<>0 then
       objStream.Close
       Set objStream=nothing
       Response.Status="404 Not Found"; 'remove the semicolon
       Response.End
    end if
    But you said that the code doesn't work even without that modifications.

  • #12
    Regular Coder
    Join Date
    Sep 2002
    Location
    Adelaide, Australia
    Posts
    472
    Thanks
    3
    Thanked 0 Times in 0 Posts
    yeah.. just shows a blank page

  • #13
    Regular Coder
    Join Date
    Sep 2002
    Location
    Bugaha, NE
    Posts
    330
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Insert a debugging line inside your if err.number <> ""



    response.write "boo"


    If when running it, you see the word "boo", we know that it's finding an error and executing that bit of code.
    This is in spanish when you're not looking.

  • #14
    Senior Coder
    Join Date
    Apr 2003
    Location
    England
    Posts
    1,192
    Thanks
    5
    Thanked 13 Times in 13 Posts
    another way of forcing downloads can be done with fso but use the method said above unless you really cant for some reason.. fso needs some clever coding so that you do not memory out the server (close asap etc.. sometimes better done with dlls)

    anyway.. you can prob ignore everything i just said as the other method is better


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •