#####################################################################
备注:我没有验证,提供给大家一种参考!请大家自行验证
欢迎大家发表你认为好的精彩程序代码
大风(xuankong) 2000。12。8
#####################################################################
标题:还是用我的无组件文件上载吧,与aspcn组件有异曲同工之妙,下面是例程:
作者:dudu(dusj)
时间:2000-12-7 上午 09:48:00
阅读次数:8
详细信息:
—————————–
表单填写页的内容:
—————————–
<html>
<head>
<meta name="author" content="sander duivestein">
<meta http-equiv="content-type" content="text/html; charset=gb2312">
<meta name="generator" content="microsoft frontpage 4.0">
<meta name="progid" content="frontpage.editor.document">
<title>将本地的文件上载到数据库</title>
</head>
<body>
<font size="2" size="2"><br>
</font>
<form name="frmupload" method="post" enctype="multipart/form-data" action="upload.asp">
<table cellspacing="0" cellpadding="0" width="490">
<tr>
<td width="167">
<p align="right"><font size="2" size="2">请选择文件:</font></p>
</td>
<td width="319"><font size="2" size="2"><input type="file" name="vfilename"></font></td>
</tr>
<tr>
<td align="right" width="167"><font size="2" size="2">文件说明1:</font></td>
<td width="319"><input name="shuoming" size="32" ></font></td>
</tr>
<tr>
<td align="right" width="167"><font size="2" size="2">文件说明2:</font></td>
<td width="319"><input name="shuoming_2" size="32" onchange="isdate1(document.frmupload.dengji)" </td>
</tr>
<td width="167"></td>
<td width="319">
<p align="left"><font size="2" size="1"><input type="submit" style="background-image: url(images/back.jpg); cursor: hand; font-sizd: smaller; left: 0px; width: 94; top: 5px; height: 25" value=" 上 载 " style="background-image: url(images/back.jpg); cursor: hand; font-sizd: smaller; left: 0px; width: 94; top: 5px; height: 25" size="23" ></font></p>
</td>
<tr>
<td colspan="2" align="right" width="487"></td>
</tr>
</table>
</form>
</body>
</html>
———————————-
upload.asp的内容:
———————————-
<%
response.buffer = true
response.clear
bytecount = request.totalbytes
requestbin = request.binaryread(bytecount)
response.binarywrite requestbin
取得表单的全部内容
dim uploadrequest
set uploadrequest = createobject("scripting.dictionary")
uploadrequest结构将用来存放表单
posbeg = 1
posend = instrb(posbeg,requestbin,stob(chr(13)))
boundary = midb(requestbin,posbeg,posend-posbeg)
求字段间的分隔串(即:—————————–7d029e347d8 )
boundarypos = instrb(1,requestbin,boundary)
do until (boundarypos=instrb(requestbin,boundary & stob("–")))
members variable of objects are put in a dictionary object
dim uploadcontrol
set uploadcontrol = createobject("scripting.dictionary")
get an object name
pos = instrb(boundarypos,requestbin,stob("content-disposition"))
pos = instrb(pos,requestbin,stob("name="))
posbeg = pos+6
posend = instrb(posbeg,requestbin,stob(chr(34)))
name = btos(midb(requestbin,posbeg,posend-posbeg))
posfile = instrb(boundarypos,requestbin,stob("filename="))
posbound = instrb(posend,requestbin,boundary)
response.write "<hr>"
response.write "name="&name&"<br>"
name为表单项的名字
test if object is of file type
if posfile<>0 and (posfile<posbound) then
posbeg = posfile + 10
posend = instrb(posbeg,requestbin,stob(chr(34)))
filename = btos(midb(requestbin,posbeg,posend-posbeg))
response.write "<br>"
response.write "filename="&filename&"<br>"
filename为全路径文件名(如果是文件类型话)
uploadcontrol.add "filename", filename
pos = instrb(posend,requestbin,stob("content-type:"))
posbeg = pos+14
posend = instrb(posbeg,requestbin,stob(chr(13)))
contenttype = btos(midb(requestbin,posbeg,posend-posbeg))
uploadcontrol.add "contenttype",contenttype
response.write "filetype="&contenttype&"<br>"
response.write "<br>"
posbeg = posend+4
posend = instrb(posbeg,requestbin,boundary)-2
value = midb(requestbin,posbeg,posend-posbeg)
filesize = posend – posbeg
uploadcontrol.add "filesize",filesize
response.write "value="
response.binarywrite value
response.write "<br>"
else
pos = instrb(pos,requestbin,stob(chr(13)))
posbeg = pos+4
posend = instrb(posbeg,requestbin,boundary)-2
value = btos(midb(requestbin,posbeg,posend-posbeg))
response.write "value="&value&"<br>"
value为表单项的值,如果为文件项,则value为一长串二进制码
end if
uploadcontrol.add "value" , value
uploadrequest.add name, uploadcontrol
uploadrequest增加一个key为name的项,此项的value仍是一个dixtionary对象
boundarypos=instrb(boundarypos+lenb(boundary),requestbin,boundary)
loop
contenttype = uploadrequest.item("vfilename").item("contenttype")
filesize = uploadrequest.item("vfilename").item("filesize")
filepathname = uploadrequest.item("vfilename").item("filename")
filename = right(filepathname,len(filepathname)-instrrev(filepathname,"\"))
filevalue = uploadrequest.item("vfilename").item("value")
shuoming = uploadrequest.item("shuoming").item("value")
shuoming_2 = uploadrequest.item("shuoming_2").item("value")
取出嵌套的dictionary对象的值,注意dictionary的key区分大小写!!!
处理数据库
set conngraph = server.createobject("adodb.connection")
conngraph.connectionstring = application("dns")
conngraph.open
set db = server.createobject("adodb.recordset")
db.open "select * from t_recordnote where 1<>1",conngraph,1,3
db.addnew
db("record").appendchunk filevalue
db("shuoming") = shuoming
db("shuoming_2") = shuoming_2
db("username") = session("username")
db("filetype") = contenttype
db("time") = now()
db("filename") = filename
db("filesize") = filesize
db.update
db.close
set db = nothing
response.write "文件"&filename&"上载成功"
function stob(varstr)
str2bin = ""
for i = 1 to len(varstr)
varchar = mid(varstr, i, 1)
varasc = asc(varchar)
asc对中文字符求出来的值可能为负数,
加上65536就可求出它的无符号数值
-1在机器内是用补码表示的0xffff,
其无符号值为65535,65535=-1+65536
其他负数依次类推。
if varasc < 0 then
varasc = varasc + 65535
end if
对中文的处理:把双字节低位和高位分开
if varasc > 255 then
varlow = left(hex(asc(varchar)), 2)
varhigh = right(hex(asc(varchar)), 2)
str2bin = str2bin & chrb("&h" & varlow) & chrb("&h" & varhigh)
else
str2bin = str2bin & chrb(ascb(varchar))
end if
next
stob = str2bin
end function
byte string to string conversion
function btos(binstr)
中文字符skip标志
skipflag = 0
strc = ""
if not isnull(binstr) then
lnglen = lenb(binstr)
for i = 1 to lnglen
if skipflag = 0 then
tmpbin = midb(binstr, i, 1)
判断是否中文的字符
if ascb(tmpbin) > 127 then
ascw会把二进制的中文双字节字符高位和低位反转,所以要先把中文的高低位反转
strc = strc & chr(ascw(midb(binstr, i + 1, 1) & tmpbin))
skipflag = 1
else
strc = strc & chr(ascb(tmpbin))
end if
else
skipflag = 0
end if
next
end if
btos = strc
end function
%>
——————————
表结构:
——————————
drop table [t_recordnote]
create table [t_recordnote] (
[recordid] [int] identity (1, 1) not null ,
[filename] [varchar] (60) null ,
[filetype] [varchar] (40) null ,
[username] [varchar] (30) null ,
[time] [datetime] null ,
[shuoming] [varchar] (200) null ,
[shuoming_2] [varchar] (200) null ,
[filesize] [int] null ,
[record] [image] null ,
primary key clustered
(
[recordid]
) on [primary]
)
go
本文章来自于http://www.aspcn.com asp中华网如要转载请务必注明出处,谢谢
