Profile photo
Submitted by G-Stone
on May 13th, 2011 at 08:05:23 EST

How to force download in classic ASP?

I need to force download dialog using classic ASP. For example, I need to force download files like pdf or jpeg rather than opening in the browser. Is there any easy way to do this? If I directly link to the pdf or jpeg files then it will open in the browser. But I need to force download so that client can save it to their local computer.

Profile photo
Replied by SBajra
on May 13th, 2011 at 09:33:30 EST
You can force download in ASP using HTML header such as "ContentType" and "Content-Disposition"

Here is the sample code for ASP:

Response.ContentType = "application/octet-stream"
Response.AddHeader "Content-Disposition", "attachment; filename=test.txt"
Response.Write("Hello World")

Here, I used content type as "application/octet-stream" to tell the response is binary mime type. Then I added "Content-Disposition" in the header telling that the response is an attachment and filename=text.txt

In this example, I just used text file as an example. If you need to force download like jpeg, you read the image binary file and then use Response.BinaryWrite() method to write binary data to the response stream.
Accepted Answer

Profile photo
Replied by G-Stone
on May 13th, 2011 at 10:15:01 EST
This is exactly what I am looking for. Thanks