欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

用asp实现文件浏览、上传、下载的程序

程序员文章站 2023-11-20 20:37:10
可以放在服务器上,对服务器上的文件进行浏览、上传、下载,可下载文件源码。 把下所有代码入在一个文件里即可,文件的后缀要为asp。 复制代码 代码如下:<% ...
可以放在服务器上,对服务器上的文件进行浏览、上传、下载,可下载文件源码。
把下所有代码入在一个文件里即可,文件的后缀要为asp。
复制代码 代码如下:

<% 
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, 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.copyto tstream,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) 

   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_="该后缀名的文件不允许上传!" 
  end if 
 end if 
 ofilestream.close 
 set ofilestream = nothing 
 if iserr_=3 then savetofileex="" else savetofileex=getfilename(tmppath) 
end function 

'取得文件数据 
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,instrrev(fullpath, "\")) 
    else 
    getfilepath = "" 
  end if 
end function 

public function getfilename(fullpath) 
  if fullpath <> "" then 
    getfilename = mid(fullpath,instrrev(fullpath, "\")+1) 
    else 
    getfilename = "" 
  end if 
end function 

public function getfileext(fullpath) 
  if fullpath <> "" then 
    getfileext = lcase(mid(fullpath,instrrev(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>当前目录:</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="上传" 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" >size</td> 
          <td width="200" >lasttime</td> 
          <td width="100" >operate</td> 
        </tr> 
      </table> 
    </td> 
  </tr> 
  <tr>  
    <td align="right" >  
      <% 
            upfolder=left(foldinfo,len(foldinfo)-1) 
            upfolder=left(upfolder,instrrev(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">  
          <td width="300">file</td> 
          <td width="180">size</td> 
          <td width="200">lasttime</td> 
          <td width="100">operate</td> 
        </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='750' border='0' cellspacing='1' cellpadding='1'>") 
response.write("<tr>") 
response.write("<td width='300' bgcolor='#ecffd9'><font face='wingdings' font size='3pt'>0</font><a href='?foldinfo="&schild&"'>"&s&"</a></td>") 
response.write("<td width='180' bgcolor='#ecffd9'>"&fsize&"</td>") 
response.write("<td width='200' bgcolor='#ecffd9'>"&p&"</td>") 
response.write("<td width='100' 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='750' border='0' cellspacing='1' cellpadding='1'>") 
response.write("<tr>") 
response.write("<td width='300' bgcolor='#f4f4ff'><font face='wingdings' font size='3pt'>2</font>"&filenamecode&"</td>") 
response.write("<td width='180' bgcolor='#f4f4ff'>"&filesize&"</td>") 
response.write("<td width='200' bgcolor='#f4f4ff'>"&lastmodify&"</td>") 
response.write("<td width='100' 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>