用asp实现文件浏览、上传、下载的程序
2018-09-06 13:04
可以放在服务器上,对服务器上的文件进行浏览、上传、下载,可下载文件源码。
把下所有代码入在一个文件里即可,文件的后缀要为asp。
复制代码 代码如下:
<%
thedir=request(thedir)
ifthedir=then
folderini=server.mappath(.)&\
else
folderini=server.mappath(thedir)&\
endif
foldinfo=trim(Request.Querystring(foldinfo))
iffoldinfo=then
foldinfo=folderini
endif
classclsUp
DimForm,File
DimAllowExt_
DimNoAllowExt_
PrivateoUpFileStream
PrivateisErr_
PrivateErrMessage_
PrivateisGetData_
PublicPropertyGetVersion
Version=v1.0.0
EndProperty
PublicPropertyGetisErr
isErr=isErr_
EndProperty
PublicPropertyGetErrMessage
ErrMessage=ErrMessage_
EndProperty
PublicPropertyGetAllowExt
AllowExt=AllowExt_
EndProperty
PublicPropertyLetAllowExt(Value)
AllowExt_=LCase(Value)
EndProperty
PublicPropertyGetNoAllowExt
NoAllowExt=NoAllowExt_
EndProperty
PublicPropertyLetNoAllowExt(Value)
NoAllowExt_=LCase(Value)
EndProperty
PrivateSubClass_Initialize
isErr_=0
NoAllowExt=
NoAllowExt=LCase(NoAllowExt)
AllowExt=
AllowExt=LCase(AllowExt)
isGetData_=false
EndSub
PrivateSubClass_Terminate
onerrorResumeNext
Form.RemoveAll
SetForm=Nothing
File.RemoveAll
SetFile=Nothing
oUpFileStream.Close
SetoUpFileStream=Nothing
EndSub
PublicSubGetData(MaxSize)
onerrorResumeNext
ifisGetData_=falsethen
Dimgetupdata1,sSpace,bCrLf,sInfo,iInfoStart,iInfoEnd,tStream,iStart,oFileInfo
DimsFormValue,sFileName
DimiFindStart,iFindEnd
DimiFormStart,iFormEnd,sFormName
IfRequest.TotalBytes<1Then
isErr_=1
ErrMessage_=
ExitSub
EndIf
IfMaxSize>0Then
IfRequest.TotalBytes>MaxSizeThen
isErr_=2
ErrMessage_=
ExitSub
EndIf
EndIf
SetForm=Server.CreateObject(Scripting.Dictionary)
SetFile=Server.CreateObject(Scripting.Dictionary)
SettStream=Server.CreateObject(ADODB.Stream)
SetoUpFileStream=Server.CreateObject(ADODB.Stream)
oUpFileStream.Type=1
oUpFileStream.Mode=3
oUpFileStream.Open
oUpFileStream.WriteRequest.BinaryRead(Request.TotalBytes)
oUpFileStream.Position=0
getupdata1=oUpFileStream.Read
iFormEnd=oUpFileStream.Size
bCrLf=ChrB(13)&ChrB(10)
sSpace=MidB(getupdata1,1,InStrB(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.CopyTotStream,iInfoEnd-iFormStart
tStream.Position=0
tStream.Type=2
tStream.CharSet=gb2312
sInfo=tStream.ReadText
iFormStart=InStrB(iInfoEnd,getupdata1,sSpace)-1
iFindStart=InStr(22,sInfo,name=,1)+6
iFindEnd=InStr(iFindStart,sInfo,,1)
sFormName=Mid(sinfo,iFindStart,iFindEnd-iFindStart)
IfInStr(45,sInfo,filename=,1)>0Then
SetoFileInfo=newclsFileInfo
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.addsFormName,oFileInfo
else
tStream.Close
tStream.Type=1
tStream.Mode=3
tStream.Open
oUpFileStream.Position=iInfoEnd
oUpFileStream.CopyTotStream,iFormStart-iInfoEnd-2
tStream.Position=0
tStream.Type=2
tStream.CharSet=gb2312
sFormValue=tStream.ReadText
IfForm.Exists(sFormName)Then
Form(sFormName)=Form(sFormName)&,&sFormValue
else
Form.AddsFormName,sFormValue
EndIf
EndIf
tStream.Close
iFormStart=iFormStart+iStart+2
LoopUntil(iFormStart+2)>=iFormEnd
getupdata1=
SettStream=Nothing
isGetData_=true
endif
EndSub
PublicFunctionSaveToFile(Item,Path)
SaveToFile=SaveToFileEx(Item,Path,True)
EndFunction
PublicFunctionAutoSave(Item,Path)
AutoSave=SaveToFileEx(Item,Path,false)
EndFunction
PrivateFunctionSaveToFileEx(Item,Path,Over)
OnErrorResumeNext
DimoFileStream
DimtmpPath
Dimnohack
isErr=0
SetoFileStream=CreateObject(ADODB.Stream)
oFileStream.Type=1
oFileStream.Mode=3
oFileStream.Open
oUpFileStream.Position=File(Item).FileStart
oUpFileStream.CopyTooFileStream,File(Item).FileSize
nohack=split(path,.)
tmpPath=nohack(0)&.&nohack(ubound(nohack))
ifOverthen
ifisAllowExt(GetFileExt(tmpPath))then
oFileStream.SaveToFiletmpPath,2
Else
isErr_=3
ErrMessage_=!
Endif
Else
Path=GetFilePath(Path)
ifisAllowExt(File(Item).FileExt)then
do
Err.Clear()
nohack=split(Path&GetNewFileName()&.&File(Item).FileExt,.)
tmpPath=nohack(0)&.&nohack(ubound(nohack))
oFileStream.SaveToFiletmpPath
loopUntilErr.number<1
oFileStream.SaveToFilePath
Else
isErr_=3
ErrMessage_=该后缀名的文件不允许上传!
Endif
Endif
oFileStream.Close
SetoFileStream=Nothing
ifisErr_=3thenSaveToFileEx=elseSaveToFileEx=GetFileName(tmpPath)
EndFunction
取得文件数据
PublicFunctionFileData(Item)
isErr_=0
ifisAllowExt(File(Item).FileExt)then
oUpFileStream.Position=File(Item).FileStart
FileData=oUpFileStream.Read(File(Item).FileSize)
Else
isErr_=3
ErrMessage_=
FileData=
Endif
EndFunction
PublicfunctionGetFilePath(FullPath)
IfFullPath<>Then
GetFilePath=Left(FullPath,InStrRev(FullPath,\))
Else
GetFilePath=
EndIf
Endfunction
PublicFunctionGetFileName(FullPath)
IfFullPath<>Then
GetFileName=mid(FullPath,InStrRev(FullPath,\)+1)
Else
GetFileName=
EndIf
Endfunction
PublicFunctionGetFileExt(FullPath)
IfFullPath<>Then
GetFileExt=LCase(Mid(FullPath,InStrRev(FullPath,.)+1))
Else
GetFileExt=
EndIf
Endfunction
PublicFunctionGetNewFileName()
dimranNum
dimdtNow
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
EndFunction
PublicFunctionisAllowExt(Ext)
ifNoAllowExt=then
isAllowExt=cbool(InStr(1,;&AllowExt&;,LCase(;&Ext&;)))
else
isAllowExt=notCBool(InStr(1,;&NoAllowExt&;,LCase(;&Ext&;)))
endif
EndFunction
EndClass
ClassclsFileInfo
DimFormName,FileName,FilePath,FileSize,FileMIME,FileStart,FileExt
EndClass
%>
<%
functiondeletefile(filename)
setobjfilesys=server.createobject(scripting.filesystemobject)
ss=filename
ss=foldinfo&ss
ifobjfilesys.FILEExists(ss)then
objfilesys.deleteFILEss
endif
endfunction
functiondeletedir(dirname)
setobjfilesys=server.createobject(scripting.filesystemobject)
ss=dirname&idd
ss=server.mappath(ss)
ifobjfilesys.FOLDERExists(ss)then
objfilesys.deleteFOLDERss
endif
endfunction
functiondownload(filename)
Response.Buffer=true
Response.Clear
dimurl
Dimfso,fl,flsize
dimDname
DimobjStream,ContentType,flName,isre,url1
Dname=filename
IfDname<>Then
url=foldinfo&Dname
EndIf
Setfso=Server.CreateObject(Scripting.FileSystemObject)
Setfl=fso.getfile(url)
flsize=fl.size
Setfl=Nothing
Setfso=Nothing
SetobjStream=Server.CreateObject(ADODB.Stream)
objStream.Open
objStream.Type=1
objStream.LoadFromFileurl
ContentType=text/html
Response.AddHeaderContent-Disposition,attachment;filename=&flName
Response.AddHeaderContent-Length,flsize
Response.Charset=UTF-8
Response.ContentType=ContentType
Response.BinaryWriteobjStream.Read
Response.Flush
response.Clear()
objStream.Close
SetobjStream=Nothing
endfunction
functionuploadfiles()
filepath=foldinfo
setupload=newclsUp
upload.NoAllowExt=aep
upload.GetData(3072000)
ifupload.form(act)=uploadfilethen
foreachformNameinupload.File
setfile=upload.File(formName)
randomize
filename1=file.FileName
filename=filepath&filename1
iffile.FileSize>0then
upload.SaveToFileformName,FileName
endif
setfile=nothing
next
setupload=nothing
endif
endfunction
action=request(action)
ifaction=deletefilethen
filename=request(filename)
deletefile(filename)
endif
ifaction=deletedirthen
deletedirname=request(deletedir)
deletedir(deletedirname)
endif
ifaction=downloadthen
filename=request(filename)
download(filename)
endif
ifaction=uploadfilesthen
uploadfiles()
endif
%>
<html>
<head>
<metahttp-equiv=Content-Typecontent=text/html;charset=gb2312>
</head>
<body>
<table>
<tr>
<td><font>当前目录:</font><fontcolor=#FF7120><%=foldinfo%></font>
<formname=form1method=postaction=?foldinfo=<%=foldinfo%>&action=uploadfilesenctype=multipart/form-data>
<inputtype=hiddenname=actvalue=uploadfile>
<inputtype=filename=file1style=width:300class=tx1value=>
<inputtype=submitname=Submitvalue=上传class=button>
</form>
</td>
</tr>
<tr>
<td>
<hrsize=1>
</td>
</tr>
<tr>
<td>
<tablewidth=750border=0cellspacing=1cellpadding=1>
<trbgcolor=#00CC00>
<tdwidth=300>Folder</td>
<tdwidth=180>Size</td>
<tdwidth=200>LastTime</td>
<tdwidth=100>Operate</td>
</tr>
</table>
</td>
</tr>
<tr>
<tdalign=right>
<%
upfolder=left(foldinfo,len(foldinfo)-1)
upfolder=left(upfolder,InstrRev(upfolder,\))
iffoldinfo<>folderinithen
response.write(<ahref=?foldinfo=&upfolder&>GoBack</a>)
else
response.write(GoBack)
endif%>
</td>
</tr>
<tr>
<td>
<%ShowFolderList(foldinfo)%>
</td>
</tr>
<tr>
<td>
<tablewidth=750border=0cellspacing=1cellpadding=1>
<trbgcolor=#009999>
<tdwidth=300>File</td>
<tdwidth=180>Size</td>
<tdwidth=200>LastTime</td>
<tdwidth=100>Operate</td>
</tr>
</table>
</td>
</tr>
<tr>
<td>
<%showfolderinfo(foldinfo)%>
</td>
</tr>
<tr>
<td></td>
</tr>
</table>
<%
SubShowFolderList(folderspec)
Dimfs,f,f1,fc,s,schild,p,fsize
Setfs=CreateObject(scripting.FileSystemObject)
Setf=fs.GetFolder(folderspec)
Setfc=f.SubFolders
ForEachf1infc
s=f1.name
s=s&vbCrLf
p=f1.DateLastModified
fsize=f1.Size
schild=folderspec&s&\
Response.write(<tablewidth=750border=0cellspacing=1cellpadding=1>)
Response.write(<tr>)
Response.write(<tdwidth=300bgcolor=#ECFFD9><fontface=Wingdingsfontsize=3pt>0</font><ahref=?foldinfo=&schild&>&s&</a></td>)
Response.write(<tdwidth=180bgcolor=#ECFFD9>&fsize&</td>)
Response.write(<tdwidth=200bgcolor=#ECFFD9>&p&</td>)
Response.write(<tdwidth=100bgcolor=#ECFFD9>)
Response.write(<ahref=?foldinfo=&foldinfo&&action=deletedir&deletedir=&s&>DEL</a>)
Response.write(</td>)
Response.write(</tr>)
Response.write(</table>)
Next
EndSub
Subshowfolderinfo(folderspc)
setMyFileObject=Server.CreateObject(scripting.FileSystemObject)
SetMyFolder=MyFileObject.GetFolder(folderspc)
foreachthinginMyFolder.Files
Setafile=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(<tablewidth=750border=0cellspacing=1cellpadding=1>)
Response.write(<tr>)
Response.write(<tdwidth=300bgcolor=#f4f4ff><fontface=Wingdingsfontsize=3pt>2</font>&filenamecode&</td>)
Response.write(<tdwidth=180bgcolor=#f4f4ff>&filesize&</td>)
Response.write(<tdwidth=200bgcolor=#f4f4ff>&lastmodify&</td>)
Response.write(<tdwidth=100bgcolor=#f4f4ff>)
Response.write(<ahref=?foldinfo=&foldinfo&&action=deletefile&filename=&filenamecode&>DEL</a>)
Response.write(<ahref=?foldinfo=&foldinfo&&action=download&filename=&filenamecode&>DL</a>)
Response.write(</td>)
Response.write(</tr>)
Response.write(</table>)
Next
Endsub
%>
</body>
</html>
文章标题:用asp实现文件浏览、上传、下载的程序
文章链接:http://soscw.com/index.php/essay/10879.html