There are several ways to handle file download.
The best ways due to speed and Browser compatibility are third party upload/download products.
Two of the most popular are Persits ASPUpload and SA's Fileup.
These examples will show typical code associated with both.
For Persits:
To visit their Web Site Persits Software
Version 2 is a problem with some browsers - I suggest using version 3
on error resume next
Dim s_filename, s_filepath, s_fullpath
s_filename = Request.QueryString("file_name")
S_filepath = server.MapPath("sub_directory_name") & "\"
S_fullpath = filepath & filename
Set obj_Upload = Server.CreateObject("Persits.Upload")
if err = 0 then
obj_Upload.SendBinary fullpath, True, "application/octet-stream", True ' (version 3)
' obj_Upload.SendBinary fullpath, True, "application/octet-stream" '(version 2)
else
response.write(err.Description)
end if
if err then response.write("An Error has occurred - Please contact us - Error:" & err.Description)
set obj_Upload = nothing
For SA Fileup:
To visit their Web Site SoftArtisans
Dim s_filename, s_filepath, s_fullpath, oFS, oF
s_filename = Request.QueryString("file_name")
S_filepath = server.MapPath("sub_directory_name") & "\"
S_fullpath = filepath & filename
Response.Addheader "Content-Disposition", "Attachment; filename=" & filename
Response.ContentType = "application/pdf"
fullpath = filepath & filename
set oFS = Server.CreateObject("Scripting.FileSystemObject")
set oF = oFS.GetFile(fullpath)
Response.AddHeader "Content-Length", oF.Size
set oF = nothing
set oFS = nothing
Set download = Server.CreateObject("SoftArtisans.FileUp")
download.TransferFile fullpath
set download = nothing
Now if you don't have either of these or any other third party tools then we have two other ways to set your download.
Using simple script:
This works but it is slow to start with large files so your user may think your script is bad or that his browser died.
You also must use "application/octet-stream" as the mime type else the browser may open the file rather than download it.
ASP
on error resume next
filename = Request.QueryString("filenm")
Dim s_filename, s_filepath, s_fullpath
s_filepath = server.MapPath("catalog") & "\"
s_fullpath = filepath & filename
set oFS = Server.CreateObject("Scripting.FileSystemObject")
Set oF = oFS.OpenTextFile( fullpath, 1, False )
If oFS.FileExists( fullpath ) Then
Response.Addheader "Content-Disposition", "Attachment; filename=" & filename
Response.ContentType = "application/octet-stream"
While Not Bin.AtEndOfStream
Response.BinaryWrite( ChrB( Asc( Bin.Read( 1 ) ) ) )
Wend
Else
Response.Redirect( "file_error.asp?fln=" & fullpath )
End If
Bin.Close
Set Bin = Nothing
set oFS = nothing
Simple download using .NET
For this one you must save the script as .aspx in order for it to work and your server must support .net
It is almost perfect in operation as an alternative to third party software.
Dim s_filename, s_fullpath
s_filename = Request.QueryString("file_name")
s_fullpath = Request.PhysicalApplicationPath & "catalog/" & filename
Response.Addheader("Content-Disposition", "Attachment; filename=" & s_filename )
Response.ContentType = "application/pdf"
Response.WriteFile (s_fullpath)
PHP
Check our download section for a PHP download script.