ASP does force to download file

ASP logo ASP does force to download fileMany people asked me, how to provide option for downloading the file, such as MS-Word, MS-Excel, PDF and etc,. Here is solution, Use force download, instead of giveing direct link to file.

How it is works?

When you serve a document from a Web server, you might want to immediately prompt the user to save the file directly to the user’s disk, without opening it in the browser. However, for known MIME (Multipurpose Internet Mail Extensions) types such as Microsoft Word (“application/ms-word”), the default behavior is to open the document in Internet Explorer.

Requirements: II5 and above, MDAC 2.5, ASP 3

<%
‘——————————————–
Response.Buffer = True
Dim strFilePath, strFileSize, strFileName

Const adTypeBinary = 1

strFilePath = “Absloute path to file”
strFileSize = Size of the file, it is Optional
strFileName = “SampleFilename.pdf”

Response.Clear

Set objStream = Server.CreateObject(“ADODB.Stream”)
objStream.Open
objStream.Type = adTypeBinary
objStream.LoadFromFile strFilePath

strFileType = lcase(Right(strFileName, 4))

‘ Feel Free to Add Your Own Content-Types Here
Select Case strFileType
Case “.asf”
ContentType = “video/x-ms-asf”
Case “.avi”
ContentType = “video/avi”
Case “.doc”
ContentType = “application/msword”
Case “.zip”
ContentType = “application/zip”
Case “.xls”
ContentType = “application/vnd.ms-excel”
Case “.gif”
ContentType = “image/gif”
Case “.jpg”, “jpeg”
ContentType = “image/jpeg”
Case “.wav”
ContentType = “audio/wav”
Case “.mp3″
ContentType = “audio/mpeg3″
Case “.mpg”, “mpeg”
ContentType = “video/mpeg”
Case “.rtf”
ContentType = “application/rtf”
Case “.htm”, “html”
ContentType = “text/html”
Case “.asp”
ContentType = “text/asp”
Case Else
‘Handle All Other Files
ContentType = “application/octet-stream”
End Select

Response.AddHeader “Content-Disposition”, “attachment; filename= strFileName
Response.AddHeader “Content-Length”, strFileSize
‘ In a Perfect World, Your Client would also have UTF-8 as the default
‘ In Their Browser
Response.Charset = “UTF-8″
Response.ContentType = ContentType

Response.BinaryWrite objStream.Read
Response.Flush

objStream.Close
Set objStream = Nothing
%>

You can also add more content type, which ever you want.

 

Downloading any file using ASP, FSO and the ADODB Stream object

This is another way, we will see how to allow a user to download any file from our web server. They will see a prompt, giving them the option of opening or saving it, rather than simply opening it which is the default. We can achieve this using the FSO and ADODB objects.

There are times when you want users to download a file instead of opening it up in a browser; like images, text files, ASP code files, MS Office files like Powerpoint or Word files, etc. That’s easy to do for a ZIP file for example, but hard with a GIF or TXT. The reason is that anything that the browser recognizes as a valid format, it will open right away without giving you the option to save it. We want the user to receive a prompt though, asking them if they want to save or open the file.

On Error Resume Next
Dim strPath
strPath = CStr(Request.QueryString("file"))
'-- do some basic error checking for the QueryString
If strPath = "" Then
Response.Clear
Response.Write("No file specified.")
Response.End
ElseIf InStr(strPath, "..") > 0 Then
Response.Clear
Response.Write("Illegal folder location.")
Response.End
ElseIf Len(strPath) > 1024 Then
Response.Clear
Response.Write("Folder path too long.")
Response.End
Else
Call DownloadFile(strPath)
End If

Private Sub DownloadFile(file)
‘–declare variables
Dim strAbsFile
Dim strFileExtension
Dim objFSO
Dim objFile
Dim objStream
‘– set absolute file location
strAbsFile = Server.MapPath(file)
‘– 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
Response.BinaryWrite(objStream.Read)
objStream.Close
Set objStream = Nothing
Set objFile = Nothing
Else ‘objFSO.FileExists(strAbsFile)
Response.Clear
Response.Write(“No such file exists.”)
End If
Set objFSO = Nothing
End Sub
%>

Copy the code above and save it as download.asp on your web server.

To use this code, you link to the page, passing the location of the file you want to send to the user. For example:

The first part of the code does some basic error checking. The sub DownloadFile checks to see if the file is there, and if it is then it sends it as a binary stream using the ADODB Stream object. The header Content-Length is set so that the browser can properly display the progress bar.
Possible issues

If you are trying to stream a file bigger than 4MB, then IIS 6 might send back an error saying something to the sort of “Response Buffer Limit Exceeded”. This is a maximum file setting in the web server’s metabase.

Interested in writing Technology tips for SouthDreamZ.com?

Are you interested in writing Technology articles for SouthDreamZ.com or updating ASP does force to download file article? You can! SouthDreamZ.com is currently looking for writers who are interested in writing Technology articles. Send your articles to [email protected], for more details visit Feature Yourself page.
On this article, we have tried to provide all the information on ASP does force to download file. However, if you encounter any discrepancy in the information about ASP does force to download file, do write to us. We welcome any kind of feedback that would improve the quality of the site, a site that strives to provide the best information on ASP does force to download file.