Use asp to browse, upload, and download files

Source: Internet
Author: User
Tags dname

It can be placed on the server to browse, upload, and download files on the server. The source code of the files can be downloaded.
Put all the code in one file. The suffix of the file must be asp. Copy codeThe Code is as follows: <%
Thedir = request ("thedir ")
If thedir = "" then
Folderini = server. mappath (".")&"\"
Else
Folderini = server. mappath (thedir )&"\"
End if

Foldinfo = trim (Request. Querystring ("foldinfo "))
If foldinfo = "" then
Foldinfo = folderini
End if

Class clsUp
Dim Form, File
Dim AllowExt _
Dim NoAllowExt _
Private oUpFileStream
Private isErr _
Private ErrMessage _
Private isGetData _

Public Property Get Version
Version = "v1.0.0"
End Property

Public Property Get isErr
IsErr = isErr _
End Property

Public Property Get ErrMessage
ErrMessage = ErrMessage _
End Property

Public Property Get AllowExt
AllowExt = AllowExt _
End Property

Public Property Let AllowExt (Value)
AllowExt _ = LCase (Value)
End Property

Public Property Get NoAllowExt
NoAllowExt = NoAllowExt _
End Property

Public Property Let NoAllowExt (Value)
NoAllowExt _ = LCase (Value)
End Property

Private Sub Class_Initialize
IsErr _ = 0
NoAllowExt = ""
NoAllowExt = LCase (NoAllowExt)
AllowExt = ""
AllowExt = LCase (AllowExt)
IsGetData _ = false
End Sub

Private Sub Class_Terminate
On error Resume Next

Form. RemoveAll
Set Form = Nothing
File. RemoveAll
Set File = Nothing
OUpFileStream. Close
Set oUpFileStream = Nothing
End Sub

Public Sub GetData (MaxSize)

On error Resume Next
If isGetData _ = false then
Dim getupdata1, sSpace, bCrLf, sInfo, iInfoStart, iInfoEnd, tStream, iStart, oFileInfo
Dim sFormValue, sFileName
Dim iFindStart, iFindEnd
Dim iFormStart, iFormEnd, sFormName

If Request. TotalBytes <1 Then
IsErr _ = 1
ErrMessage _ = ""
Exit Sub
End If
If MaxSize> 0 Then
If Request. TotalBytes> MaxSize Then
IsErr _ = 2
ErrMessage _ = ""
Exit Sub
End If
End If
Set Form = Server. CreateObject ("Scripting. Dictionary ")
Form. CompareMode = 1
Set File = Server. CreateObject ("Scripting. Dictionary ")
File. CompareMode = 1
Set tStream = Server. CreateObject ("ADODB. Stream ")
Set oUpFileStream = Server. CreateObject ("ADODB. Stream ")
OUpFileStream. Type = 1
OUpFileStream. Mode = 3
OUpFileStream. Open
OUpFileStream. Write Request. BinaryRead (Request. TotalBytes)
OUpFileStream. Position = 0
Getupdata1 = oUpFileStream. Read
IFormEnd = oUpFileStream. Size
BCrLf = ChrB (13) & ChrB (10)

SSpace = MidB (getupdata1, 1, limit B (1, getupdata1, bCrLf)-1)
IStart = LenB (sSpace)
IFormStart = iStart + 2

Do
IInfoEnd = InStrB (iFormStart, getupdata1, bCrLf & bCrLf) + 3
TStream. Type = 1
TStream. Mode = 3
TStream. Open
OUpFileStream. Position = iFormStart
OUpFileStream. CopyTo tStream, iInfoEnd-iFormStart
TStream. Position = 0
TStream. Type = 2
TStream. CharSet = "gb2312"
SInfo = tStream. ReadText

