文件上传之asp代码,工作原理,以及post包数据格式

来源:互联网 发布:js 右键事件 编辑:程序博客网 时间:2024/06/05 09:10

    1. asp上传文件代码

          代码 来源于网络,我根据自己的需要进行了一定的修改. 

     1.1 asp代码调试

      asp就是在调试上有点麻烦,关键是要将iis7中ASP模块里面的“将错误发送到浏览器”改成True.

      这样可以再出错时,通过抓包看到出错信息是什么,从而修改asp代码.

      另外,在asp里添加response.write("no hello")
      response.write request.TotalBytes
      response.write("\r\n")

      response.end的效果等同于c里的printf.

     1.2 asp上传文件代码

      以下是代码,大家可以拿去用:

<html>

<head>

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

<title>无标题文档</title>

</head>

<body>

<%

'On Error Resume Next

Response.Expires=0

if request.TotalBytes = 0 then

response.write("TotalBytes = 0")

response.end 

end if

response.write("no hello")

response.write request.TotalBytes

response.write("\r\n")

if Request.TotalBytes > 0 then 

set a=createobject("adodb.stream")  

a.Type=1  

a.Open  

a.write Request.BinaryRead(Request.TotalBytes)  

a.Position=0  

b=a.Read  

c=chrB(13)&chrB(10)  

d=clng(instrb(b,c)) 

'response.write d

'response.end

e=instrb(d+1,b,c)  

set f=createobject("adodb.stream")   

f.type=1   

f.open   

a.Position=d+1 

response.write e

response.write("\r\n")

response.write d

response.write("\r\n")

'a.copyto f,e-d-3   

a.copyto f,-1

f.Position=0   

f.type=2   

f.CharSet="GB2312"   

g=f.readtext   

f.Close   

h=mid(g,instrRev(g,"\")+1,e)   

i=instrb(b,c&c)+4   

j=instrb(i+1,b,leftB(b,d-1))-i-2   

if j <1 then    

set f =nothing    

set a =nothing    

response.write "未选择要上传的文件<a href='?'>重新上传</a>"    

response.end   

end if   

f.Type=1   

f.Open 

a.Position=i-1   

a.CopyTo f,j   

h = Mid(h, InStrRev(h, "filename=""") + 10, 5) '文件名固定为1.txt,因为这里我把长度固定了.

f.SaveToFile server.mappath(h),2

f.Close  

set f=Nothing 

a.Close 

set a=Nothing 

end if

If Err.number<> 0 Then

  response.Write err.number

  response.Write err.Description

  Response.End

  End If

%>

      1.3 工作原理

     利用Stream的核心代码(各种名目的无组件代码都基本上是这样用的)接受二进制文件流filedata=Request.BinaryRead(filesize)
     创建一个将数据写入 Stream 对象,dr.Write(FormData)stream
     用SaveToFile保存为磁盘文件,dr.SaveToFile 文件名
     很多写成了类的无组件上传,和这个略有所不同,但基本工作原理都一样.

             1.4  <<ASP中实现文件上传方法的研究>>是一篇更详细的原理说明.

          http://www.knowsky.com/4272.html
    2. post数据格式说明
    
0 1
原创粉丝点击