asp利用檔案上傳組件實現檔案上傳

來源:互聯網
上載者:User


<style>body,form,fieldset,input,textarea{margin:0;padding:0;}body{background:#f2f2f2;height:100%;font:12px tahoma,arial;color:#333;text-align:center;}</style>
<body>
<form name="form" method="post" action="upload.asp教程?act=upfile" enctype="multipart/form-data">
<input type="file" name="file11" size="30">
<input type="submit" name="submit" value="上傳" class="button">
</form>
</body>
</html>


<%if act="upfile" then '上傳相關%>
<html>
<head>
<style>body,form,fieldset,input,textarea{margin:0;padding:0;}body{background:#f2f2f2;height:100%;font:12px tahoma,arial;color:#333;text-align:center;}</style>
<body>
<script runat=server language=vbscript>
dim data_5xsoft
class upload_5xsoft

dim objform,objfile,version
public function form(strform)
strform=lcase(strform)
if not objform.exists(strform) then form="" else form=objform(strform)
end function

public function file(strfile)
strfile=lcase(strfile)
if not objfile.exists(strfile) then set file=new fileinfo else set file=objfile(strfile)
end function

private sub class_initialize
dim requestdata,sstart,vbcrlf,sinfo,iinfostart,iinfoend,tstream,istart,thefile
dim ifilesize,sfilepath,sfiletype,sformvalue,sfilename
dim ifindstart,ifindend
dim iformstart,iformend,sformname
version="化境http上傳程式 version 2.0"
set objform=server.createobject("scripting.dictionary")
set objfile=server.createobject("scripting.dictionary")
if request.totalbytes<1 then exit sub
set tstream = server.createobject("adodb.stream")
set data_5xsoft = server.createobject("adodb.stream")
data_5xsoft.type = 1
data_5xsoft.mode =3
data_5xsoft.open
data_5xsoft.write request.binaryread(request.totalbytes)
data_5xsoft.position=0
requestdata =data_5xsoft.read
iformstart = 1
iformend = lenb(requestdata)
vbcrlf = chrb(13) & chrb(10)
sstart = midb(requestdata,1, instrb(iformstart,requestdata,vbcrlf)-1)
istart = lenb (sstart)
iformstart=iformstart+istart+1
while (iformstart + 10) < iformend
    iinfoend = instrb(iformstart,requestdata,vbcrlf & vbcrlf)+3
    tstream.type = 1
    tstream.mode =3
    tstream.open
    data_5xsoft.position = iformstart
    data_5xsoft.copyto tstream,iinfoend-iformstart
    tstream.position = 0
    tstream.type = 2
    tstream.charset ="gb2312"
    sinfo = tstream.readtext
    tstream.close
    '取得表單項目名稱
    iformstart = instrb(iinfoend,requestdata,sstart)
    ifindstart = instr(22,sinfo,"name=""",1)+6
    ifindend = instr(ifindstart,sinfo,"""",1)
    sformname = lcase(mid (sinfo,ifindstart,ifindend-ifindstart))
    '如果是檔案
    if instr (45,sinfo,"filename=""",1) > 0 then
        set thefile=new fileinfo
        '取得檔案名稱
        ifindstart = instr(ifindend,sinfo,"filename=""",1)+10
        ifindend = instr(ifindstart,sinfo,"""",1)
        sfilename = mid (sinfo,ifindstart,ifindend-ifindstart)
        thefile.filename=getfilename(sfilename)
        thefile.filepath=getfilepath(sfilename)
        '取得檔案類型
        ifindstart = instr(ifindend,sinfo,"content-type: ",1)+14
        ifindend = instr(ifindstart,sinfo,vbcr)
        thefile.filetype =mid (sinfo,ifindstart,ifindend-ifindstart)
        thefile.filestart =iinfoend
        thefile.filesize = iformstart -iinfoend -3
        thefile.formname=sformname
        if not objfile.exists(sformname) then objfile.add sformname,thefile
    else
    '如果是表單項目
        tstream.type =1
        tstream.mode =3
        tstream.open
        data_5xsoft.position = iinfoend
        data_5xsoft.copyto tstream,iformstart-iinfoend-3
        tstream.position = 0
        tstream.type = 2
        tstream.charset ="gb2312"
        sformvalue = tstream.readtext
        tstream.close
        if objform.exists(sformname) then
         objform(sformname)=objform(sformname)&", "&sformvalue       
        else
         objform.add sformname,sformvalue
        end if
    end if
    iformstart=iformstart+istart+1
    wend
requestdata=""
set tstream =nothing
end sub
private sub class_terminate
if request.totalbytes>0 then
    objform.removeall
    objfile.removeall
    set objform=nothing
    set objfile=nothing
    data_5xsoft.close
    set data_5xsoft =nothing
end if
end sub
 
private function getfilepath(fullpath)
if fullpath <> "" then getfilepath = left(fullpath,instrrev(fullpath, "")) else getfilepath = ""
end function

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

end class

class fileinfo
dim formname,filename,filepath,filesize,filetype,filestart
private sub class_initialize
  filename = ""
  filepath = ""
  filesize = 0
  filestart= 0
  formname = ""
  filetype = ""
end sub

public function saveas(fullpath)
  dim dr,errorchar,i
  saveas=true
  if trim(fullpath)="" or filestart=0 or filename="" or right(fullpath,1)="/" then exit function
  set dr=createobject("adodb.stream")
  dr.mode=3
  dr.type=1
  dr.open
  data_5xsoft.position=filestart
  data_5xsoft.copyto dr,filesize
  dr.savetofile fullpath,2
  dr.close
  set dr=nothing
  saveas=false
end function
end class
</script>
<%
dim upload,file,formpath,icount,filename,fileext
dim formname,uploadsuc,forum_upload,forumupload,upf,f_type,f_name,f_ftn,rannum
set upload=new upload_5xsoft '建立上傳對象
'********************************列出所有上傳檔案***************************************************
for each formname in upload.objfile
set file=upload.file(formname)
if file.filesize>0 then
    '********************************檢測檔案類型****************************************************
    fileext=ucase(right(file.filename,4))
    uploadsuc=false
    forum_upload="rar|zip|swf|jpg|png|gif|doc|txt|chm|pdf|ace|mp3|wma|wmv|midi|avi|rm|ra|rmvb|mov|xls"
    forumupload=split(forum_upload,"|")
    for i=0 to ubound(forumupload)
        if fileext="."&trim(forumupload(i)) then
            uploadsuc=true
            exit for
        else
            uploadsuc=false
        end if
    next
    if uploadsuc=false then
        response.write "檔案格式不正確[<a href=""post.asp?act=upload"">繼續上傳</a>]"
        response.end
    end if
    '********************************建立檔案上傳的目錄檔案夾****************************************
    set upf=server.createobject("scripting.filesystemobject")
    if err<>0 then
        err.clear
        response.write("您的伺服器不支援fso")
        response.end
    end if
    f_type= replace(fileext,".","")
    f_name= year(now)&"-"&month(now)
    if upf.folderexists(server.mappath("upload/"&f_name))=false then
       upf.createfolder server.mappath("upload/"&f_name)
    end if
    f_ftn="upload/"&f_name
    set upf=nothing
    '********************************儲存上傳檔案至檔案夾*****************************************
    randomize
    rannum=int(90000*rnd)+10000
    filename=f_ftn&"/"&day(now)&"-"&rannum&"-"&file.filename
    file.saveas server.mappath(filename)  '儲存檔案
        if f_type="jpg" or f_type="gif" or f_type="png" then
            response.write "<script>parent.form1.content.value+='[img]"&filename&"[/img]'</script>"
        elseif f_type="zip" or f_type="rar" or f_type="doc" or f_type="txt" then
            response.write "<script>parent.form1.content.value+='[url]"&filename&"[/url]'</script>"
        else
            response.write "<script>parent.form1.content.value+=' "&filename&" '</script>"
        end if
        icount=icount+1
set file=nothing
end if
next
set upload=nothing '刪除此對象
response.write("檔案上傳成功!<a href=""upload.asp?act=upload"">繼續上傳</a>")
%>
</body>
</html>
<%end if%>

<%call closeall%>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.