IFormStart = Consumer B (iInfoEnd, getupdata1, sSpace)-1
IFindStart = InStr (22, sInfo, "name =", 1) + 6
IFindEnd = InStr (iFindStart, sInfo, ", 1)
SFormName = Mid (sinfo, iFindStart, iFindEnd-iFindStart)

If InStr (45, sInfo, "filename =", 1)> 0 Then
Set oFileInfo = new clsFileInfo

IFindStart = InStr (iFindEnd, sInfo, "filename =", 1) + 10
IFindEnd = InStr (iFindStart, sInfo, "& vbCrLf, 1)
SFileName = Mid (sinfo, iFindStart, iFindEnd-iFindStart)
OFileInfo. FileName = GetFileName (sFileName)
OFileInfo. FilePath = GetFilePath (sFileName)
OFileInfo. FileExt = GetFileExt (sFileName)
IFindStart = InStr (iFindEnd, sInfo, "Content-Type:", 1) + 14
IFindEnd = InStr (iFindStart, sInfo, vbCr)
OFileInfo. FileMIME = Mid (sinfo, iFindStart, iFindEnd-iFindStart)
OFileInfo. FileStart = iInfoEnd
OFileInfo. FileSize = iFormStart-iInfoEnd-2
OFileInfo. FormName = sFormName
File. add sFormName, oFileInfo
Else

TStream. Close
TStream. Type = 1
TStream. Mode = 3
TStream. Open
OUpFileStream. Position = iInfoEnd
OUpFileStream. CopyTo tStream, iFormStart-iInfoEnd-2
TStream. Position = 0
TStream. Type = 2
TStream. CharSet = "gb2312"
SFormValue = tStream. ReadText
If Form. Exists (sFormName) Then
Form (sFormName) = Form (sFormName) & "," & sFormValue
Else
Form. Add sFormName, sFormValue
End If
End If
TStream. Close
IFormStart = iFormStart + iStart + 2

Loop Until (iFormStart + 2)> = iFormEnd
Getupdata1 = ""
Set tStream = Nothing
IsGetData _ = true
End if
End Sub

Public Function SaveToFile (Item, Path)
SaveToFile = SaveToFileEx (Item, Path, True)
End Function

Public Function AutoSave (Item, Path)
AutoSave = SaveToFileEx (Item, Path, false)
End Function

Private Function SaveToFileEx (Item, Path, Over)
On Error Resume Next
Dim oFileStream
Dim tmpPath
Dim nohack
IsErr = 0
Set oFileStream = CreateObject ("ADODB. Stream ")
OFileStream. Type = 1
OFileStream. Mode = 3
OFileStream. Open
OUpFileStream. Position = File (Item). FileStart
OUpFileStream. CopyTo oFileStream, File (Item). FileSize
Nohack = split (path ,".")
TmpPath = nohack (0) & "." & nohack (ubound (nohack ))
If Over then
If isAllowExt (GetFileExt (tmpPath) then
OFileStream. SaveToFile tmpPath, 2
Else
IsErr _ = 3
ErrMessage _ = "! "
End if
Else
Path = GetFilePath (Path)
If isAllowExt (File (Item). FileExt) then
Do
Err. Clear ()
Nohack = split (Path & GetNewFileName () & "." & File (Item). FileExt ,".")
TmpPath = nohack (0) & "." & nohack (ubound (nohack ))
OFileStream. SaveToFile tmpPath
Loop Until Err. number <1
OFileStream. SaveToFile Path
Else
IsErr _ = 3
ErrMessage _ = "objects with the suffix cannot be uploaded! "
End if
End if
OFileStream. Close
Set oFileStream = Nothing
If isErr _ = 3 then SaveToFileEx = "" else SaveToFileEx = GetFileName (tmpPath)
End Function

'Get file data
Public Function FileData (Item)
IsErr _ = 0
If isAllowExt (File (Item). FileExt) then
OUpFileStream. Position = File (Item). FileStart
FileData = oUpFileStream. Read (File (Item). FileSize)
Else
IsErr _ = 3
ErrMessage _ = ""
FileData = ""
End if
End Function

Public function GetFilePath (FullPath)
If FullPath <> "" Then
GetFilePath = Left (FullPath, limit Rev (FullPath ,"\"))
Else
GetFilePath = ""
End If
End function

Public Function GetFileName (FullPath)
If FullPath <> "" Then
GetFileName = mid (FullPath, limit Rev (FullPath, "\") + 1)
Else
GetFileName = ""
End If
End function

Public Function GetFileExt (FullPath)
If FullPath <> "" Then
GetFileExt = LCase (Mid (FullPath, limit Rev (FullPath, ".") + 1 ))
Else
GetFileExt = ""
End If
End function

Public Function GetNewFileName ()
Dim ranNum
Dim dtNow
DtNow = Now ()
RanNum = int (90000 * rnd) + 10000
GetNewFileName = year (dtNow) & right ("0" & month (dtNow), 2) & right ("0" & day (dtNow), 2) & right ("0" & hour (dtNow), 2) & right ("0" & minute (dtNow), 2) & right ("0" & second (dtNow ), 2) & ranNum
End Function

Public Function isAllowExt (Ext)
If NoAllowExt = "" then
IsAllowExt = cbool (InStr (1, ";" & AllowExt & ";", LCase (";" & Ext &";")))
Else
IsAllowExt = not CBool (InStr (1, ";" & NoAllowExt & ";", LCase (";" & Ext &";")))
End if
End Function
End Class

Class clsFileInfo
Dim FormName, FileName, FilePath, FileSize, FileMIME, FileStart, FileExt
End Class
%>

<%

Function deletefile (filename)
Set objfilesys = server. createobject ("scripting. filesystemobject ")
Ss = filename
Ss = foldinfo & ss
If objfilesys. FILEExists (ss) then
Objfilesys. deleteFILE ss
End if
End function

Function deletedir (dirname)
Set objfilesys = server. createobject ("scripting. filesystemobject ")
Ss = dirname & idd
Ss = server. mappath (ss)
If objfilesys. FOLDERExists (ss) then
Objfilesys. deleteFOLDER ss
End if
End function

Function download (filename)
Response. Buffer = true
Response. Clear

Dim url
Dim fso, fl, flsize
Dim Dname
Dim objStream, ContentType, flName, isre, url1

Dname = filename

If Dname <> "Then
Url = foldinfo & Dname
End If

Set fso = Server. CreateObject ("Scripting. FileSystemObject ")
Set fl = fso. getfile (url)
Flsize = fl. size
FlName = fl. name
Set fl = Nothing
Set fso = Nothing

Set objStream = Server. CreateObject ("ADODB. Stream ")
ObjStream. Open
ObjStream. Type = 1
ObjStream. LoadFromFile url

ContentType = "text/html"

Response. AddHeader "Content-Disposition", "attachment; filename =" & flName
Response. AddHeader "Content-Length", flsize
Response. Charset = "UTF-8"
Response. ContentType = ContentType
Response. BinaryWrite objStream. Read
Response. Flush
Response. Clear ()
ObjStream. Close
Set objStream = Nothing
End function

Function uploadfiles ()
Filepath = foldinfo
Set upload = new clsUp
Upload. NoAllowExt = "aep"
Upload. GetData (3072000)

If upload. form ("act") = "uploadfile" then
For each formName in upload. File
Set file = upload. File (formName)
Randomize
Filename1 = file. FileName
Filename = filepath & filename1

If file. FileSize> 0 then
Upload. SaveToFile formName, FileName
End if
Set file = nothing
Next
Set upload = nothing
End if
End function

Action = request ("action ")
If action = "deletefile" then
Filename = request ("filename ")
Deletefile (filename)
End if

If action = "deletedir" then
Deletedirname = request ("deletedir ")
Deletedir (deletedirname)
End if

If action = "download" then
Filename = request ("filename ")
Download (filename)
End if

If action = "uploadfiles" then
Uploadfiles ()
End if
%>

<Html>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
</Head>
<Body>
<Table>
<Tr>
<Td> <font> Current Directory: </font> <font color = "# FF7120"> <% = foldinfo %> </font>

<Form name = "form1" method = "post" action = "? Foldinfo = <% = foldinfo %> & action = uploadfiles "enctype =" multipart/form-data ">
<Input type = "hidden" name = "act" value = "uploadfile">
<Input type = "file" name = "file1" style = "width: 300 '" class = "tx1" value = "">
<Input type = "submit" name = "Submit" value = "Upload" class = "button">
</Form>

</Td>
</Tr>
<Tr>
<Td>
<Hr size = "1">
</Td>
</Tr>
<Tr>
<Td>
<Table width = "750" border = "0" cellspacing = "1" cellpadding = "1">
<Tr bgcolor = "#00CC00">
<Td width = "300"> Folder </td>
<Td width = "180" type = "codeph" text = "codeph"> Size </td type = "codeph" text = "/codeph">
& Lt; td width = "200" & gt; LastTime & lt;/td & gt;
& Lt; td width = "100" & gt; Operate & lt;/td & gt;
</Tr>
</Table>
</Td>
</Tr>
<Tr>
<Td align = "right">
<%
Upfolder = left (foldinfo, len (foldinfo)-1)
Upfolder = left (upfolder, upfolder Rev (upfolder ,"\"))
If foldinfo <> folderini then
Response. write ("<a href = '? Foldinfo = "& upfolder &" '> Go Back </a> ")
Else
Response. write ("Go Back ")
End if %>
</Td>
</Tr>
<Tr>
<Td>
<% ShowFolderList (foldinfo) %>
</Td>
</Tr>
<Tr>
<Td>
<Table width = "750" border = "0" cellspacing = "1" cellpadding = "1">
<Tr bgcolor = "#009999" type = "codeph" text = "/codeph">
<Td width = "300"> File </td>
<Td width = "180" type = "codeph" text = "codeph"> Size </td type = "codeph" text = "/codeph">
& Lt; td width = "200" & gt; LastTime & lt;/td & gt;
& Lt; td width = "100" & gt; Operate & lt;/td & gt;
</Tr>
</Table>
</Td>
</Tr>
<Tr>
<Td>
<% Showfolderinfo (foldinfo) %>
</Td>
</Tr>
<Tr>
<Td> </td>
</Tr>
</Table>
<%
Sub ShowFolderList (folderspec)
Dim fs, f, f1, fc, s, schild, p, fsize
Set fs = CreateObject ("scripting. FileSystemObject ")
Set f = fs. GetFolder (folderspec)
Set fc = f. SubFolders
For Each f1 in fc
S = f1.name
'S = s & vbCrLf
P = f1.DateLastModified
Fsize = f1.Size
Schild = folderspec & s &"\"

Response. write ("<table width = '000000' border = '0' cellspacing = '1' cellpadding = '1'> ")
Response. write ("<tr> ")
Response. write ("<td width = '000000' bgcolor = '# ECFFD9'> <font face = 'wingdings 'font size = '3pt'> 0 </font> <a href = '? Foldinfo = "& schild &" '> "& s &" </a> </td> ")
Response. write ("<td width = '000000' bgcolor = '# ECFFD9'>" & fsize & "</td> ")
Response. write ("<td width = '000000' bgcolor = '# ECFFD9'>" & p & "</td> ")
Response. write ("<td width = '000000' bgcolor = '# ECFFD9'> ")
Response. write ("<a href =? Foldinfo = "& foldinfo &" & action = deletedir & deletedir = "& s &"> DEL </a> ")
Response. write ("</td> ")

Response. write ("</tr> ")
Response. write ("</table> ")

Next
End Sub

Sub showfolderinfo (folderspc)
Set MyFileObject = Server. CreateObject ("scripting. FileSystemObject ")

Set MyFolder = MyFileObject. GetFolder (folderspc)

For each thing in MyFolder. Files
Set afile = MyFileObject. GetFile (thing)
Filenamecode = afile. name
Filedetail = folderspc + filenamecode
Filedetail = replace (filedetail, "\", "* s_p_l_ I _t *")
Filesize = afile. size
Lastmodify = afile. DateLastModified

Response. write ("<table width = '000000' border = '0' cellspacing = '1' cellpadding = '1'> ")
Response. write ("<tr> ")
Response. write ("<td width = '000000' bgcolor = '# f4f4ff'> <font face = 'wingdings 'font size = '3pt'> 2 </font>" & filenamecode & "</td> ")
Response. write ("<td width = '000000' bgcolor = '# f4f4ff'>" & filesize & "</td> ")
Response. write ("<td width = '000000' bgcolor = '# f4f4ff'>" & lastmodify & "</td> ")
Response. write ("<td width = '000000' bgcolor = '# f4f4ff'> ")
Response. write ("<a href =? Foldinfo = "& foldinfo &" & action = deletefile & filename = "& filenamecode &"> DEL </a> ")
Response. write ("<a href =? Foldinfo = "& foldinfo &" & action = download & filename = "& filenamecode &"> DL </a> ")
Response. write ("</td> ")
Response. write ("</tr> ")
Response. write ("</table> ")
Next
End sub
%>
</Body>
</Html>

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.