欢迎光临
我们一直在努力

多文件多文本框上传程序-ASP教程,ASP应用

建站超值云服务器,限时71元/月

接触asp也有将近一年时间,总想说一下自己的心得。正好今天单位工作不忙,我把我自己编写的多文件多文本框同时提交到服务器的纯asp上传程序整理了一下,发表出来。希望与大家共同交流。

程序分为三部分:

1、储存文件路径、文件说明、上传文件个数的一个数据库aaa.mdb,在这里我放在应用目录里mydatabase文件夹里

2、上传程序界面 gjimg.asp

3、纯asp上传主程序 khimg.asp 上传文件存放在upload文件夹里

+====================================================

数据库aaa.mdb

表tpic

字段 类型

id 自动

picpath 字段 储存文件的在服务器的绝对路径

piccou 字段 储存这次上传的第几个文件

pictxt 字段 储存文件的文字说明

表的每一行储存一个文件和它的文字说明

+=====================================================

以下代码是上传界面 gjimg.asp

申明一下:这个界面是我从一个叫“红涯”的网友那里得到,不过她没有提供上传代码,我根据她的界面编写了上传主程序。

——————————————————————-

<%@language="vbscript" codepage="936"%>

<html>

<head>

<meta http-equiv="content-type" content="text/html; charset=gb2312">

<title>投稿图片上传</title>

<script language="javascript">

function setid()

{ if(!window.form11.imgcou.value)

window.form11.imgcou.value=1;

aa=window.form11.imgcou.value;

if (aa>100) //限制为100个文件

{aa=100;

window.form11.imgcou.value=100;

}

str1=

for(j=1;j<=aa;j++)

str1+=<br> <font color="#003333" size="2" id="shuang">图片+j+:</font><input name="file+j+ " type="file" class="xian" style="height:20;width:221;"><br> <font color="#003333" size="2">图片说明+j+:</font> <input name="txt+j+" type="text" class="xian" style="height:20;width:200;"> <br><br>;

window.imgid.innerhtml=str1;

}

</script>

</head>

<body >

<center>

<table width="100%" border="0" cellpadding="0" cellspacing="0">

<tr>

<td height="25" colspan="2" align="center"><font color="#ff0000" size="2"><%=session("errmess11")%></font></td>

</tr>

<tr>

<td width="67%" height="25" align="right" ><font color="#003333" size="2">请输入上传图片的张数:</font>

</td>

<td width="33%" height="25" align="left" > <form method="post" action="" name="form11" id="form11">

<input name="imgcou" id="imgcou" type="text" style="height:20;width:20;" class="xian">

&nbsp;

<input name="tsd" type="button" value="设定" style="height:20;width:50;" class="xian" onclick="setid();">

</form></td>

</tr>

<form name="scm" action="khimg.asp" method="post" enctype="multipart/form-data">

<tr>

<td colspan="2" align="center" id="imgid"><br> <font color="#003333" size="2" id="shuang">图片1:</font>

<input name="file1" type="file" class="xian" style="height:20;width:221;">

<br> <font color="#003333" size="2">图片说明1:</font> <input name="txt1" type="text" class="xian" style="height:20;width:200;">

<br></td>

</tr>

<tr>

<td align="center" height="30" ></td>

<td align="left" ><input type="submit" name="fmdj" value="提交" style="height:20;width:40;" class="xian">

<input type="reset" name="submit2" value="重设" style="height:20;width:40;" class="xian"></td>

</tr>

</form>

</table>

</center>

</body>

</html>

+========================================================================

以下代码是纯asp上传主程序 khimg.asp 我已经在重要的地方都加了注释了。

————————————————————————-

<%@language="vbscript" codepage="936"%>

<%response.buffer=true

response.clear()

dim tfilestart,tfilesize,ttxt,errno,tid,tfileext

dim ulrequest

errno=0

set ulrequest=createobject("scripting.dictionary")

bytecount=request.totalbytes

requestbin=request.binaryread(bytecount) "读取表单所有二进制数据

builduploadrequest requestbin "调用上传主函数 builduploadrequest

set readfile=createobject("adodb.stream")

readfile.mode=3

readfile.type=1

readfile.open

readfile.write requestbin "把读取的二进制数据再存入一个adodb.stream对象中

select case errno

case 0 没有发生错误的情况下把文件存进指定目录,文件路径、文本、文件数量写入数据库,

connstr="dbq= "&server.mappath("mydatabase\aaa.mdb")&"; driver={microsoft access driver (*.mdb)}"

set conn=server.createobject("adodb.connection")

conn.open connstr

set rec=server.createobject("adodb.recordset")

sqlstr="select * from tpic "

rec.open sqlstr,conn,2,3 打开数据库

ulkey=ulrequest.keys 取出数据字典ulrequest的键

set addfile=createobject("adodb.stream")

addfile.mode=3

addfile.type=1再建立一个adodb.stream对象,以便拷贝readfile对象的文件数据。

aa=ubound(ulkey)取键的个数,个数代表这有文件和文本的总数。

cou=0

for i=0 to aa "每个循环写存入一个文件或一个文本写入数据库

addfile.open

if left(trim(ulkey(i)),4)="file" then "是文件数据

titem=ulkey(i)

tfilesize=ulrequest.item(titem).item("filesize")

tfilestart=ulrequest.item(titem).item("filestart")

tfileext=ulrequest.item(titem).item("fileext") 取得文件数据在 readfile中的位置,以及扩展名

