myUpload version 1.0 build 2
今天继续写了个DEMO,是AJAX上传文件显进度的ASP版,现在发下代码,太晚了不写解释了.
注释里面都有了.
const filePath = "" ' 上传最基本的目录,例如:upload\
const dbg = false ' 是否进入调试模式
const subSize = 1024 ' 每次1KB传输
const exists = true ' 如果文件存在是否删除文件再上传
dim maxSize : maxSize = (500 * 1024 * 1024) ' 限制文件大小,MB
dim enable : enable = true ' 是否上传
dim errmsg : errmsg = 0 ' 错误提示
dim dataSize : dataSize = Request.TotalBytes ' 数据总大小
dim length : length = 0 ' 现在的上传进度 bytes
dim percent : percent = 0 ' 百分比
'初始化
Session("error") = ""
'上传测试
dim result : result = upload("xxx.txt")
if result <> 0 then
'系统开启调试功能
if dbg then
Session("error") = check(result)
else
Session("error") = "上传失败!详情请开启debug模式,错误代码:" & result
end if
end if
function check(byval e)
select case e
case 1 check = "文件大小为空或超过上传限制,请重试!"
case 2 check = "您的系统不支持adodb.stream,上传则无法运行!"
case 3 check = "您的系统不支持Scripting.FileSystemObject,上传则无法运行!"
case 8 check = "上传文件已存在,您并没有设置删除文件,请检查系统设置!"
case 10 check = "用户取消上传,上传终止!"
case 9 check = "系统未开启上传功能!"
case else
check = "上传失败,原因不明"
end select
end function
function upload(byval fileName)
'如果未开启上传,则直接退出不上传
if enable = false then
upload = 9
exit function
end if
if (dataSize <= 0) or (dataSize => maxSize) then
upload = dataSize
exit function
else
dim objstm,objstm2,objfso
' 无组建上传需要2个adodb.stream对象
set objstm = Server.CreateObject("adodb.stream")
set objstm2 = Server.CreateObject("adodb.stream")
if isnull(objstm) then
upload = 2
exit function
end if
set objfso = Server.CreateObject("Scripting.FileSystemObject")
if isnull(objstm) then
upload = 3
exit function
end if
'判断预上传文件是否存在,存在则按照用户旨意
if objfso.FileExists(Server.MapPath(filePath & fileName)) then
' 用户设置是否可以删除
if exists then
objfso.DeleteFile(Server.MapPath( filePath & fileName))
else
upload = 8
exit function
end if
end if
'上传开始,预设一些属性
objstm.type = 1
objstm.mode = 3
objstm.open
objstm2.type = 1
objstm2.mode = 3
objstm2.open
'准备上传,设置是否在上传
Session("upload") = false
'如果数据小于1KB则直接上传
if dataSize < 1024 then
Session("upload") = true
objstm.write Request.BinaryRead(subSize)
Session("percent") = 100
Session("length") = dataSize
Session("size") = dataSize
Session("upload") = false
'否则分段然后按照每次1KB上传
else
Session("upload") = true
while length < dataSize
'用户取消
if Session("cancel") = true then
Session("upload") = false
upload = 10
'销毁对象
objstm.close
objstm2.close
set objstm = nothing
set objstm2 = nothing
set objfso = nothing
exit function
end if
'开始上传
Session("upload") = true
objstm.write Request.BinaryRead(subSize)
'更新已上传长度
length = length + subSize
'更新百分比
percent = percent + subSize / dataSize * 100
'精确到小数点后2位
Session("percent") = round(percent,0)
Session("length") = length
Session("size") = dataSize
'objstm.flush
wend
'上传完毕
Session("upload") = false
objstm.position = 0
'修正文件,读取缓冲区到data
dim data : data = objstm.read(dataSize)
'查找位置,特征码
dim break : break = chrb(13) & chrb(10) & chrb(13) & chrb(10)
dim ereak : ereak = chrb(13) & chrb(10) & chrb(45) & chrb(45) & chrb(45) & chrb(45) & chrb(45) & chrb(45) & chrb(87)
dim bpos : bpos = instrb(data,break)
dim epos : epos = instrb(data,ereak)
'修正
objstm.position = bpos+3
objstm.copyto objstm2,epos-bpos-4
objstm.close
set objstm = nothing
'开始保存文件
objstm2.savetofile Server.MapPath(filePath & fileName)
'干掉对象内存
objstm2.close
set objstm2 = nothing
set objfso = nothing
'至此,上传成功
upload = 0
end if
end if
end function
HTML:
注意那个iframe
<script src="jquery-1.4.1.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#upload").click(function(){
//this.form.action = "upload3.asp"
this.form.submit();
setInterval(get,1000);
});
$("#cancel").click(function(){
$("#uploader").attr("str","about:blank");
});
});
function get(){
$.get("state.asp",function(data){
//alert(data);
$("#state").html(data);
});
}
</script>
</head>
<body>
<iframe style="display:none;" name="uploader" src="about:blank" frameborder="0" marginheight="0" marginwidth="0" id="uploader"></iframe>
<form action="upload3.asp" method="post" enctype="multipart/form-data" target="uploader">
<input name="file1" type="file" size="40"/>
<input id="upload" name="" type="button" value="upload" />
<input id="cancel" value="cancel" type="button" />
</form>
<div id="state"></div>
</body>
浙公网安备 33010602011771号