请教这段文件上传代码的含义,回帖有分,给予详细解释的加赠100分

alphacsdn 2003-08-08 09:13:51
小弟这里有一段文件上传代码,很好用,就是有些地方不大明白,特来求教,感激涕零!
=====================================
<%
dim contentlen
contentlen=request.totalbytes

if contentlen>102400 then
response.write "文件太大,超过100k,不允许上传。请返回"
else

dim content
content=request.binaryread(request.totalbytes)

'二进制相互转换
Function getByteString(StringStr)
getByteString=""
For i=1 to Len(StringStr)
char=Mid(StringStr,i,1)
getByteString=getByteString&chrB(AscB(char))
Next
End Function

Function getString(StringBin)
getString =""
For intCount = 1 to LenB(StringBin)
getString = getString & chr(AscB(MidB(StringBin,intCount,1)))
Next
End Function

dim upbeg,upend,lineone,linetwo,linethree,line1,line2,line3
upbeg=1
upend=instrb(upbeg,content,getbytestring(chr(10)))
lineone=midb(content,upbeg,upend-upbeg)
upbeg=upend+1
line1=lenb(lineone)
upend=instrb(upbeg,content,getbytestring(chr(10)))
linetwo=midb(content,upbeg,upend-upbeg)
upbeg=upend+1
line2=lenb(linetwo)
upend=instrb(upbeg,content,getbytestring(chr(13)))
linethree=midb(content,upbeg,upend-upbeg)
line3=lenb(linethree)

'获得文件名
dim pp,checknametemp,checklen,checkname,filename
pp=instrb(1,linetwo,getbytestring(chr(46)))
checknametemp=rightb(linetwo,line2-pp+1)
checklen=instrb(1,checknametemp,getbytestring(chr(34)))
checkname=getstring(leftb(checknametemp,checklen-1))
filename=year(now)&month(now)&day(now)&hour(now)&minute(now)&second(now)&checkname

'上传文件
dim alllen,upstream,upstreamend,file
alllen=line1+line2+line3+6
set upstream=server.createobject("adodb.stream")
set upstreamend=server.createobject("adodb.stream")
upstream.type=1
upstreamend.type=1
upstream.open
upstreamend.open
upstream.write content
upstream.position=alllen
file=upstream.read(clng(contentlen-alllen-line1-5))
upstreamend.write file
upstreamend.savetofile(server.mappath("/pub/" & filename))
upstream.close
upstreamend.close
set upstream=nothing
set upstreamend=nothing

response.write("已上传")
end if
%>
=====================================
我的问题是:
·1·content=request.binaryread(request.totalbytes)这句话读到content里的内容是什么样的格式?
·2·为什么要设置lineone,linetwo,linethree,line1,line2,line3 这几个变量,它们的变量值是什么?
·3·两个stream对象的相关语句都是什么意思?
·4·为什么要用二进制格式
=====================================
拜托各位!!:)
...全文
32 22 打赏 收藏 转发到动态 举报
写回复
用AI写文章
22 条回复
切换为时间正序
请发表友善的回复…
发表回复
alphacsdn 2003-08-08
  • 打赏
  • 举报
回复
减alllen时候已经减去一次line1了,而且把含文件名的linetwo也减了,怎么还要减一次line1?难道在alllen个字符之后还有和lineone一样的内容?
zorou_fatal 2003-08-08
  • 打赏
  • 举报
回复
这些和二进制流的数据分布方式有关系。
upstream.position=alllen
file=upstream.read(clng(contentlen-alllen-line1-5))
首先定位到alllen处
然后开始读取数据,
clng(contentlen-alllen-line1-5)是需要读取的长度
contenlen是总的二进制流的长度,而alllen是前面三段信息的长度,而后面还要再
减去一个line1和5,就是在读取文件的时候,把它和文件本身内容无关的,比如文件名以及分界符等去掉。
LonelyStark 2003-08-08
  • 打赏
  • 举报
回复
换行回车也算进长度里去了
所以加6
alphacsdn 2003-08-08
  • 打赏
  • 举报
回复
zorou_fatal(Red Star Over China)

alllen=line1+line2+line3+6, 为什么要加6呢?

upstream.position=alllen
file=upstream.read(clng(contentlen-alllen-line1-5))
这两句话我不能明白,给upstream定位之后,是不是read的时候就是从这个定位开始读?那么contentlen-alllen-line1-5 这个数值是什么的长度?为什么还要减line1?
laker_tmj 2003-08-08
  • 打赏
  • 举报
回复
up learn
zorou_fatal 2003-08-08
  • 打赏
  • 举报
回复
有可能
alphacsdn 2003-08-08
  • 打赏
  • 举报
回复
我上传了一个文本文件,但是在后面加了这么一些代码,是不是这个上传程序的写文件部分字符串长度计算的不对呀?

==附加的代码========================
-----------------------------7d33161a40388
Content-Disposition: form-data; name="submit1"

上传
===============================
alphacsdn 2003-08-08
  • 打赏
  • 举报
回复
第二行里放的有文件名,代码
dim pp,checknametemp,checklen,checkname,filename
pp=instrb(1,linetwo,getbytestring(chr(46)))
checknametemp=rightb(linetwo,line2-pp+1)
checklen=instrb(1,checknametemp,getbytestring(chr(34)))
checkname=getstring(leftb(checknametemp,checklen-1))
filename=year(now)&month(now)&day(now)&hour(now)&minute(now)&second(now)&checkname
的意思是取得文件的后缀名,我说的对不对??
zorou_fatal 2003-08-08
  • 打赏
  • 举报