readfile.position=tfilestart-1

readfile.copyto addfile,tfilesize 把 readfile里的文件数据拷贝入addfile

filepath=server.mappath("upload")

filename=year(now())&month(now())&day(now())&hour(now())&minute(now())&second(now())&i&tfileext

filepath=filepath&"\"&filename"给文件命名,我这里是用时间给文件命名。

vfilepath="upload\"&filename

addfile.savetofile filepath 文件存入upload文件夹

cou=cou+1

rec.addnew

rec.fields("picpath")=vfilepath

rec.fields("piccou")=cou "数据库写入文件信息以及是第几个文件

elseif left(trim(ulkey(i)),3)="txt" then "是文本

titem=ulkey(i)

ttxt=ulrequest.item(titem).item("myvalue")

rec.fields("pictxt")=ttxt "取文本存入数据库。

rec.update

end if

addfile.close

next

set addfile=nothing

set readfile=nothing

rec.close

conn.close

set rec=nothing

set conn=nothing

session("errmess22")="图片上传成功!"

response.redirect("upimg.asp")

case 1

readfile.close

set readfile=nothing

session("errmess11")="上传类型不正确!"

response.redirect("gjimg.asp")

case 2

readfile.close

set readfile=nothing

session("errmess11")="上传的文件中有未选图片的!"

response.redirect("gjimg.asp")

case 3

readfile.close

set readfile=nothing

session("errmess11")="上传的图片中,有图片超过200k了"

response.redirect("upimg.asp")

end select

%>

<!–文件上传主函数–>

<%sub builduploadrequest(requestbin)

dim fileext,filesize,filestart

posbeg=1

posend=instrb(posbeg,requestbin,getbytestring(chr(13)))

boundary=midb(requestbin,posbeg,posend-posbeg)

boundarypos=instrb(1,requestbin,boundary)

boundarypos=clng(boundarypos)"取上传数据分隔符和起始点

do until (boundarypos=instrb(requestbin,boundary&getbytestring("–")))

dim uploadcontrol

set uploadcontrol=createobject("scripting.dictionary") "建立数据字典

pos=instrb(boundarypos,requestbin,getbytestring("content-disposition"))

pos=instrb(pos,requestbin,getbytestring("name="))

posbeg=pos+6

posend=instrb(posbeg,requestbin,getbytestring(chr(34)))

myname=getstring(midb(requestbin,posbeg,posend-posbeg))"取得上传表单的名字

posfile=instrb(boundarypos,requestbin,getbytestring("filename="))

posbound=instrb(posend,requestbin,boundary)

if posfile<>0 and (posbound>posfile) then"如果是文件

posbeg=posfile+10

posend=instrb(posbeg,requestbin,getbytestring(chr(34)))

filename=getstring(midb(requestbin,posbeg,posend-posbeg))取得文件名

fileext=right(trim(filename),4) "文件扩展名

uploadcontrol.add "fileext",fileext "文件名写入数据字典

if fileext<>".jpg" and fileext<>".gif" and fileext<>".gif" and fileext<>".jpg" then "这里是限制 上传文件类型,这里把文件类型限制为gif、jpg

errno=1

end if

uploadcontrol.add "filename",filename

pos=instrb(posend,requestbin,getbytestring("content-type:"))

posbeg=pos+14

posend=instrb(posbeg,requestbin,getbytestring(chr(13)))

ctype=getstring(midb(requestbin,posbeg,posend-posbeg))

uploadcontrol.add "ctype",ctype

posbeg=posend+4

posend=instrb(posbeg,requestbin,boundary)-2

myvalue=""

filestart=posbeg

filesize=posend-posbeg "得到文件数据在readfile里的起始点和大小

if filesize<=0 then "判断文件大小是否为0,既没有文件上传。

errno=2

end if

if filesize>cint(200)*1024 then "限制文件大小,我这里限制为200k。

errno=3

end if

uploadcontrol.add "filestart",filestart

uploadcontrol.add "filesize",filesize "文件数据的大小和起始点分别写入uploadcontrol数据字典的filestart、filesize项

else 如果是文本框文字

pos=instrb(pos,requestbin,getbytestring(chr(13)))

posbeg=pos+4

posend=instrb(posbeg,requestbin,boundary)-2

if posbeg<posend then

myvalue=getstring(midb(requestbin,posbeg,posend-posbeg))

else

myvalue=""

end if

end if

uploadcontrol.add "myvalue",myvalue "把文本存入uploadcontrol的"myvalue"项

ulrequest.add myname,uploadcontrol "把uploadcontrol再存入ulrequest的myname项,myname是上传表单的某一张表单的名字。

boundarypos=instrb(boundarypos+lenb(boundary),requestbin,boundary)

loop

end sub

取字节函数

function getbytestring(stringstr)

for i=1 to len(stringstr)

char=mid(stringstr,i,1)

getbytestring=getbytestring&chrb(ascb(char))

next

end function

取字符函数

function getstring(stringbin)

for i=1 to lenb(stringbin)

bin=midb(stringbin,i,1)

if ascb(bin)>127 then处理汉字

getstring=getstring&chr(ascw(midb(stringbin,i+1,1)&bin))

i=i+1

else

getstring=getstring&chr(ascb(bin))

end if

next

end function

%>

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 多文件多文本框上传程序-ASP教程,ASP应用
分享到: 更多 (0)

相关推荐

  • 暂无文章