无组件上传图片之文件采用方案

2006-07-15 12:35:07

首先,图片在页面中能查找选择。设计表单页面index.asp和上传选择页upload.asp,upload.asp在index.asp中以iframe包含。 其次,所选图片应能上传到某文件夹。建立一文件夹uploadimg 最后,传上去的图片应如何引用?很显然,采用UBB立即显示。upload.asp的指向对象upfile.asp具有写入UBB标签的功能。 无组件上传调试文件夹 -- index.asp -- upload.asp -- upfile.asp -- uploadimg文件夹 图片上传采用稻香老农的无组件上传。所以upload.inc文件必不可少。 1,upload.inc(拷贝以下文本框的所有代码) [code]<SCRIPT RUNAT=SERVER LANGUAGE=VBSCRIPT> dim upfile_5xSoft_Stream Class upload_5xSoft dim Form,File,Version Private Sub Class_Initialize dim iStart,iFileNameStart,iFileNameEnd,iEnd,vbEnter,iFormStart,iFormEnd,theFile dim strDiv,mFormName,mFormValue,mFileName,mFileSize,mFilePath,iDivLen,mStr Version="" if Request.TotalBytes<1 then Exit Sub set Form=CreateObject("Scripting.Dictionary") set File=CreateObject("Scripting.Dictionary") set upfile_5xSoft_Stream=CreateObject("Adodb.Stream") upfile_5xSoft_Stream.mode=3 upfile_5xSoft_Stream.type=1 upfile_5xSoft_Stream.open upfile_5xSoft_Stream.write Request.BinaryRead(Request.TotalBytes) vbEnter=Chr(13)&Chr(10) iDivLen=inString(1,vbEnter)+1 strDiv=subString(1,iDivLen) iFormStart=iDivLen iFormEnd=inString(iformStart,strDiv)-1 while iFormStart < iFormEnd iStart=inString(iFormStart,"name=""") iEnd=inString(iStart+6,"""") mFormName=subString(iStart+6,iEnd-iStart-6) iFileNameStart=inString(iEnd+1,"filename=""") if iFileNameStart>0 and iFileNameStart<iFormEnd then iFileNameEnd=inString(iFileNameStart+10,"""") mFileName=subString(iFileNameStart+10,iFileNameEnd-iFileNameStart-10) iStart=inString(iFileNameEnd+1,vbEnter&vbEnter) iEnd=inString(iStart+4,vbEnter&strDiv) if iEnd>iStart then mFileSize=iEnd-iStart-4 else mFileSize=0 end if set theFile=new FileInfo theFile.FileName=getFileName(mFileName) theFile.FilePath=getFilePath(mFileName) theFile.FileSize=mFileSize theFile.FileStart=iStart+4 theFile.FormName=FormName file.add mFormName,theFile else iStart=inString(iEnd+1,vbEnter&vbEnter) iEnd=inString(iStart+4,vbEnter&strDiv) if iEnd>iStart then mFormValue=subString(iStart+4,iEnd-iStart-4) else mFormValue="" end if form.Add mFormName,mFormValue end if iFormStart=iformEnd+iDivLen iFormEnd=inString(iformStart,strDiv)-1 wend End Sub Private Function subString(theStart,theLen) dim i,c,stemp upfile_5xSoft_Stream.Position=theStart-1 stemp="" for i=1 to theLen if upfile_5xSoft_Stream.EOS then Exit for c=ascB(upfile_5xSoft_Stream.Read(1)) If c > 127 Then if upfile_5xSoft_Stream.EOS then Exit for stemp=stemp&Chr(AscW(ChrB(AscB(upfile_5xSoft_Stream.Read(1)))&ChrB(c))) i=i+1 else stemp=stemp&Chr(c) End If Next subString=stemp End function Private Function inString(theStart,varStr) dim i,j,bt,theLen,str InString=0 Str=toByte(varStr) theLen=LenB(Str) for i=theStart to upfile_5xSoft_Stream.Size-theLen if i>upfile_5xSoft_Stream.size then exit Function upfile_5xSoft_Stream.Position=i-1 if AscB(upfile_5xSoft_Stream.Read(1))=AscB(midB(Str,1)) then InString=i for j=2 to theLen if upfile_5xSoft_Stream.EOS then inString=0 Exit for end if if AscB(upfile_5xSoft_Stream.Read(1))<>AscB(MidB(Str,j,1)) then InString=0 Exit For end if next if InString<>0 then Exit Function end if next End Function Private Sub Class_Terminate form.RemoveAll file.RemoveAll set form=nothing set file=nothing upfile_5xSoft_Stream.close set upfile_5xSoft_Stream=nothing End Sub Private function GetFilePath(FullPath) If FullPath <> "" Then GetFilePath = left(FullPath,InStrRev(FullPath, "\")) Else GetFilePath = "" End If End function Private function GetFileName(FullPath) If FullPath <> "" Then GetFileName = mid(FullPath,InStrRev(FullPath, "\")+1) Else GetFileName = "" End If End function Private function toByte(Str) dim i,iCode,c,iLow,iHigh toByte="" For i=1 To Len(Str) c=mid(Str,i,1) iCode =Asc(c) If iCode<0 Then iCode = iCode + 65535 If iCode>255 Then iLow = Left(Hex(Asc(c)),2) iHigh =Right(Hex(Asc(c)),2) toByte = toByte & chrB("&H"&iLow) & chrB("&H"&iHigh) Else toByte = toByte & chrB(AscB(c)) End If Next End function End Class Class FileInfo dim FormName,FileName,FilePath,FileSize,FileStart Private Sub Class_Initialize FileName = "" FilePath = "" FileSize = 0 FileStart= 0 FormName = "" End Sub Public function SaveAs(FullPath) dim dr,ErrorChar,i SaveAs=1 if trim(fullpath)="" or FileSize=0 or FileStart=0 or FileName="" then exit function if FileStart=0 or right(fullpath,1)="/" then exit function set dr=CreateObject("Adodb.Stream") dr.Mode=3 dr.Type=1 dr.Open upfile_5xSoft_Stream.position=FileStart-1 upfile_5xSoft_Stream.copyto dr,FileSize dr.SaveToFile FullPath,2 dr.Close set dr=nothing SaveAs=0 end function End Class </SCRIPT> [/code] 2,表单页面index.asp。注意框架包含的上传选择页upload.asp [code]<form name="form_name" method="POST" action="add.asp"> <textarea cols="100" name="cn_content" rows="18" width="100%"></textarea> </form> <iframe border="0" frameBorder="0" noResize scrolling="no" width="100%" src="upload.asp"></iframe> [/code] 3,上传选择页upload.asp 注意: enctype="multipart/form-data" [code]<form name="form" method="post" action="upfile.asp" enctype="multipart/form-data"> <input type="hidden" name="filepath" value="uploadimg"> <input type="hidden" name="act" value="upload"> <input type="file" name="file1" size=40> <input type="submit" class=button name="Submit" value="上传图片" onclick="parent.document.forms[0].Submit.disabled=true">类型:gif,jpg,限制:100K </form> [/code]4,最后一 个文件 upfile.asp 主要作用:生成图片名,并将图片上传,同样也要将UBB标签写入index.asp中的textarea中。 (拷贝以下文本框的所有代码) [code]sub HtmEnd(Msg) set upload=nothing response.write "上传成功 [ <a href=# onclick=history.go(-1)>继续上传</a>]" response.end end sub %> </body> </html> [/code]当然,保持图片的文件夹uploadimg不能少 ---------------------------------------------------------------------- 不过我这里是重新编辑的无组件上传,增添了如下内容 1,支持图片、音频、视频、压缩等各类型文件的上传(可自定义限制的格式文件) 2,对不同类型文件的文件夹归类(按照文件类型建立文件夹,并按“年-月”建立子文件夹) 3,上传的文件名不再采用流水号(这样对搜索不好),当然也加了随机数 4,还有功能程式的说明,具体看文件内容 声明:既然是上传,一般是需要相关上传组件的支持的,而无组件上传则是由网络上盛传的稻香老农的无组件上传类,目前版本为V2.0,该文件内容如下,拷贝代码,保存为 upload.inc [code] <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 if 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 if 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 end if 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 If End function Private function GetFileName(FullPath) If FullPath <> "" Then GetFileName = mid(FullPath,InStrRev(FullPath, "\")+1) Else GetFileName = "" End If 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> [/code]接着,需要一个正常的后台信息录入平台,该平台页面是通过信息内容的输入,并提供“浏览”表单元素以提供本地文件的上传,该文件内容如下,拷贝代码,保存为index.asp [code]<form name="cn_bruce" method="POST" action=""> <textarea cols="100" name="cn_content" rows="18" width="100%"></textarea><br> </form> <iframe border="0" frameBorder="0" noResize scrolling="no" width="100%" src="upload.asp"></iframe> [/code]其中,index.asp中的“浏览”表单元素的出现是在浮动框架内的upload.asp,用浮动框架是防止文件上传的同时与index.asp的信息干扰,该文件内容如下,拷贝代码,保存为upload.asp [code]<meta http-equiv="content-type" content="text/html;charset=gb2312"> <form name="form" method="post" action="upfile.asp" enctype="multipart/form-data"> <input type="hidden" name="upfilefolder" value="uploadfile"> <input type="hidden" name="act" value="upload"> <input type="file" name="file11" size=40> <input type="file" name="file12" size=40> <input type="file" name="file13" size=40> <input type="submit" name="Submit" value="上传"> </form> [/code]最后,最重要的文件就是如何将upload.asp选择的本地的文件传递到服务器上定义的文件夹中,action="upfile.asp",该文件内容如下,拷贝代码,保存为upfile.asp [code]<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%> <!--#include file="upload.inc"--> <html> <head> <title>文件上传</title> <meta http-equiv="content-type" content="text/html;charset=gb2312"> </head> <body> <% dim upload,f_folder,file,formPath,iCount,filename,fileExt,filesizemin,filesizemax filesizemin=100 filesizemax=200*1024 set upload=new upload_5xSoft '建立上传对象 f_folder=upload.form("upfilefolder") '********************************列出所有上传文件*************************************************** For each formName in upload.objFile set file=upload.file(formName) If file.filesize>0 then '********************************检测文件大小*************************************************** If file.filesize<filesizemin Then response.write "你上传的文件太小了 [ <a href=# onclick=history.go(-1)>重新上传</a> ]" ElseIf file.filesize>filesizemax then response.write "文件大小超过了 "&filesizemax&"字节 限制 [ <a href=# onclick=history.go(-1)>重新上传</a> ]" End If '********************************检测文件类型**************************************************** 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=# onclick=history.go(-1)>重新上传</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(f_folder&"/"&f_type&"/"&f_name))=False Then If upf.FolderExists(Server.MapPath(f_folder&"/"&f_type))=False Then If upf.FolderExists(Server.MapPath(f_folder))=False Then upf.CreateFolder Server.MapPath(f_folder) upf.CreateFolder Server.MapPath(f_folder&"/"&f_type) upf.CreateFolder Server.MapPath(f_folder&"/"&f_type&"/"&f_name) Else upf.CreateFolder Server.MapPath(f_folder&"/"&f_type) upf.CreateFolder Server.MapPath(f_folder&"/"&f_type&"/"&f_name) End If Else upf.CreateFolder Server.MapPath(f_folder&"/"&f_type&"/"&f_name) End If End If f_ftn=f_folder&"/"&f_type&"/"&f_name Set upf=Nothing '********************************保存上传文件至文件夹***************************************** randomize ranNum=int(90000*rnd)+10000 filename=f_ftn&"/"&day(now)&"-"&ranNum&"-"&file.filename if file.filesize>filesizemin and file.filesize<filesizemax then file.SaveAs Server.mappath(filename) '保存文件 if f_type="JPG" or f_type="GIF" or f_type="PNG" then response.write "<script>parent.cn_bruce.cn_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.cn_bruce.cn_content.value+='[url]"&filename&"[/url]'</script>" 'ElseIf else response.write "<script>parent.cn_bruce.cn_content.value+=' "&filename&" '</script>" end if iCount=iCount+1 end if set file=nothing end if next set upload=nothing '删除此对象 response.write (iCount&" 个文件上传成功! <a href=# onclick=history.go(-1)>继续上传</a>") %> </body> </html> [/code] 注意: 1,需要服务器支持FSO,否则文件夹建立不了; 2,在文件上传后最终在index.asp的文本区域中显示什么内容,由您的编辑器类型决定 比如支持UBB,则使用UBB标签,支持HTML标签,则将upfile.asp中parent.cn_bruce.cn_content.value+= 后的内容修改 调试地址:http://www.164.cc/2006/upf/ 另外,对于已经上传的文件,随着时间的漫长,内容的更替,这些上传文件就需要管理 内容参看:(ASP)文件系统之文件管理 http://www.cnbruce.com/blog/showlog.asp?log_id=928&cat_id=26