Link to home
Start Free TrialLog in
Avatar of bvdm23
bvdm23

asked on

Force to download a file

How can I force a file to be downloaded with ASP without using 3rd party components?
Avatar of markhoy
markhoy
Flag of United Kingdom of Great Britain and Northern Ireland image

yes. set the content disposition and it will download:

 


1. VERSION ONE:
 
<%
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
Set objTS = objFSO.OpenTextFile("location of your file")

Response.ContentType = "application/octet-stream"
response.AddHeader "Content-Disposition", "attachment;filename="&request("filename")
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("FileName")
DownloadFolder = "d:\test"
BaseFileName = Split(DownloadFileName, ".")

Set ObjFile = Server.CreateObject("Scripting.FileSystemObject")
FilePath = Server.MapPath(DownloadFolder)

'Define path to file named in ASP format
InitFileName = FilePath & "\" & BaseFileName(0) & ".asp"

'Define path for temporary download file
DownloadFileName = FilePath & "\" & Replace(Request.ServerVariables("REMOTE_ADDR"),".","") & ".txt"

'Copy file from .ASP to .PDF for downloading
If ObjFile.FileExists(InitFileName) then
ObjFile.CopyFile InitFileName, DownloadFileName
%>

<%
Set objStream = Server.CreateObject("ADODB.Stream")
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(objStream.Read(adReadAll))

'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
%>
 
Avatar of bvdm23
bvdm23

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
ASKER CERTIFIED SOLUTION
Avatar of markhoy
markhoy
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of bvdm23

ASKER

I'll accept the answer from markhoy, but as soon as I have 50 points I'll give them TomasBracke.