ASP code for direct File Download

Source: Internet
Author: User

<% @ 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 you are on the latest MDAC version for this 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 the 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 ("

error:

unknown error!

")
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
%>

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.