Download dialog for known MIME type

M

mizi

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top