免费预览已结束,剩余11页可下载查看
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
实训十三 ASP+access实现无组件上传图片本次实训的最终目的是将图片文件上传到文件夹,并将文件名写入数据库中。一、建立数据库myfile.mdb,建表myfile,表中有四个字段id(自动编号),upname(文本,表示上传者名字),filename(文本,表示上传后的文件名),uptime(日期/时间,表示上传的时间)。二、建立一个名为myfile的文件夹,用来放上传后的图片。三、代码部分1、upload_5xsoft.asp(这是一个写好的上传类,一会将该文件包含进去,只要会用就可以了)% 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=!小陈跑跑! 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) 0 then set theFile=new FileInfo 取得文件名 iFindStart = InStr(iFindEnd,sInfo,filename=,1)+10 iFindEnd = InStr(iFindStart,sInfo,1) sFileName =Mid (sinfo,iFindStart,iFindEnd-iFindStart) sFileName = replace(cstr(now(),:,) 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.TotalBytes0 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) Randomize If FullPath Then GetFileName =replace(replace(replace(cstr(now(),:,),),-,) & mid(FullPath,InStrRev(FullPath, )+1) tmpFile_Name = mid(FullPath,InStrRev(FullPath, )+1) tmp_instr=len(tmpFile_Name)-InStrRev(tmpFile_Name,.)+1 tmpFile_Name=right(tmpFile_Name,tmp_instr) GetFileName =replace(replace(replace(cstr(now(),:,),),-,) & Cstr(Int(10000000* Rnd )& VKEY & tmpFile_Name 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 %二、conn.asp(数据库连接代码,一会包含使用)三、index.asp上传者:图片文件:ahref=myfile/target=_blank点击查看文件夹上传时间:上传者:上传图片:四、postnew.asp(处理表单数据的文件)0andfile.filesize0说明有文件数据FileNameext=LCase(Right(file.fileName,4)最文件名的后四位字符,并转成小写ifFileNameext=.jpgorFileNameext=.giforFileNameext=.GIForFileNameext=.JPGthen判断图片的格式,你也可以去掉不判断Pic_Name=file.fileName将文件名赋值给变量Pic_Namefile.SaveAsServer.mappath(formPath&file.FileName)保存文件iCount=iCount+1elseresponse.writeresponse.writealert(你上传的图片格式不正确);response.writehistory.go(-1);response.writeresponse.endendifelseiffile.filesize262144then如果文件大于256K,这个数值你可以自己算2561024response.writeresponse.writealert(上传的图片大于256K,上传失败!);response.writehistory.go(-1);response.writeresponse.endendifendifsetfile=nothingnextup_pic=Pic_Name本函数的作用是,将上传的图片上传到文件夹中,并将文件名返回endfunctionpicname=up_pic()调用up_pic()函数,返回文件名setrs=server.CreateObject(adodb.recordset)建立一个记录集对象rssql=select*frommyfilers.opensql,conn,1,3打开记录集参数1,3表示可以修改方式打开rs
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 土方开挖施工方案及开挖顺序
- 声屏障现场安装施工方案
- 保温外墙一体化板施工方案
- 高大模板专项施工方案流程规范
- 屋顶彩钢瓦施工质量管理方案
- 河道清淤工程实施组织管理方案
- 具身智能+学校智能安防机器人应用场景方案可行性报告
- 外墙施工吊篮安装技术措施方案
- 厂房围墙施工方案
- 具身智能在灾害救援场景的协同方案可行性报告
- 院感爆发知识培训课件
- 高一期末生物试卷及答案
- 起重吊装安全作业专项方案
- 跨境电商 的物流
- 成人肠造口护理团体标准
- 报批报建流程
- 音标表48个国际音标发音教学视频
- 2024年度人工智能数据采集与算法优化服务合同3篇
- 部编版高中语文必修下册第二单元检测卷(含答案)
- 异构网络编址技术-洞察分析
- 四川省射洪市射洪中学校2024-2025学年七年级上学期期中考试地理试题
评论
0/150
提交评论