一个无组件上传的ASP代码
发布时间:2007-10-09 19:49:00
标签:上传,无组件
上传问题可以说是网络编程中经常遇到的,也是一个很重要的问题,我们不仅要实现上传文件,图片等基本功能,还有考虑到上传程序的安全性,本文介绍了一个无组件上传的ASP源代码:
<!--#include file="../lib/filelib.asp"-->
<%
Response.write "<title>上传文件至当前文件夹</title>"
Response.Write "<body bgcolor=""#D6D3CE"" leftmargin=""0"" topmargin=""0"" title = "" 请您遵守国家相关法律法规上传文件。上传前请杀毒,否则系统将会自动删除此文件!"">"
'**Start Encode**
Action=Request("A")
If Action="UL" Then
DoUpload Request.Cookies("DAZHOU.NET")("nowpath") & "\"
'CheckDiskSpace
' Response.redirect "fileman.asp"
Else
ShowUploadForm
End If
Set fso=Nothing
'========================
SUB ShowUploadForm
'========================
Response.write "<Dir><form enctype=multipart/form-data name=fmupload method=Post action=Upload.asp?A=UL><br>"
If Request("n")<>"" And IsNumeric(Request("n")) Then Session("NumUploadFields")=CInt(Request("n"))
For i=1 to 5
Response.Write "<INPUT type=file name=file"& i & " size=35><br>"
Next
Response.Write "<br><center><INPUT type=submit value=""开始上传""> <INPUT type='button' value= '取消上传' onclick='window.close()'> "
Response.Write "</form>"
End SUB
'========================
SUB DoUpload(Dir)
'========================
'If NOT Application("Debugging") Then On Error resume next
StartTime=Now
RequestBin=Request.BinaryRead(Request.TotalBytes)
Set UploadRequest=CreateObject("Scripting.Dictionary")
BuildUploadRequest RequestBin, UploadRequest
keys=UploadRequest.Keys
For i=0 to UploadRequest.Count - 1
curKey=keys(i)
fName=UploadRequest.Item(curKey).Item("FileName")
If fso.FileExists(Dir & fName) Then fso.deletefile Dir & fName
If fName<>"" And NOT fso.FileExists(Dir & fName) Then
value=UploadRequest.Item(curKey).Item("Value")
valueBeg=UploadRequest.Item(curKey).Item("ValueBeg")
valueLen=UploadRequest.Item(curKey).Item("ValueLen")
TotalULSize=TotalULSize + valueLen
Set strm1=Server.CreateObject("ADODB.Stream")
Set strm2=Server.CreateObject("ADODB.Stream")
strm1.Open
strm1.Type=1 'Binary
strm2.Open
strm2.Type=1 'Binary
strm1.Write RequestBin
strm1.Position=ValueBeg
strm1.CopyTo strm2,ValueLen
strm2.SaveToFile Dir & fName,2
Set strm1=Nothing
Set strm2=Nothing
End If
Next
If Now>StartTime Then Response.Write("<br><br><br><br><center>上传成功!<br>速度: " & Round(TotalULSize/1024/DateDiff("s",StartTime,Now)) &" 千字节/秒" )
Set UploadRequest=Nothing
End SUB
'========================
Sub BuildUploadRequest(RequestBin, UploadRequest)
'========================
'Get the boundary
PosBeg=1
PosEnd=InstrB(PosBeg,RequestBin,getByteString(chr(13)))
boundary=MidB(RequestBin,PosBeg,PosEnd-PosBeg)
boundaryPos=InstrB(1,RequestBin,boundary)
'Get all data inside the boundaries
Do until (boundaryPos=InstrB(RequestBin,boundary & getByteString("--")))
'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,getByteString("Content-Disposition"))
Pos=InstrB(Pos,RequestBin,getByteString("name="))
PosBeg=Pos+6
PosEnd=InstrB(PosBeg,RequestBin,getByteString(chr(34)))
Name=getString(MidB(RequestBin,PosBeg,PosEnd-PosBeg))
PosFile=InstrB(BoundaryPos,RequestBin,getByteString("filename="))
PosBound=InstrB(PosEnd,RequestBin,boundary)
'Test if object is of file type
If PosFile<>0 And (PosFile<PosBound) Then
'Get Filename, content-type and content of file
PosBeg=PosFile + 10
PosEnd=InstrB(PosBeg,RequestBin,getByteString(chr(34)))
FileName=getString(MidB(RequestBin,PosBeg,PosEnd-PosBeg))
FileName=Mid(FileName,InStrRev(FileName,"\")+1)
'Add filename to dictionary object
UploadControl.Add "FileName", FileName
Pos=InstrB(PosEnd,RequestBin,getByteString("Content-Type:"))
PosBeg=Pos+14
PosEnd=InstrB(PosBeg,RequestBin,getByteString(chr(13)))
'Add content-type to dictionary object
ContentType=getString(MidB(RequestBin,PosBeg,PosEnd-PosBeg))
UploadControl.Add "ContentType",ContentType
'Get content of object
PosBeg=PosEnd+4
PosEnd=InstrB(PosBeg,RequestBin,boundary)-2
Value=FileName
ValueBeg=PosBeg-1
ValueLen=PosEnd-Posbeg
Else
'Get content of object
Pos=InstrB(Pos,RequestBin,getByteString(chr(13)))
PosBeg=Pos+4
PosEnd=InstrB(PosBeg,RequestBin,boundary)-2
Value=getString(MidB(RequestBin,PosBeg,PosEnd-PosBeg))
ValueBeg=0
ValueEnd=0
End If
UploadControl.Add "Value" , Value
UploadControl.Add "ValueBeg" , ValueBeg
UploadControl.Add "ValueLen" , ValueLen
UploadRequest.Add name, UploadControl
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)
'====================================
getString =""
For intCount=1 to LenB(StringBin)
getString=getString & chr(AscB(MidB(StringBin,intCount,1)))
Next
End Function
%>
相关阅读:
0
投稿
猜你喜欢
- 如果我们的web应用有大量的异步请求,而这些异步请求是在web服务器认证的情况下,那当我们请求发生在服务器认证失效下,服务器自动302到登录
- 在制作网页以及编程的时候,适当的进行注释,不仅使自己的思路清晰,极大地减轻了维护的难度,而且方便项目组其他人了解你的代码,方便对代码的理解以
- isnull在数据库查询中的应用,特别是再语句连接的时候需要用到 比如连接时候,某个字段没有值但是又要左连接到其他表上 就会显示空, isn
- 分页浏览功能是常见的Web应用功能,对于MySQL数据库来说可以很轻松的使用limit语句实现分页,而对于SQL Server数据库来说,常
- [概 要] 这篇文章讨论常用的"sql注入"技术的细节,应用于流行的Ms IIS/ASP/SQL-Server平台。这里
- jQuery之所以如此流行并被从大公司到个人博客的几乎每个人都广泛使用,是因为它上手和使用相当简单,而且为我们提供了一些人都不知道的相当棒的
- 前言:话说,我一直没能理解美工究竟是什么(这是一篇投稿)。因为要求确实很多。目前,我只能理解成,是前端开发+页面设计+用户体验设计的全能手。
- 这个目前还是有个别无法显示,翻了下msdn貌似没看到更好的解决方案,暂时放弃继续研究,有晓得完全解决的朋友不妨回复说一声。 先附bat创建畸
- [摘要]了解如何充分利用SQL Server 2000的全文搜索功能。本文包含有关实现最大吞吐量和最佳性能的几点提示和技
- YUI Compressor 压缩 JavaScript 的内容包括:移除注释移除额外的空格细微优化标识符替换(Identifier Rep
- 我们知道,关系型数据一般以规范化的形式保存,也就是说你应该尽可能少地重复数据;在正常情况下,表与表之间仅通过各种键值实现关联。进一步地讲,规
- 最近在研究雨哲软件采集程序的时候,需要获取真实软件地址时遇到了需要读取跳转页面跳转后的真实地址的问题。在网上找了很多方法,使用WinHttp
- 我已安装了WINDOWS NT OPTON PACK 中的SMTP Service,请问如何使用它的发送邮件功能?在安装了SMTP Serv
- 网络开发的在分页上要是遇到数(几十)万以上的数据还是用ADO那样的分页会速度很慢的。有了存储过程速度就快多了。下面是本人用50万的数据进行的
- MySQL 5.0.16的乱码问题可以用下面的方法解决:1.设置phpMyAdminLanguage:Chinese simplified
- 集群是一种实现高可用性的有效解决方案,有时它会适得其反。而且,它还非常昂贵。因此,数据库管理员可使用日志转移代替集群来提供较高的可用性。日志
- 表单的验证一直是网页设计者头痛的问题,表单验证类 Validator就是为解决这个问题而写的,旨在使设计者从纷繁复杂的表单验证中解放出来,把
- 一般事件事件浏览器支持描述onClickIE3|N2|O3鼠标点击事件,多用在某个对象控制的范围内的鼠标点击onDblClickIE4|N4
- HTTP头中一般断点下载时才用到Range和Content-Range实体头,Range用户请求头中,指定第一个字节的位置和最后一个字节的位
- rss的优点 1.您可以有选择地浏览您感兴趣的以及与您的工作相关的新闻。 2.您可以把需要的信息从不需要的信息(兜售信息,垃圾邮件等)中分离