回复
·1·content=request.binaryread(request.totalbytes)这句话读到content里的内容是什么样的格式?
·2·为什么要设置lineone,linetwo,linethree,line1,line2,line3 这几个变量,它们的变量值是什么?
·3·两个stream对象的相关语句都是什么意思?
·4·为什么要用二进制格式
1.curren读取的是以二进制方式传递过来的表单的全部内容
2.lineone=midb(content,upbeg,upend-upbeg)
upbeg=upend+1
line1=lenb(lineone)
注意看这里的代码lineone 获得的是在content这段二进制数据中,从upbeg这个位置开始长度为upend-upbeg的数据而
upbeg=1
upend=instrb(upbeg,content,getbytestring(chr(10)))
upbeg开始是从二进制数据流的第一位开始的,upend是到第一个chr(10)结束。
那么第一次读取的就是从头到第一个chr(10)的部分间的内容,而line1则保存的lineone的长度。后面的linetwo,line2,linethree,line3也是同样的。
那么他们的作用是什么呢?
注意看下面的代码:
dim pp,checknametemp,checklen,checkname,filename
pp=instrb(1,linetwo,getbytestring(chr(46)))
checknametemp=rightb(linetwo,line2-pp+1)
checklen=instrb(1,checknametemp,getbytestring(chr(34)))
checkname=getstring(leftb(checknametemp,checklen-1))
filename=year(now)&month(now)&day(now)&hour(now)&minute(now)&second(now)&checkname
这里,首先从linetwo当中读出一个"."符号,这个符号和后面的"是问文件的name字段内容的标志,然后找到这两个标志位以后,就可以读出文件名了。
3.dim alllen,upstream,upstreamend,file
alllen=line1+line2+line3+6
set upstream=server.createobject("adodb.stream")
set upstreamend=server.createobject("adodb.stream")
upstream.type=1 '方式为读取
upstreamend.type=1 '方式为读取
upstream.open '打开对象
upstreamend.open '打开对象
upstream.write content 写入内容
upstream.position=alllen '定位
file=upstream.read(clng(contentlen-alllen-line1-5))'传递给一个file对象
upstreamend.write file '给file写入内容
upstreamend.savetofile(server.mappath("/pub/" & filename)) '存成文件
upstream.close '关闭
upstreamend.close '关闭
set upstream=nothing
set upstreamend=nothing
·4·为什么要用二进制格式
request.form这样的方式是无法读取文件的内容的
alphacsdn 2003-08-08
  • 打赏
  • 举报
回复
bubuy(澎湃 NoMoneyToBuy),content值是字符串吧,要不怎么能分成3行字符串呢?那3行字符串里有分别是什么内容呢,比如第二行里有文件的名字,那么第一行和第三行是什么呢?
bubuy 2003-08-08
  • 打赏
  • 举报
回复
content变量值是字节数组
alphacsdn 2003-08-08
  • 打赏
  • 举报
回复
“·4·为什么要用二进制格式”解决了!
bubuy 2003-08-08
  • 打赏
  • 举报
回复
stream:
可以读取流。(读取是从流到数据结构(如字节数组)的数据传输。)
可以写入流。
可以支持查找。
LonelyStark 2003-08-08
  • 打赏
  • 举报
回复
至于为什么要用二进制
上传文件如果不用上传组件的话
当然是用二进制了
alphacsdn 2003-08-08
  • 打赏
  • 举报
回复
先谢bubuy(澎湃 NoMoneyToBuy)!不过我想知道content变量值的格式,我想格式知道了,下面的问题就迎刃而解了。
bubuy 2003-08-08
  • 打赏
  • 举报
回复
4.如果不用二进制形式的话,怎么上传图片及其它文件啊?(如.exe)
LonelyStark 2003-08-08
  • 打赏
  • 举报
回复
upbeg=1 设置开始标志
upend=instrb(upbeg,content,getbytestring(chr(10))) 获得换行符号的位置
lineone=midb(content,upbeg,upend-upbeg) 取得第一行的数据
upbeg=upend+1 开始位置加一
line1=lenb(lineone) 获得第一行的长度
upend=instrb(upbeg,content,getbytestring(chr(10))) 获得第二行换行符的位置
linetwo=midb(content,upbeg,upend-upbeg) 读取第二行
upbeg=upend+1 这几行跟上面一样
line2=lenb(linetwo)
upend=instrb(upbeg,content,getbytestring(chr(13)))
linethree=midb(content,upbeg,upend-upbeg)
line3=lenb(linethree)
bubuy 2003-08-08
  • 打赏
  • 举报
回复
1.答:读出的是content的所有字节。request.totalbytes是获的所有字节数量。
LonelyStark 2003-08-08
  • 打赏
  • 举报
回复
没仔细看
首个问题是读取二进制数据
alphacsdn 2003-08-08
  • 打赏
  • 举报
回复
zorou_fatal(Red Star Over China) 呵呵,是呀,我也看了,但是一些控制字符又没法显示,怎么知道哪里是回车,空格什么的呢!!
=======================================
看来就这样了,下班结贴,谁有补充的赶快补充。
加载更多回复(2)

28,390

社区成员

发帖
与我相关
我的任务
社区描述
ASP即Active Server Pages,是Microsoft公司开发的服务器端脚本环境。
社区管理员
  • ASP
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