asp圖片上傳組件 查看:136 回復:0 |
AspUpload组件上传代码:
表单页面: <%Sub add()image1=request.form("image1")image2=request.form("image2")image3=request.form("image3")if image1=""or image2=""or image3="" then response.write"所有的项目都必须填写 <a href=j avascript:history.go(-1)>返回</a>"response.end end if Conn.execute("insert into imagedata(img_100,img_800,img_1024)values('"&image 1&"','"&image2&"','"&image3&"')")response.redirect "Admin_Image.Asp?Temp=添加成功 "&image3 End Sub%> <table border="1" width="550" bordercolorlight="#808080" cellspacing="0"style="border-collapse: collapse"bgcolor="#F6F6F6"cellpadding="5"> <%sub addimg()%> <form method="POST"action="?linzi=add"name="form 1"> <tr> <td width="789"bgcolor="#C0C0C0"colspan="4"height="21"> <b>添加图片</b></td></tr> <tr> <td width="98"align="right">缩略图:</td> <td width="429"colspan="3"><input type="text"name="image 1" size="36"></td> </tr> <tr> <td align="right">800*600图:</td> <td colspan="3"><input type="text"name="image2" size="36"></td> </tr> <tr> <td align="right">1024*768图:</td> <td colspan="3"><input type="text"name="image3" size="36"></td> </tr> <tr> <td align="right">图片上传:</td> <td colspan="3"> <iframe name="I 1" src="Admin_Upload.Asp" width="372" height="25" frameborder=0scrolling=no></iframe></td> </tr> <tr> <td align="right"> </td> <td colspan="3"><input type="submit"value="提交"name="B 1"></td> </tr></form> 1/9页 <%end sub%> </table> Admin_Upl o ad.As p <%Const UpFilesPath=". ./UploadFile"'存放上传文件的目录%> <table width="100%"border="0"cellspacing="0"cellpadding="0"bordercolor="#CCCCCC"><tr> <form name="form"method="post"acti on="Admin_Up file.Asp"enctype="multipart/form-data"><td valign="middle"><input type="file"name="file 1" size="24"> <input type="hidden"name="filepath"value="<%=UpFilesPath%>/"> <input type="submit" name="Submit" value=" 上 传 "oncli ck="parent.do cument.form 1.Sub mit.dis able d=true,parent.document.form 1.Submit2.dis able d =true;"></td></form> </tr> </table> Admin_Up fil e.As p <!--#include file="Admin_Upinc.Asp"--> <% '上传文件大小限制 Const MaxFileSize=5000000 '允许的上传文件类型 Const UpFileType="gif|jpg|doc|swf|rar|zip|exe" %> <table width="100%"border="0"cellspacing="0"cellpadding="0"bordercolor="#CCCCCC"><tr> <td width="388"align="center"valign="middle"> <%dim upload,file,formName,formPath,filename,fileExt set upload=new upload_5xSoft ''建立上传对象formPath=upload.form("filepath")methods=upl oad.form("metho ds") '在目录后加(/)if right(formPath,1)<>"/" then formPath=formPath&"/" '按月份自动明名上传文件夹,需要组件支持。 Dim objFSO,Fsofolder,uploadpath uploadpath=year(now)&"-"&month(now) '以年月创建上传文件夹格式 20048On Error Resume Next Set objFSO=Server.CreateObject("Scripting.FileSystemObject") If objF SO.FolderExists(Server.MapPath(FormPath&uploadpath))=False Then 2/9页 objFSO.CreateFolder Server.MapPath(FormPath&uploadpath) End If If Err.Number=0 Then CreatePath=uploadpath&"/" Else CreatePath="" End If set obj F SO=nothing for each formName in upload.file''列出所有上传了的文件set file=upload.file(formName) ''生成一个文件对象if file.filesize<1 then response.write "<font class=14p>请先选择你要上传的文件 [ <a onclick=history.go(-1)style='cursor:hand'class=14p>重新上传</a>]</font>"response.end end if if file.filesize>(MaxFileSize*1024) then response.write "<font class=14p>文件大小超过了限制"&CStr(MaxFileSize)&"K [ <a onclick=history.go(-1) style='cursor:hand'class=14p>重新上传</a>]</font>"response.end end if fileExt=lcase(right(file.filename,3))arrUpFileType=split(UpFileType,"|")for i=0 to ubound(arrUpFileType)if fileEXT=trim(arrUpFileType(i)) then EnableUpload=true exit for end if next if fileEXT="asp"or fileEXT="asa"or fileEXT="aspx" then EnableUpload=false end if if EnableUpload=false then response.write "<font class=14p>只允许上传 " &UpFileType &"文件类型 [ <a onclick=history.go(-1) style='cursor:hand'class=14p>重新上传</a>]</font>"response.end end if rando mi z e ranNum=int(90000*rnd)+10000 Showname=CreatePath&year(now)&month(now)&day(now)&ranNum&"."&fileExt filename=formPath&S howname 3/9页 if file.FileSize>0 then''如果FileSize>0说明有文件数据file.SaveAs Server.mappath(FileName) ''保存文件if methods="" then response.write"<script>parent.document.form 1.image3.value='"&showname&"'</script>"end if end if set file=nothing next set upload=nothing''删除此对象 Response.Write "<a href=admin_upload.asp><span style='font-size: 12px'>上传成功 返回</span></a>" %> </td> </tr> </table> Admin_Upinc.As p <SCRIPT RUNAT=SERVER LANGUAGE=VB SCRIPT>dim upfile_5xSoft_Stream Class upload_5xSoft dim Form,File,Version Private Sub Class_Initialize dim i Start,iFileName Start,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=CreateObj ect("Scripting.Dictionary")set File=CreateObj ect("Scripting.Dictionary")set upfile_5xSoft_Stream=CreateObject("Adodb.Stream")upfile_5xSoft_Stream.mode=3upfile_5xSoft_Stream.type=1upfile_5xSoft_Stream.open upfile_5xSoft_Stream.write Request.BinaryRead(Request.TotalBytes)vbEnter=Chr(13)&Chr(10)iDivLen=inString(1,vbEnter)+1strDiv=subString(1,iDivLen)iFormStart=iDivLen iFormEnd=inString(iformStart,strDiv)-1while iFormStart<iFormEnd 4/9页 i Start=inString(iF ormStart,"name=""")iEnd=inString(i Start+6,"""")mF ormName=sub String(i Start+6,iEnd-i Start-6)iFileNameStart=inString(iEnd+1,"filename=""")if iFileNameStart>0 and iFileNameStart<iFormEnd then iFileNameEnd=inString(iFileName Start+10,"""")mFileName=sub String(iFileName Start+10,iFileNameEnd-iFileName Start-10)i Start=inString(iFileNameEnd+1,vbEnter&vbEnter)iEnd=inString(i Start+4,vbEnter&strDiv)if iEnd>iStart then mFileSize=iEnd-iStart-4else mFileSize=0end if set theFile=new FileInfo the F i l e.F i l eN a me=g etF i l eN a me(mF i l eN a me)theFile.FilePath=getFilePath(mFileName)theFile.FileSize=mFileSize theFile.File Start=i Start+4theFile.FormName=FormName file.add mFormName,theFile else iStart=inString(iEnd+1,vbEnter&vbEnter)iEnd=inString(i Start+4,vbEnter&strDiv)if iEnd>iStart then mFormValue=sub String(iStart+4,iEnd-iStart-4)else mFormValue=""end if form.Add mFormName,mFormValue end if iFormStart=iformEnd+iDivLen iFormEnd=inString(iformStart,strDiv)-1wend End Sub Private Function sub String(the Start,theLen)dim i,c,stemp upfile_5xS oft_Stream.Position=the Start-1stemp=""for i=1 to theLen if upfile_5xSoft_Stream.EOS then Exit for c=ascB(upfile_5xSoft_Stream.Read(1)) 5/9页 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+1else stemp=stemp&Chr(c) End If Next subString=stemp End function Private Function inString(the Start,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-1if 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 6/9页 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=mi d(FullPath,InStrRev(FullPath, "\")+1)Else G etF i l eN a me="" 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 F i l eN a me="" FilePath="" FileSize=0 File Start=0 FormName="" End Sub 7/9页 Public function SaveAs(FullPath)dim dr,ErrorChar,i SaveAs=1if 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=3dr.Typ e=1dr.Open upfile_5xSoft_Stream.position=FileStart-1upfile_5xSoft_Stream.copyto dr,FileSize dr.SaveToFile FullPath,2dr.Close set dr=nothing SaveAs=0end function End Class </S CRIP T> YouUpload组件上传代码: 表单页面: <form name="upload"action="upload.asp"method="post"enctype="multipart/form-data"><table align=center border=0 width=608> <br><br> <tbody> <tr> <td colspan=2> <span class="style1">选取资料 </span> <input name="file 1" type=file class="textarea" size=50 maxlength=100> </td> </tr> <br><br> <tr><td align=center colspan=2><br><br> <span class="style1"></span> <input class=button name="save" type=submit value=上传> <span class="style 1"><font color=red>(确定选取了资料后再上传)</font></span></td></tr> </tb o dy> </table> </form> 上传页面: <% Set Obj=Server.CreateObject("Yousoft.UploadFile") FileName=obj.FileName("File 1") 8/9页 session("filename")=FileName 'obj.SaveFile"File 1", "e:\bbkav-1\engin\ziliao\"&FileName obj.SaveFile"File 1", "e:\bbkav-1\engin\ziliao\"&FileName 'obj.SaveFile"File 1", "e:\web\market\engin\ziliao\"&FileName set obj=nothing%> |