文档库 最新最全的文档下载
当前位置:文档库 › 动态网页开发基础实训指导书第十三次实训

动态网页开发基础实训指导书第十三次实训

动态网页开发基础实训指导书第十三次实训
动态网页开发基础实训指导书第十三次实训

动态网页开发基础实训指导书第十三次实训

————————————————————————————————作者:————————————————————————————————日期:

实训十三ASP+access实现无组件上传图片本次实训的最终目的是将图片文件上传到文件夹,并将文件名写入数据库中。

一、建立数据库my,建表myfile ,表中有四个字段 id(自动编号),upname(文本,表示上传者名字),(文本,表示上传后的文件名),uptime(日期/时间,表示上传的时间)。

二、建立一个名为myfile的文件夹,用来放上传后的图片。

三、代码部分

1、upload_5xsoft.asp (这是一个写好的上传类,一会将该文件包含进去,只要会用就可以了)

<%

dim Data_5xsoft

Class upload_5xsoft

dimobjForm,obj

Publicfunction Form(strForm)

strForm=lcase(strForm)

if notobjForm.exists(strForm) then

Form=""

else

Form=objForm(strForm)

end if

end function

Publicfunction )

str(strFile)

if not obj(strFile) then

set

else

set (strFile)

end if

endfunction

Private Sub Class_Initialize

dim RequestData,sStart,vbCrlf,sInfo,iInfoStart,iInfoEnd,tStream,iStart,theFile

dim i

dim iFindStart,iFindEnd

dim iFormStart,iFormEnd,sFormName

Version="!小陈跑跑!"

setobjForm=Server.CreateObject("Scripting.Dictionary")

set obj("Scripting.Dictionary")

ifRequest.TotalBytes&lt;1 then Exit

Sub

settStream = Server.CreateObject("ad odb.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(iFor mStart,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,iFindS tart,iFindEnd-iFindStart))

'如果是文件

if InStr (45,sInfo,"""",1)> 0 then

setthe

'取得文件名

iFindStart =InStr(iFindEnd,sInfo,"""",1)+10

iFindEnd= InStr(iFindStart,sInfo,"""",1)

s =Mid (sinfo,iFindStart,iFindEnd-iFindS tart)

's = replace(cstr(now()),":","")

the(s)

the(s)

'取得文件类型

iFindStart = InStr(iFindEnd,sInfo,"C ontent-Type: ",1)+14

iFindEnd = InStr(iFindStart,sInfo,vbCr)

the =Mid (sinfo,iFindStart,iFindEnd-iFin dStart)

the=iInfoEnd

the = iFormStart -iInfoEnd -3

the

if not obj(sFormName) then

obj sFormName,theFile

end if

else

'如果是表单项目

tStream.Type=1

tStream.Mode=3

tStream.Open

Data_5xsoft.Position =iInfoEnd

Data_5xsoft.CopyTo tStream,iFormStart-iInfoEn d-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=""

settStream =nothing

End Sub

Private Sub Class_Terminate

ifRequest.TotalBytes>0 then

objForm.RemoveAll

obj

set objForm=nothing

setobj

Data_5xsoft.Close

set Data_5xsoft =nothing

endif

EndSub

Private function Get(FullPath)

If FullPath <> ""Then

Get= left(FullPath,InStrRev(FullPath, "\"))

Else

Get = ""

EndIf

End function

Private functionGet(FullPath)

Randomize

If FullPath <> ""Then

'Get =replace(replace(replace(cstr(now()),":",""),"",""),"-","")&amp; mid(FullPath,InStrR ev(FullPath, "\")+1)

tmp =mid(FullPath,InStrRev(FullPath, "\")+1) tmp_instr=len(tmp)-InStrRev(tmp,".")+1 tmp(tm p)

Get =replace(replace(replace(cstr(now()),":",""),"",""),"-","")&amp; Cstr(Int(10000000*Rnd ))& "VKEY" &tmp

Else

Get = ""

End If

End function

End Class

Class

dim FormName,

Private Sub Class_Initialize

= ""

= ""

= 0

FormName= ""

= ""

End Sub

Public function SaveAs(FullPath)

dimdr,ErrorChar,i

SaveAs=true

if trim(fullpath)=""or or""or right(fullpath,1)="/" then exit functionset dr=CreateObject("Adodb.Stream")

dr.Mode=3

dr.Type=1

dr.Open

Data_5xsoft.position=

Data_5xsoft.copyto dr,

dr.SaveToFileFullPath,2

dr.Close

set dr=nothing

SaveAs=false

end function

EndClass

%>

二、conn.asp(数据库连接代码,一会包含使用)

<%

'打开数据库连接 db="my"

Set conn = Server.CreateObject("ADODB.Connection") connstr="Provider=Microsoft.Jet.OLEDB.4.0;Data Source="& Server.MapPath(""&db&"")

'如果你的服务器采用较老版本Access驱动,请用下面连接方法

'connstr="driver={Microsoft Access Driver (*.mdb)};dbq=" & Server.MapPath(db)conn.Open connstr %>

三、index.asp

<%

set rs=server.CreateObject("adodb.recordset") sql="select* from myfile" ‘表’ rs.ope

n sql,conn,1,1 do while not rs.eof%> <tr> 上传者:</td>

图片文件:</td>

<td width="167">" target='_blank' >点击查看‘文件夹’ 上传时间:</td>

</tr> <%

rs.movenext loop

rs.close set rs=nothing conn.close set con n=nothing %>

<tr>

<td width="92">上传者:

<tr>

<td>上传图片:</td>

</tr>

</td>

<%

'这是处理提交过来的数据的页面

formpath="myfile/" '设置上传的文件夹名 url="index.asp" '上传完后,返回到的页面。

set upload=new upload_5xsoft '实例化一个上传对象upload,这个无组件上传类最大只能上200K的文件,上传超过200K文件,将会非常的慢,如果想上传更大的文件,可以使用有组件上传

function up_pic() '这是一个上传函数 iCount=0'记录上传图片数

foreach formName in upload.objFile '列出所有上传了的文件 set (formName) '生成一个文件对象 int if >0 and <=262144 then '如果 > 0 说明有文件数据

= LCase(Right()) '最文件名的后四位字符,并转成小写if ".jpg" or ".gif" or ".GIF" or ".JPG"then '判断图片的格式,你也可以去掉不判断 Pic_Name= '将文件名赋值给变量Pic_Name

Server.mappath(formPath&) ''保存文件 i Count=iCount+1 else

response.write ""

response.write "alert('你上传的图片格式不正确');" response.write" history.go(-1);"response.write""

response.end end if

else if >262144 then '如果文件大于256K,这个数值你可以自己算256×1024

response.write "<script language=javascript>"

response.write "alert('上传的图片大于256K,上传失败!');" response.write" history.go(-1);" response.writ e "" response.end end if

end if

set

next

up_pic=Pic_Name '本函数的作用是,将上传的图片上传到文件夹中,并将文件名返回 end function

picname=up_pic() '调用up_pic()函数,返回文件名set rs=server.CreateObject("adodb.recordse

t") '建立一个记录集对象rs sql="select * from myfile"

rs.open sql,conn,1,3 '打开记录集参数1,3表示可以修改方式打开

rs.addnew

rs("upname")=trim(upload.form("upname")) '这里要注意,必须用upload.form来获取表单的值,和平常不一样,不能用request.form rs("")=picname

rs("uptime")=now() rs.update

rs.close '使用完后关闭记录集 set rs=nothing

conn.close '关闭数据库连接对象set conn=nothin g

%>

相关文档 最新文档