asp实现上传功能的源码演示(简单易懂)

上传人:wt****50 文档编号:32725610 上传时间:2018-02-12 格式:DOC 页数:9 大小:57KB
返回 下载 相关 举报
asp实现上传功能的源码演示(简单易懂)_第1页
第1页 / 共9页
asp实现上传功能的源码演示(简单易懂)_第2页
第2页 / 共9页
asp实现上传功能的源码演示(简单易懂)_第3页
第3页 / 共9页
asp实现上传功能的源码演示(简单易懂)_第4页
第4页 / 共9页
asp实现上传功能的源码演示(简单易懂)_第5页
第5页 / 共9页
点击查看更多>>
资源描述

《asp实现上传功能的源码演示(简单易懂)》由会员分享,可在线阅读,更多相关《asp实现上传功能的源码演示(简单易懂)(9页珍藏版)》请在金锄头文库上搜索。

1、asp 无组件上传图片的源码分析及下载要实现图片的上传功能其实非常简单,只需要四个文件与一个文件夹即可实现。第一个文件:Upload.asp 这个文件的源代码如下:说明:这个文件将引用一个名为 upload_Photo.asp 的文件也就是下面要讲的第二个文件作为框架,同时这里传递一个 PhotoUrlID 参数给 pload_Photo.asp 这个文件的 form1 表单中的名为 PhotoUrlID 的隐藏域名,其值为 1第二个文件:Upload_Photo.asp,这个文件的源代码如下:function check() var strFileName=document.form1.Fi

2、leName.value;if (strFileName=)alert(请选择要上传的文件);document.form1.FileName.focus();return false; 说明:这个是作为第一个文件 Upload.asp 的框架文件,注意这里的 FileName 的文本框是用于接受上传成功之后图片的地址第三个文件:Upfile_Photo.asp,其源代码如下:0 then 如果出错select case upload.errcase 1response.write 请先选择你要上传的文件!case 2response.write 你上传的文件总大小超出了最大限制(200k)en

