Download dialog for known MIME type

Discussion in 'ASP General' started by mizi, Oct 30, 2005.

  1. mizi

    mizi Guest

    I am writing an ASP page for downloading some files, such as jpg, doc
    which are known MIME type. I want to force user save these files through
    a "file download" dialog instead of open them on the IE by default. I
    search the Internet and found the best way to do it is by using stream.
    The following is my code for the download.asp. There are 2 problems, one
    is when I use firefox and click the link to download.asp, I found the
    system become slower and slower, it seems the CPU usage become very high.
    Another problem is when I use the right button of my mice on the link to
    download.asp, and choose "save as", it will not save the doc file, it
    saves download.asp. Please help me, thanks!

    Server.ScriptTimeOut=999999
    '--declare variables
    Dim strAbsFile
    Dim strFileExtension
    Dim objFSO
    Dim objFile
    Dim objStream
    '-- set absolute file location
    strAbsFile = Server.MapPath(sPath)
    '-- create FSO object to check if file exists and get
    properties
    Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
    '-- check to see if the file exists
    If objFSO.FileExists(strAbsFile) Then
    Set objFile = objFSO.GetFile(strAbsFile)
    '-- first clear the response, and then set the
    appropriate headers
    Response.Clear
    '-- the filename you give it will be the one that is
    shown
    ' to the users by default when they save
    Response.AddHeader "Content-Disposition", "attachment;
    filename=" + objFile.name
    Response.AddHeader "Content-Length", objFile.Size
    Response.ContentType = "application/octet-stream"
    Set objStream = Server.CreateObject("ADODB.Stream")
    objStream.Open
    '-- set as binary
    objStream.Type = 1
    Response.CharSet = "UTF-8"
    '-- load into the stream the file
    objStream.LoadFromFile(strAbsFile)
    '-- send the stream in the response
    binData = objStream.Read
    Response.BinaryWrite(binData)
    Response.Flush
    objStream.Close
    Set objStream = Nothing
    Set objFile = Nothing
    Else 'objFSO.FileExists(strAbsFile)
    Response.Clear
    Response.Write("File does not exist!")
    End If
    Set objFSO = Nothing
     
    mizi, Oct 30, 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. CJ
    Replies:
    1
    Views:
    1,602
    Andrew Thompson
    Oct 29, 2004
  2. Bret Thompson

    Downloading files of known mime type

    Bret Thompson, Jul 8, 2005, in forum: ASP General
    Replies:
    3
    Views:
    210
    Bret Thompson
    Jul 8, 2005
  3. Jan Arickx
    Replies:
    0
    Views:
    216
    Jan Arickx
    Aug 25, 2003
  4. joe
    Replies:
    0
    Views:
    212
  5. FAQ server
    Replies:
    44
    Views:
    879
Loading...

Share This Page