ASP file download code

Source: Internet
Author: User

<%
Dim Url,strurl,strpath
Dim Strinceptfile
Strinceptfile = "SWF,FLA,JPG,JPEG,GIF,PNG,BMP,TIF,IFF,MP3,WMA,RM,WMV,MID,RMI,CDA,AVI,MPG,MPEG,RA,RAM,WOV,ASF"
url = replace (replace (Request ("url"), "'", ""), "%", "" "," "," "/"

If Len (URL) > 3 Then
If Left (url,1) = "/" Then
Response.Redirect URL
End If
If InStr (URL, "..."). /") > 0 Then
Response.Redirect URL
End If
strURL = Left (url,10)
If InStr (strURL, "://") > 0 Then
Response.Redirect URL
End If
If InStr (URL, "/") > 0 Then
URL =replace (URL, "..."). /", "")
If checkfileext (URL) Then
strpath = Server.MapPath (".") & "" & URL
strpath = Replace (strpath, "/", "")
Call Downthisfile (strpath)
End If
Else
Response.Redirect URL
End If
End If

Sub Downthisfile (Thepath)
Response.Clear
On Error Resume Next
Dim Stream, FileName, Filecontenttype

FileName = Split (Thepath, "") (UBound (Split (Thepath, ""))
Set stream = Server.CreateObject ("ADODB.stream")
Stream. Open
Stream. Type = 1
Stream. LoadFromFile (Thepath)
Response.AddHeader "Content-disposition", "attachment"; Filename= "& FileName
Response.AddHeader "Content-length", stream. Size
Response.Charset = "UTF-8"
Response.ContentType = "Application/octet-stream"
Response.BinaryWrite Stream. Read
Response.Flush
Stream. Close
Set stream = Nothing
End Sub

Function Checkfileext (ByVal strfile)
Dim Arrinceptfile
Dim I, Strfileext

On Error Resume Next

If Trim (strfile) = "" Or isempty (strfile) Then
Checkfileext = False
Exit Function
End If

Strfileext = Getfileextname (strfile)
Strfileext = LCase (Strfileext)
Strinceptfile = LCase (strinceptfile)
If Len (Strinceptfile) = 0 Then
Checkfileext = True
Exit Function
End If
Arrinceptfile = Split (Strinceptfile, ",")

For i = 0 to UBound (arrinceptfile)
If trim (strfileext) = Trim (Arrinceptfile (i)) Then
Checkfileext = True
Exit Function
Else
Checkfileext = False
End If
Next
Checkfileext = False
End Function
Function getfileextname (ByVal strFilePath)
Dim Strextname
Strextname = Mid (strFilePath, InStrRev (strFilePath, ".") + 1)
If InStr (Strextname, "?") > 0 Then
Getfileextname = Left (Strextname, InStr (Strextname, "?")-1)
Else
Getfileextname = Strextname
End If
End Function
%>

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.