Force Download fails when I select "Open" but works fine when I select "Save"

Discussion in 'ASP General' started by Michael, Jan 25, 2005.

  1. Michael

    Michael Guest

    I have a problem forcing files to download. If I select Save the document is
    saved with no problems. If I select "Open" the document is empty or I get a
    "File not found" error from the application opening the file...

    Code:

    function UTIL_ForceDownloadFile(strFileName, strFileSaveName)
    Response.Buffer = True
    Dim strFileSize
    Const adTypeBinary = 1
    Response.Clear

    '#Code Find File Size
    dim fso, fileObject
    Set fso = Server.CreateObject("Scripting.FileSystemObject")
    Set fileObject = fso.GetFile(Server.MapPath(strFileName))
    strFileSize = fileObject.Size
    Set fileObject = Nothing
    Set fso = Nothing

    dim objStream, strFileType, ContentType
    Set objStream = Server.CreateObject("ADODB.Stream")
    objStream.Open
    objStream.Type = adTypeBinary
    objStream.LoadFromFile Server.MapPath(strFileName)

    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 ".pdf"
    ContentType = "application/pdf"
    Case ".htm", "html"
    ContentType = "text/html"
    Case ".asp"
    ContentType = "text/asp"
    Case ".txt"
    ContentType = "text/plain"
    Case Else
    ContentType = "application/octet-stream"
    End Select

    Response.AddHeader "Content-Disposition", "attachment; filename=" &
    strFileSaveName
    Response.AddHeader "Content-Length", strFileSize

    Response.ContentType = ContentType
    Response.Charset = "UTF-8"
    Response.BinaryWrite objStream.Read
    Response.Flush
    objStream.Close
    Set objStream = Nothing
    response.end
    end function
     
    Michael, Jan 25, 2005
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Adam P
    Replies:
    0
    Views:
    492
    Adam P
    Sep 27, 2005
  2. Eric
    Replies:
    5
    Views:
    578
  3. Replies:
    3
    Views:
    818
    blackstaronline.net
    Apr 4, 2006
  4. tiewknvc9
    Replies:
    12
    Views:
    1,326
    Chris Uppal
    Feb 19, 2007
  5. JJA
    Replies:
    3
    Views:
    125
Loading...

Share This Page