3、d selectresponse.endend ifPhotoUrlID=Clng(trim(upload.form(PhotoUrlID)if PhotoUrlID0 thenSavePath = UploadFiles 存放上传文件的目录elseSavePath = UploadFiles 存放上传文件的目录end ifif right(SavePath,1)/ then SavePath=SavePath&/ 在目录后加(/)for each formName in upload.file 列出所有上传了的文件set ofile=upload.file(formName) 生成一个文件对

4、象oFileSize=ofile.filesizeif oFileSize(MaxFileSize*1024) thenmsg=文件大小超过了限制,最大只能上传 & CStr(MaxFileSize) & K 的文件!FoundErr=trueend ifcase 1if oFileSize(10000*1024) thenmsg=文件大小超过了限制,最大只能上传 10M 的文件!FoundErr=trueend ifend selectend iffileExt=lcase(ofile.FileExt)arrUpFileType=split(gif|jpg|bmp|png|swf|doc|r

5、ar|xls,|)如需添加其它类型的文档请在这里添加文档后缀名for i=0 to ubound(arrUpFileType)if fileEXT=trim(arrUpFileType(i) thenEnableUpload=trueexit forend ifnextif fileEXT=asp or fileEXT=asa or fileEXT=aspx thenEnableUpload=falseend ifif EnableUpload=false thenmsg=这种文件类型不允许上传!nn 只允许上传这几种文件类型: & gif|jpg|bmp|png|swf|doc|rar|xl

6、sFoundErr=trueend ifstrJS= & vbcrlfif FoundErrtrue thenrandomizeranNum=int(900*rnd)+100filename=SavePath&year(now)&month(now)&day(now)&hour(now)&minute(now)&second(now)&ranNum&.&fileExtofile.SaveToFile Server.mappath(FileName) 保存文件response.write 文件上传成功!文件大小为: & cstr(round(oFileSize/1024) & Kselect c

7、ase PhotoUrlIDcase 0strJS=strJS & parent.document.myform.PhotoSize1.value= & cstr(round(oFileSize/1024) & vbcrlfcase 1strJS=strJS & parent.document.myform.DefaultPicUrl.value= & fileName & vbcrlfend selectelsestrJS=strJS & vbcrlfstrJS=strJS & vbcrlfend ifstrJS=strJS & & vbcrlfresponse.write strJSset

8、 file=nothingnextset upload=nothingend sub%第四个文件 upfile_class.asp,源代码如下0 Then 如果限制大小If Request.TotalBytes MaxSize ThenErr = 2 如果上传的数据超出限制Exit SubEnd IfEnd IfSet Form = Server.CreateObject (Scripting.Dictionary)Form.CompareMode = 1Set File = Server.CreateObject (Scripting.Dictionary)File.CompareMode

9、= 1Set tStream = Server.CreateObject (ADODB.Stream)Set oUpFileStream = Server.CreateObject (ADODB.Stream)oUpFileStream.Type = 1oUpFileStream.Mode = 3oUpFileStream.Open oUpFileStream.Write Request.BinaryRead (Request.TotalBytes)oUpFileStream.Position = 0RequestBinData = oUpFileStream.Read iFormEnd =

10、oUpFileStream.SizebCrLf = ChrB (13) & ChrB (10)取得每个项目之间的分隔符sSpace = MidB (RequestBinData,1, InStrB (1,RequestBinData,bCrLf)-1)iStart = LenB (sSpace)iFormStart = iStart+2分解项目Do iInfoEnd = InStrB (iFormStart,RequestBinData,bCrLf & bCrLf)+3tStream.Type = 1tStream.Mode = 3tStream.OpenoUpFileStream.Posit

11、ion = iFormStartoUpFileStream.CopyTo tStream,iInfoEnd-iFormStarttStream.Position = 0tStream.Type = 2tStream.CharSet = gb2312sInfo = tStream.ReadText 取得表单项目名称iFormStart = InStrB (iInfoEnd,RequestBinData,sSpace)-1iFindStart = InStr (22,sInfo,name=,1)+6iFindEnd = InStr (iFindStart,sInfo,1)sFormName = M

12、id (sinfo,iFindStart,iFindEnd-iFindStart)如果是文件If InStr (45,sInfo,filename=,1) 0 ThenSet oFileInfo = new FileInfo_Class取得文件属性iFindStart = InStr (iFindEnd,sInfo,filename=,1)+10iFindEnd = InStr (iFindStart,sInfo,1)sFileName = Mid (sinfo,iFindStart,iFindEnd-iFindStart)oFileInfo.FileName = Mid (sFileName

13、,InStrRev (sFileName, )+1)oFileInfo.FilePath = Left (sFileName,InStrRev (sFileName, )oFileInfo.FileExt = Mid (sFileName,InStrRev (sFileName, .)+1)iFindStart = InStr (iFindEnd,sInfo,Content-Type: ,1)+14iFindEnd = InStr (iFindStart,sInfo,vbCr)oFileInfo.FileType = Mid (sinfo,iFindStart,iFindEnd-iFindSt

14、art)oFileInfo.FileStart = iInfoEndoFileInfo.FileSize = iFormStart -iInfoEnd -2oFileInfo.FormName = sFormNamefile.add sFormName,oFileInfoelse如果是表单项目tStream.ClosetStream.Type = 1tStream.Mode = 3tStream.OpenoUpFileStream.Position = iInfoEnd oUpFileStream.CopyTo tStream,iFormStart-iInfoEnd-2tStream.Posi

15、tion = 0tStream.Type = 2tStream.CharSet = gb2312sFormValue = tStream.ReadText If Form.Exists (sFormName) ThenForm (sFormName) = Form (sFormName) & , & sFormValueelseform.Add sFormName,sFormValueEnd IfEnd IftStream.CloseiFormStart = iFormStart+iStart+2如果到文件尾了就退出Loop Until (iFormStart+2) = iFormEnd RequestBinData = Set tStream = NothingEnd SubEnd Class-文件属性类Class FileInfo_ClassDim FormName,FileName,FilePath,FileSize,FileType,FileStart,FileExt保存文件方法Public Function SaveToFile (Path)if lcase(right(Path,3)lcase(

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 建筑/环境 > 建筑机械

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号