bvdm23
asked on
Force to download a file
How can I force a file to be downloaded with ASP without using 3rd party components?
ASKER
This works great for *.txt, *.asp,.. files, but what if you don't know in advance if it is a PDF file, a PPT file, a DOC file or an XLS file you wish to download(dynamically pass the path of this file)?
In case you don't know the contenttype you can use "application/x-msdownload" as the content type.
Here's part of a file-download script i wrote once
Select Case sFileType
Case ".asf"
sContentType = "video/x-ms-asf"
Case ".avi"
sContentType = "video/avi"
Case ".doc"
sContentType = "application/msword"
Case ".zip"
sContentType = "application/zip"
Case ".css"
sContentType = "text/css"
Case ".pdf"
sContentType = "application/pdf"
Case ".xls"
sContentType = "application/vnd.ms-excel"
Case ".gif"
sContentType = "image/gif"
Case ".jpg", "jpeg"
sContentType = "image/jpeg"
Case ".wav"
sContentType = "audio/wav"
Case ".mp3"
sContentType = "audio/mpeg3"
Case ".mpg", "mpeg"
sContentType = "video/mpeg"
Case ".rtf"
sContentType = "application/rtf"
Case ".htm", "html"
sContentType = "text/html"
Case ".asp", ".asa"
sContentType = "text/asp"
Case Else
sContentType = "application/x-msdownload"
End Select
Greetings
Here's part of a file-download script i wrote once
Select Case sFileType
Case ".asf"
sContentType = "video/x-ms-asf"
Case ".avi"
sContentType = "video/avi"
Case ".doc"
sContentType = "application/msword"
Case ".zip"
sContentType = "application/zip"
Case ".css"
sContentType = "text/css"
Case ".pdf"
sContentType = "application/pdf"
Case ".xls"
sContentType = "application/vnd.ms-excel"
Case ".gif"
sContentType = "image/gif"
Case ".jpg", "jpeg"
sContentType = "image/jpeg"
Case ".wav"
sContentType = "audio/wav"
Case ".mp3"
sContentType = "audio/mpeg3"
Case ".mpg", "mpeg"
sContentType = "video/mpeg"
Case ".rtf"
sContentType = "application/rtf"
Case ".htm", "html"
sContentType = "text/html"
Case ".asp", ".asa"
sContentType = "text/asp"
Case Else
sContentType = "application/x-msdownload"
End Select
Greetings
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I'll accept the answer from markhoy, but as soon as I have 50 points I'll give them TomasBracke.
1. VERSION ONE:
<%
Set objFSO = Server.CreateObject("Scrip
Set objTS = objFSO.OpenTextFile("locat
Response.ContentType = "application/octet-stream"
response.AddHeader "Content-Disposition", "attachment;filename="&req
Response.Buffer = True
Do While Not objTS.AtEndOfStream
strChunk = objTS.Read(32)
strTmp = ""
For i = 1 to Len(strChunk)
strTmp = strTmp & ChrB(Asc(Mid(strChunk, i, 1)))
Next
Response.BinaryWrite strTmp
Response.Flush
Loop
objTS.Close
Set objTS = Nothing
Set objFSO = Nothing
%>
2. VERSION 2:
Here is a nice article that should help you clear up this question;
http://www.aspemporium.com/aspEmporium/tutorials/vbs_download.asp
But in the mean time, (since you are not sure if you are downloading binary or text data), here is a nice little script that uses the ADO Stream method;
<%
DownloadFileName = Request.QueryString("FileN
DownloadFolder = "d:\test"
BaseFileName = Split(DownloadFileName, ".")
Set ObjFile = Server.CreateObject("Scrip
FilePath = Server.MapPath(DownloadFol
'Define path to file named in ASP format
InitFileName = FilePath & "\" & BaseFileName(0) & ".asp"
'Define path for temporary download file
DownloadFileName = FilePath & "\" & Replace(Request.ServerVari
'Copy file from .ASP to .PDF for downloading
If ObjFile.FileExists(InitFil
ObjFile.CopyFile InitFileName, DownloadFileName
%>
<%
Set objStream = Server.CreateObject("ADODB
objStream.Type = adTypeBinary
objStream.Open
'Read the file
objStream.LoadFromFile DownloadFileName
'Send the data to the browser
Response.Expires = 0
Response.Buffer = True
Response.ContentType = "application/text"
Response.Addheader "Content-Disposition", "inline; filename=" & "stream.txt"
Response.Addheader "Content-Length", objStream.size
Response.BinaryWrite(objSt
'Delete temporary download file and close objects
ObjFile.DeleteFile DownloadFileName
objStream.Close
Set objStream = Nothing
Set objFile = Nothing
Else
Response.Write "File not found...."
End if
Response.End()
%>
3. VERSION 3:
asp example
http://www.genasis.net/code/download.asp
jsp example
http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=2183&lngWId=2
4. VERSION 4:
FTP with java to download file
http://forum.java.sun.com/forum?13@165.tfueaxn1bvv^0@.ee85f09/0
http://forum.java.sun.com/forum?13@165.tfueaxn1bvv^0@.ee77fb6/0
http://developer.java.sun.com/developer/techDocs/SearchData/qa/about_javas_ftp.html
5. VERSIION 5
If you use Content-Disposition in your header, the browser will ask the user what he wants to do with the file;
<%@ Language=VBScript %>
<%
fileName = "Text.txt"
'ContentType specifies the MIME type of this header.
Response.ContentType = "text/plain"
'The AddHeader method adds an HTML header with a specified value.
'Content-disposition forces the browser to download.
Response.AddHeader "content-disposition", "attachment; filename=""" & fileName & """"
Response.End
%>