现在的位置: 网页制作教程网站制作经验 >正文
jquery相关教程

jquery uploadify for asp 批量文件上传程序 整合风声asp无组件上传

发表于2017/5/5 网站制作经验 0条评论 ⁄ 热度 2,285℃

jquery 上传插件 uploadify (官网:http://www.uploadify.com/)

uploadify上传组件

uploadify功能强大,支持多文件、批量上传、状态进度条等,提供前端文件限制相关设置。由于是flash环境全面支持asp、asp.net、php等平台;官方目前只提供php程序接入方式,asp.net程序接入在网络上也有很多,而asp程序的只找到一个老外写的,有很多功能调试起来很不太方便,所以我用风声asp无组件上传程序重新整合起来。

 

主要修改三个文件:

demo.html 该文件用于设置前端相关限制,如文件类型、大小、批量上传等。

upload.asp 该文件主要用于接收demo.html 提交过来的数据、执行上传程序、返回上传结果。

UpLoadClass.asp 该文件为风声ASP无组件上传程序,可在此修改文件上传保存名称、路径、文件类型、大小等。

下面是我模仿QQ中转站的上传界面整合出来的效果图:

上传界面

demo.html代码如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Demo</title>
<link rel="Stylesheet" href="uploadify.css" />
<script src="jquery.min.js" type="text/javascript"></script>
<script type="text/javascript" src="swfobject.js"></script>
<script type="text/javascript" src="jquery.uploadify.js"></script>
<script language="javascript" type="text/javascript">
$(document).ready(function() {
$("#uploadify").uploadify({
'uploader': 'uploadify.swf',//指定上传控件的主体文件,默认'uploader.swf'
'script': 'upload.asp',//指定服务器端上传处理文件
'cancelImg': 'cancel.png',//指定取消上传的图片,默认'cancel.png'
//'buttonImg': 'images/button.jpg',//指定浏览文件按钮的图片
//'fileDesc' : '图片文件',//出现在上传对话框中的文件类型描述
//'fileExt' : '*.jpg;*.jpeg;*.gif;*.bmp;*.png',//控制可上传文件的扩展名,启用本项时需同时声明fileDesc
'sizeLimit': 30000000, //控制上传文件的大小,单位byte服务器默认只支持30MB,修改服务器设置请查看相关资料
//'simUploadLimit' :5,//多文件上传时,同时上传文件数目限制
'buttonText':'choose',//按钮显示文字,不支持中文,要用中文直接用背景图片cancelImg设置
//'folder': 'upload',//要上传到的服务器路径(PS:已在服务端设置)
'queueID': 'fileQueue',//队列
'fileDataName': 'Filedata',//提交文件域名称
'auto': false,//选定文件后是否自动上传,默认false
'multi': true,//是否允许同时上传多文件,默认false
'method':'post',//提交方式 post or get
//'scriptData'  : {'firstName':'Ronnie','age':30},//提交自定义数据
onComplete:function(event,queueID,fileObj,response,data){//上传成功执行
//返回服务端JSON数据,可在服务端修改返回数据类型
var filetext=eval("(" + response + ")")//解析JSON数据
switch   ( filetext.err )   {   
        case0:
$("#RequestText").prepend('保存时间:' + filetext.time + "<br /><br />");
$("#RequestText").prepend('文件类型:' + filetext.ext + "<br />");
$("#RequestText").prepend('文件大小:' + filetext.size + "<br />");
$("#RequestText").prepend('保存路径:' + filetext.path + "<br />");
$("#RequestText").prepend('保存文件名:' + filetext.savename + "<br />");
$("#RequestText").prepend('<br />文件名:' + filetext.name + "<br />");
break ;
        case1:
          $("#RequestText").prepend('<br />文件因过大而未被保存<br />');
break ;
case2:
          $("#RequestText").prepend('<br />文件类型因不匹配而未被保存<br />');
break ;
case3:
          $("#RequestText").prepend('<br />文件因过大并且类型不匹配而未被保存<br />');
break ;
case-1:
          $("#RequestText").prepend('<br />没有文件上传<br />');
break ;
}
//返回uploadify数据
//$("#RequestText").append('文件名:' + fileObj.name + "<br />");
//$("#RequestText").append('文件大小:' + fileObj.size + "<br />");
//$("#RequestText").append('创建时间:' + fileObj.creationDate + "<br />");
//$("#RequestText").append('最后修改时间:' + fileObj.modificationDate + "<br />");
//$("#RequestText").append('文件类型:' + fileObj.type + "<br />");
},
});
});  
</script>
</head>
 
<body>
  <form id="form1" enctype="multipart/form-data" method="post" >
        <input type="file" name="uploadify" id="uploadify" />
        <a href="javascript:$('#uploadify').uploadifyUpload()">上传</a>| <a href="javascript:$('#uploadify').uploadifyClearQueue()"> 取消上传</a>
        <div id="fileQueue"></div>
    </form>
<div id="RequestText"></div>
</body>
</html>

upload.asp代码如下:

<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<%
OPTION EXPLICIT
Server.ScriptTimeOut=5000
%>
<!-- #include file="UpLoadClass.asp" -->
<%
Dim f_Name,f_SaveName,f_Path,f_Size,f_Ext,f_Err,f_Save,f_Time
dim FileUpload , FormName
FormName = "Filedata"'文件域名称
set FileUpload = New UpLoadClass
FileUpload.Charset="UTF-8"
FileUpload.Open() '开始执行上传程序
 
f_Err = FileUpload.Form(FormName & "_Err") '获取上传状态
IF f_Err = 0 Then '上传成功
f_Name = FileUpload.Form(FormName & "_Name")'原文件名
f_SaveName = FileUpload.Form(FormName)'保存文件名
f_Path = FileUpload.SavePath'保存路径
f_Size = FileUpload.Form(FormName & "_Size")'文件大小
f_Ext = FileUpload.Form(FormName & "_Ext")'文件类型
f_Time = Now()'保存时间
Response.Write("{""name"":""" & f_Name & """,""savename"":""" & f_SaveName & """,""path"":""" & f_Path & """,""size"":" & f_Size & ",""ext"":""" & f_Ext & """,""time"":""" & f_Time & """,""err"":" & f_Err & "}") '输出JSON数据
Else 
Response.Write("{""err"":" & f_Err & "}") ‘返回上传失败原因
End IF
set FileUpload = nothing
%> 

UpLoadClass.asp为风声asp无组件上传程序,大伙可在风声官方网站下载:http://www.fonshen.com

代码如下:

<%
'----------------------------------------------------------
'**************  风声 ASP 无组件上传类 V2.11  *************
'作者:风声
'网站:http://www.fonshen.com
'邮件:webmaster@fonshen.com
'版权:版权全体,源代码公开,各种用途均可免费使用
'**********************************************************
'----------------------------------------------------------
Class UpLoadClass


Private m_TotalSize,m_MaxSize,m_FileType,m_SavePath,m_AutoSave,m_Error,m_Charset
Private m_dicForm,m_binForm,m_binItem,m_strDate,m_lngTime
Public FormItem,FileItem


Public Property Get Version
Version="Fonshen ASP UpLoadClass Version 2.11"
End Property


Public Property Get Error
Error=m_Error
End Property


Public Property Get Charset
Charset=m_Charset
End Property
Public Property Let Charset(strCharset)
m_Charset=strCharset
End Property


Public Property Get TotalSize
TotalSize=m_TotalSize
End Property
Public Property Let TotalSize(lngSize)
if isNumeric(lngSize) then m_TotalSize=Clng(lngSize)
End Property


Public Property Get MaxSize
MaxSize=m_MaxSize
End Property
Public Property Let MaxSize(lngSize)
if isNumeric(lngSize) then m_MaxSize=Clng(lngSize)
End Property


Public Property Get FileType
FileType=m_FileType
End Property
Public Property Let FileType(strType)
m_FileType=strType
End Property


Public Property Get SavePath
SavePath=m_SavePath
End Property
Public Property Let SavePath(strPath)
m_SavePath=Replace(strPath,chr(0),"")
End Property


Public Property Get AutoSave
AutoSave=m_AutoSave
End Property
Public Property Let AutoSave(byVal Flag)
select case Flag
case 0,1,2: m_AutoSave=Flag
end select
End Property


Private Sub Class_Initialize
m_Error   = -1
m_Charset  = "gb2312"
m_TotalSize= 0
m_MaxSize  = 0
m_FileType = "jpg/gif/rar/doc"'可设置jpg/gif/rar等类型,空为不限制
m_SavePath = "upload/"'当前目录请设为空
CheckFolder(m_SavePath)'检测文件夹是否存在
m_AutoSave = 0
Dim dtmNow : dtmNow = Date()
m_strDate  = Year(dtmNow)&Right("0"&Month(dtmNow),2)&Right("0"&Day(dtmNow),2)
m_lngTime  = Clng(Timer()*1000)
Set m_binForm = Server.CreateObject("ADODB.Stream")
Set m_binItem = Server.CreateObject("ADODB.Stream")
Set m_dicForm = Server.CreateObject("Scripting.Dictionary")
m_dicForm.CompareMode = 1
End Sub


Private Sub Class_Terminate
m_dicForm.RemoveAll
Set m_dicForm = nothing
Set m_binItem = nothing
m_binForm.Close()
Set m_binForm = nothing
End Sub


Public Function Open()
Open = 0
if m_Error=-1 then
m_Error=0
else
Exit Function
end if
Dim lngRequestSize : lngRequestSize=Request.TotalBytes
if m_TotalSize>0 and lngRequestSize>m_TotalSize then
m_Error=5
Exit Function
elseif lngRequestSize<1 then
m_Error=4
Exit Function
end if


Dim lngChunkByte : lngChunkByte = 102400
Dim lngReadSize : lngReadSize = 0
m_binForm.Type = 1
m_binForm.Open()
do
m_binForm.Write Request.BinaryRead(lngChunkByte)
lngReadSize=lngReadSize+lngChunkByte
if  lngReadSize >= lngRequestSize then exit do
loop 
m_binForm.Position=0
Dim binRequestData : binRequestData=m_binForm.Read()


Dim bCrLf,strSeparator,intSeparator
bCrLf=ChrB(13)&ChrB(10)
intSeparator=InstrB(1,binRequestData,bCrLf)-1
strSeparator=LeftB(binRequestData,intSeparator)


Dim strItem,strInam,strFtyp,strPuri,strFnam,strFext,lngFsiz
Const strSplit="'"">"
Dim strFormItem,strFileItem,intTemp,strTemp
Dim p_start : p_start=intSeparator+2
Dim p_end
Do
p_end = InStrB(p_start,binRequestData,bCrLf&bCrLf)-1
m_binItem.Type=1
m_binItem.Open()
m_binForm.Position=p_start
m_binForm.CopyTo m_binItem,p_end-p_start
m_binItem.Position=0
m_binItem.Type=2
m_binItem.Charset=m_Charset
strItem = m_binItem.ReadText()
m_binItem.Close()
intTemp=Instr(39,strItem,"""")
strInam=Mid(strItem,39,intTemp-39)


p_start = p_end + 4
p_end = InStrB(p_start,binRequestData,strSeparator)-1
m_binItem.Type=1
m_binItem.Open()
m_binForm.Position=p_start
lngFsiz=p_end-p_start-2
m_binForm.CopyTo m_binItem,lngFsiz


if Instr(intTemp,strItem,"filename=""")<>0 then
if not m_dicForm.Exists(strInam&"_From") then
strFileItem=strFileItem&strSplit&strInam
if m_binItem.Size<>0 then
intTemp=intTemp+13
strFtyp=Mid(strItem,Instr(intTemp,strItem,"Content-Type: ")+14)
strPuri=Mid(strItem,intTemp,Instr(intTemp,strItem,"""")-intTemp)
intTemp=InstrRev(strPuri,"\")
strFnam=Mid(strPuri,intTemp+1)
m_dicForm.Add strInam&"_Type",strFtyp
m_dicForm.Add strInam&"_Name",strFnam
m_dicForm.Add strInam&"_Path",Left(strPuri,intTemp)
m_dicForm.Add strInam&"_Size",lngFsiz
if Instr(strFnam,".")<>0 then
strFext=Mid(strFnam,InstrRev(strFnam,".")+1)
else
strFext=""
end if


select case strFtyp
case "image/jpeg","image/pjpeg","image/jpg"
if Lcase(strFext)<>"jpg" then strFext="jpg"
m_binItem.Position=3
do while not m_binItem.EOS
do
intTemp = Ascb(m_binItem.Read(1))
loop while intTemp = 255 and not m_binItem.EOS
if intTemp < 192 or intTemp > 195 then
m_binItem.read(Bin2Val(m_binItem.Read(2))-2)
else
Exit do
end if
do
intTemp = Ascb(m_binItem.Read(1))
loop while intTemp < 255 and not m_binItem.EOS
loop
m_binItem.Read(3)
m_dicForm.Add strInam&"_Height",Bin2Val(m_binItem.Read(2))
m_dicForm.Add strInam&"_Width",Bin2Val(m_binItem.Read(2))
case "image/gif"
if Lcase(strFext)<>"gif" then strFext="gif"
m_binItem.Position=6
m_dicForm.Add strInam&"_Width",BinVal2(m_binItem.Read(2))
m_dicForm.Add strInam&"_Height",BinVal2(m_binItem.Read(2))
case "image/png"
if Lcase(strFext)<>"png" then strFext="png"
m_binItem.Position=18
m_dicForm.Add strInam&"_Width",Bin2Val(m_binItem.Read(2))
m_binItem.Read(2)
m_dicForm.Add strInam&"_Height",Bin2Val(m_binItem.Read(2))
case "image/bmp"
if Lcase(strFext)<>"bmp" then strFext="bmp"
m_binItem.Position=18
m_dicForm.Add strInam&"_Width",BinVal2(m_binItem.Read(4))
m_dicForm.Add strInam&"_Height",BinVal2(m_binItem.Read(4))
case "application/x-shockwave-flash"
if Lcase(strFext)<>"swf" then strFext="swf"
m_binItem.Position=0
if Ascb(m_binItem.Read(1))=70 then
m_binItem.Position=8
strTemp = Num2Str(Ascb(m_binItem.Read(1)), 2 ,8)
intTemp = Str2Num(Left(strTemp, 5), 2)
strTemp = Mid(strTemp, 6)
while (Len(strTemp) < intTemp * 4)
strTemp = strTemp & Num2Str(Ascb(m_binItem.Read(1)), 2 ,8)
wend
m_dicForm.Add strInam&"_Width", Int(Abs(Str2Num(Mid(strTemp, intTemp + 1, intTemp), 2) - Str2Num(Mid(strTemp, 1, intTemp), 2)) / 20)
m_dicForm.Add strInam&"_Height",Int(Abs(Str2Num(Mid(strTemp, 3 * intTemp + 1, intTemp), 2) - Str2Num(Mid(strTemp, 2 * intTemp + 1, intTemp), 2)) / 20)
end if
end select


m_dicForm.Add strInam&"_Ext",strFext
m_dicForm.Add strInam&"_From",p_start
if m_AutoSave<>2 then
intTemp=GetFerr(lngFsiz,strFext)
m_dicForm.Add strInam&"_Err",intTemp
if intTemp=0 then
if m_AutoSave=0 then
strFnam=GetTimeStr()
if strFext<>"" then strFnam=strFnam&"."&strFext
end if
m_binItem.SaveToFile Server.MapPath(m_SavePath&strFnam),2
m_dicForm.Add strInam,strFnam
end if
end if
else
m_dicForm.Add strInam&"_Err",-1
end if
end if
else
m_binItem.Position=0
m_binItem.Type=2
m_binItem.Charset=m_Charset
strTemp=m_binItem.ReadText
if m_dicForm.Exists(strInam) then
m_dicForm(strInam) = m_dicForm(strInam)&","&strTemp
else
strFormItem=strFormItem&strSplit&strInam
m_dicForm.Add strInam,strTemp
end if
end if


m_binItem.Close()
p_start = p_end+intSeparator+2
loop Until p_start+3>lngRequestSize
FormItem=Split(strFormItem,strSplit)
FileItem=Split(strFileItem,strSplit)

Open = lngRequestSize
End Function


Private Function GetTimeStr()
m_lngTime=m_lngTime+1
GetTimeStr=m_strDate&Right("00000000"&m_lngTime,8)
End Function


Private Function GetFerr(lngFsiz,strFext)
dim intFerr
intFerr=0
if lngFsiz>m_MaxSize and m_MaxSize>0 then
if m_Error=0 or m_Error=2 then m_Error=m_Error+1
intFerr=intFerr+1
end if
if Instr(1,LCase("/"&m_FileType&"/"),LCase("/"&strFext&"/"))=0 and m_FileType<>"" then
if m_Error<2 then m_Error=m_Error+2
intFerr=intFerr+2
end if
GetFerr=intFerr
End Function


Public Function Save(Item,strFnam)
Save=false
if m_dicForm.Exists(Item&"_From") then
dim intFerr,strFext
strFext=m_dicForm(Item&"_Ext")
intFerr=GetFerr(m_dicForm(Item&"_Size"),strFext)
if m_dicForm.Exists(Item&"_Err") then
if intFerr=0 then
m_dicForm(Item&"_Err")=0
end if
else
m_dicForm.Add Item&"_Err",intFerr
end if
if intFerr<>0 then Exit Function
if VarType(strFnam)=2 then
select case strFnam
case 0:strFnam=GetTimeStr()
if strFext<>"" then strFnam=strFnam&"."&strFext
case 1:strFnam=m_dicForm(Item&"_Name")
end select
end if
m_binItem.Type = 1
m_binItem.Open
m_binForm.Position = m_dicForm(Item&"_From")
m_binForm.CopyTo m_binItem,m_dicForm(Item&"_Size")
m_binItem.SaveToFile Server.MapPath(m_SavePath&strFnam),2
m_binItem.Close()
if m_dicForm.Exists(Item) then
m_dicForm(Item)=strFnam
else
m_dicForm.Add Item,strFnam
end if
Save=true
end if
End Function


Public Function GetData(Item)
GetData=""
if m_dicForm.Exists(Item&"_From") then
if GetFerr(m_dicForm(Item&"_Size"),m_dicForm(Item&"_Ext"))<>0 then Exit Function
m_binForm.Position = m_dicForm(Item&"_From")
GetData = m_binForm.Read(m_dicForm(Item&"_Size"))
end if
End Function


Public Function Form(Item)
if m_dicForm.Exists(Item) then
Form=m_dicForm(Item)
else
Form=""
end if
End Function


Private Function BinVal2(bin)
dim lngValue,i
lngValue=0
for i = lenb(bin) to 1 step -1
lngValue = lngValue *256 + Ascb(midb(bin,i,1))
next
BinVal2=lngValue
End Function


Private Function Bin2Val(bin)
dim lngValue,i
lngValue=0
for i = 1 to lenb(bin)
lngValue = lngValue *256 + Ascb(midb(bin,i,1))
next
Bin2Val=lngValue
End Function


Private Function Num2Str(num, base, lens)
Dim ret,i
ret = ""
while(num >= base)
i   = num Mod base
ret = i & ret
num = (num - i) / base
wend
Num2Str = Right(String(lens, "0") & num & ret, lens)
End Function


Private Function Str2Num(str, base)
Dim ret, i
ret = 0 
for i = 1 to Len(str)
ret = ret * base + Cint(Mid(str, i, 1))
next
Str2Num = ret
End Function



'检测文件夹是否存在,不存在则创建
Function CheckFolder(CFolder)
Dim MyFileObject,MyPath
Set MyFileObject=Server.CreateObject("Scripting.FileSystemObject")
MyPath=server.mappath(CFolder)
IF MyFileObject.FolderExists(MyPath)=False Then 
MyFileObject.Createfolder(MyPath) 
End IF
Set MyFileObject = Nothing
End Function


End Class
%>
  • 暂无评论