ASP using FSO and stream method to realize ie download documents directly

Source: Internet
Author: User
fso|stream| Download

When IE is linking to documents, if you encounter OLE supported documents, IE will automatically call the appropriate program to open it, sometimes this function is not what we need, although we can remind users to use the right mouse button--> "target Save As ..." command to download the document, but this is not very friendly, This article describes the use of FSO and stream method to achieve IE direct download documents.

<%@ Language=vbscript codepage=65001%>

<%
' Filename must be input
If Request ("Filename") = "" Then
Response.Write "Else
Call DownloadFile (replace (Request ("Filename", "\", ""), "/", ""))

Function DownloadFile (strfile)
' Make sure your are on the latest MDAC version of To work
' Get full path of specified file
strFileName = Server. MapPath (strfile)

' Clear the buffer
Response.Buffer = True
Response.Clear

' Create stream
Set s = Server.CreateObject ("ADODB.") Stream ")
S.open

' Set as Binary
S.type = 1

' Load in ' file
On Error Resume Next

' Check the file exists
Set FSO = Server.CreateObject ("Scripting.FileSystemObject")
If not FSO. FileExists (strFileName) Then
Response.Write ("Response.End
End If

' Get length of file '
Set f = fso. GetFile (strFileName)
Intfilelength = F.size

S.loadfromfile (strFileName)
If Err Then
Response.Write ("Response.End
End If

' Send the headers to the users Browse
Response.AddHeader "Content-disposition", "attachment"; Filename= "&f.name
Response.AddHeader "Content-length", intfilelength
Response.Charset = "UTF-8"
Response.ContentType = "Application/octet-stream"

' Output the file to the ' browser
Response.BinaryWrite S.read
Response.Flush

' Tidy up
S.close
Set s = Nothing

End Function
End If
%>



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.