Automatic Collection Program

Source: Internet
Author: User
Tags file type file

I recently made a music station, music file... It is generally collected from the Internet .. So .. Write a collection Section Program . CopyCode The Code is as follows: <%
On Error resume next
Const uploadpath = "/uploads/" 'file storage path
Const allowfileext = "JPG, WMA, SwF, GIF" 'file type that can be collected
& Apos; const allowfilesize = "200"
Function GetFile (URL)
If url = "" then
Exit Function
Else
Url = trim (URL)
End if

'Get the file
Fileext = lcase (mid (URL, limit Rev (URL, ".") + 1) 'file type
Filename = lcase (mid (URL, limit Rev (URL, "/") + 1, limit Rev (URL ,". ")-faster Rev (URL,"/")-1) 'No file type file name
Newfilepath = getnow ("date") & "_" & replace (formatdatetime (now (), 3), ":", "") & "_" & cleanfilename (filename) &". "& fileext

If instr ("," & lcase (allowfileext) & "," & fileext & ",") = 0 then
GetFile = "the file type is not allowed"
Exit Function
End if

Set XMLHTTP = server. Createobject ("Microsoft. XMLHTTP ")
XMLHTTP. Open "get", URL, false
XMLHTTP. Send
'While XMLHTTP. readystate <> 4
'Xmlhttp. waitforresponse 1000
'Wend
If XMLHTTP. Status <> 200 then
GetFile = "An error occurred while obtaining the file"
Exit Function
Else
Set folder = server. Createobject ("scripting. FileSystemObject ")
Dim Foldername
Foldername = getnow ("year") & getnow ("month") 'folder
If folder. folderexists (server. mappath (uploadpath) = false then
Folder. createfolder server. mappath (uploadpath)
End if
If folder. folderexists (server. mappath (uploadpath & Foldername) = false then
Folder. createfolder server. mappath (uploadpath & Foldername)
End if
Set folder = nothing

File = XMLHTTP. responsebody
If lenb (File)> allowfilesize then
GetFile = "the file is too large to be saved! "
Exit Function
Else
Set objadostream = server. Createobject ("ADODB. Str" & "EAM ")
Objadostream. open ()
Objadostream. type = 1
Objadostream. Write (file)
Objadostream. savetofile (server. mappath (uploadpath & Foldername & "/" & newfilepath ))
Objadostream. seteos
Set objadostream = nothing
GetFile = "<a href =" "& uploadpath & Foldername &"/"& newfilepath &" target = "" _ balnk ""> collection successful </a>"
End if
End if
Set XMLHTTP = nothing
End Function
%>
<SCRIPT runat = "server" Language = "jscript">
Function cleanfilename (STR ){
STR = Str. Replace (/[^ _ \. A-Za-Z \ D]/ig ,"");
STR = Str. Replace (/^ [\/\.] + /,"");
Return STR;
}
Function getnow (N)
{
D = new date ();
Switch (N)
{
Case "year ":
Return D. getyear ();
Case "month ":
Return (D. getmonth () + 1 );
Case "date ":
Return D. getdate ();
}

}< br>
<% if request ("do") = "GetFile" and request ("file ") <> "" Then
response. write (GetFile (Request ("file")
else %>





<% 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.