利用SA FileUp组件进行多文件上传
程序员文章站
2023-08-13 22:04:36
大家可以根据自己的实际情况进行修改,特别是数据库操作部分。============================================利用稻香老农的无组件进行多...
大家可以根据自己的实际情况进行修改,特别是数据库操作部分。
============================================
利用稻香老农的无组件进行多文件上传
请见:
http://bbs.blueidea.com/viewthread.php?tid=1249535
==================================================
本例属于文件和表单项的混合提交。
简单说明:
虽然重点在处理页上,但我觉得有必要介绍一下表单的项目。
本例是相册里像片的上传。
其中的groupid是隐藏域传递的大类的id
其中的albumid是隐藏域传递的小类的id
file1-->>file5是文件
phototitle1-->>phototitle5 是像片的标题
photointro1-->>photointro5 是像片的简介
photowidth1-->>photowidth5 是像片的宽度
photoheight1-->>photoheigth5 是像片的高度
photosize1-->>photosize5 是像片的大小。
注意:因本人没有声明变量的习惯,所以大家要是强制声明变量的话,就得自己加了。
===========================================
提交页:主要代码如下
<form name="fileform" method="post" action="photo_savephoto.asp" enctype="multipart/form-data" onsubmit="return checkform();">
<input name="groupid" type="hidden" id="groupid" value="<%=groupid%>" size="10">
<input name="albumid" type="hidden" id="albumid" value="<%=albumid%>" size="10">
上传说明:最多可以同时上传五个文件,其中标题最多30字,简介200字。
<table width="96%" align="center" cellpadding="4" cellspacing="2">
<tr align="center" valign="middle">
<td align="left" bgcolor="#f4cece" id="upid">文件1</td>
<td height="29" align="left" bgcolor="#f4cece" id="upid"> <input name="file1" type="file" class="myinput" size="20">
宽: <input name="photowidth1" type="text" class="input-disabled" id="photowidth1" size="5" readonly="">
高: <input name="photoheight1" type="text" class="input-disabled" id="photoheight1" size="5" readonly="">
大小:<input name="filesize1" type="text" class="input-disabled" id="filesize1" size="5" readonly="">
</td>
</tr>
<tr align="center" valign="middle">
<td colspan="2" align="left" bgcolor="#fdf2f2" id="upid">
标题: <input name="phototitle1" type="text" class="myinput" id="phototitle1" size="40">
<br>
简介:<textarea name="photointro1" cols="60" rows="5" class="myinput" id="photointro1"></textarea>
</td>
</tr>
。。。。。。。
<tr align="center" valign="middle" bgcolor="#f4cece">
<td height="24" colspan="2">
<input name="submit" type="submit" class="mybutton" value="开始上传">
<input name="submit2" type="reset" class="mybutton" value="重新填写">
</td>
</tr>
</table>
</form>
下面的script是我用来检查图片属性的。其中检查了图片的宽度,高度,大小,是否是图片。
<script language="javascript">
<!--
var upfilesize=<%=upfilesize%>;
//-->
</script>
<script language="javascript" src="photo_addphoto.js"></script>
<table width="98%" border="0" cellpadding="0" cellspacing="0" style="table-layout:fixed;">
<tr>
<td height="1"><img src="images/add.gif" id="loadphoto" onload="if(!firstload)showinfo();"></td>
</tr>
</table>
photo_addphoto.js
提示:您可以先修改部分代码再运行===============
这里只写出了一个文件的相关输入框,其他四个类似。
说明一点,并不是有5 个的限制,只是我这里只写了5个。
其中的photo_addphoto.js在客户端对图片进行一遍检查,不是图片的会提示,大小超过限制的也会提示。
这样就避免了用户经过耐心的等待后才被告诉不合标准的烦恼。
=================================================
处理页代码:
<%@ codepage="936"%>
<%
server.scripttimeout=5000 '--脚本超时设置为5000
%>
<!--#include file="conn.asp" -->
<%
set ofileup = server.createobject("softartisans.fileup") '--建立sa fileup object
'ofileup.path = server.mappath("/upfile/") '--我这里要自己定义文件名,所以没有使用path属性,注意这句被注释掉了。
strrestrictby = "" '--文件验证方式,分为extension和contenttype
icount=0 '--文件上传数的计数变量
formpath="upfile/" '文件保存位置
'-----------检查是否有在此位置上传的权限-----------这里省略了。
groupid=trim(ofileup.form("groupid"))
albumid=trim(ofileup.form("albumid"))
'-----------检查权限完成------
if errmsg="" then '----如果到此还没有错误
for each strformelement in ofileup.form
if isobject(ofileup.form(strformelement)) then '如果是文件
if not ofileup.form(strformelement).isempty then '--文件不为空
flagok=1
'--不是文件或文件大于限制,设置错误信息
if ofileup.form(strformelement).totalbytes<100 then
flagok=0
elseif ofileup.form(strformelement).totalbytes> upfilesize then
flagok=0
errmsg=errmsg+"文件:"&ofileup.form(strformelement).userfilename&" 大于"&upfilesize\1024&"kb!<br>"
else
strshortfilename = mid(ofileup.form(strformelement).userfilename,instrrev(ofileup.form(strformelement).userfilename, "\")+1) '取得文件名
strextension = mid(strshortfilename, instrrev(strshortfilename, ".")) '取得扩展名
if strrestrictby = "extension" then '--验证方式为扩展名
'strshortfilename = ofileup.form(strformelement).shortfilename
'======检查后缀名====
select case lcase(strextension)
case ".jpg", ".gif", ".bmp",".png"
case else
flagok=0
ofileup.form(strformelement).delete
response.write("<b>错误:</b> 扩展名为 <i>" & strextension & "</i> 的文件不能被上传。<br>")
end select
else '--验证方式为mime类型
strcontenttype = ofileup.form(strformelement).contenttype
select case lcase(strcontenttype)
case "image/gif", "image/jpeg", "image/pjpeg"
case else
flagok=0
ofileup.form(strformelement).delete
response.write("<b>错误:</b> mime类型为 <i>" & strcontenttype & "</i> 的文件不能被上传。<br>")
end select
end if '--end if 验证方式
end if 'end if 文件大小判断
if flagok=1 then '如果文件通过检查,保存文件,并插入数据库纪录
randomize
rannum=int(900*rnd)+100
filename=year(now())&month(now())&day(now())&hour(now())&minute(now())&second(now())&rannum&lcase(strextension)
ofileup.form(strformelement).saveinvirtual formpath&filename '让文件名不重复,保存文件,这里用的是saveinvirtual方法
'--输出服务器上的文件路径
response.write ofileup.form(strformelement).servername & ":servername<br>"
'--输出客户端的文件路径
response.write "<br><b>文件:</b>"&ofileup.form(strformelement).userfilename & "<br>"
'--输出该文件的大小
response.write "<b>大小:</b>"&ofileup.form(strformelement).totalbytes & "<br>"
'===添加文件的信息到数据库里===
myindex=right(strformelement,1) '--取得文件的序号,如file1则取得为1,file2取得为2
temp_phototitle=ofileup.form("phototitle"+myindex) '--这四行取得对应的标题,简介,宽度,高度
temp_photointro=ofileup.form("photointro"+myindex)
temp_photowidth=ofileup.form("photowidth"+myindex)
temp_photoheight=ofileup.form("photoheight"+myindex)
'====检查输入,为空则给初值==
temp_phototitle=replace(trim(temp_phototitle),"'","''")
if temp_phototitle="" then
temp_phototitle="没有填写"
end if
temp_photointro=replace(trim(temp_photointro),"'","''")
if temp_photointro="" then
temp_photointro="没有填写"
end if
if temp_photowidth="" or not isnumeric(temp_photowidth) then
temp_photowidth=160
end if
if temp_photoheight="" or not isnumeric(temp_photoheight) then
temp_photoheight=120
end if
'===插入数据库===
filesize=ofileup.form(strformelement).totalbytes
sql="insert into tbl_photo(albumid,groupid,username,addtime,photofilename,phototitle,photointro,photoclick,photosize,photowidth,photoheight,locked,viewpassword) values("&albumid&","&groupid&",'"&session("username")&"','"&now()&"','"&filename&"','"&temp_phototitle&"','"&temp_photointro&"',1,"&filesize&","&temp_photowidth&","&temp_photoheight&",'no','')"
conn.execute sql
sql="update tbl_album set photocount=photocount+1 where albumid="&albumid
conn.execute sql
sql="update tbl_group set photocount=photocount+1 where groupid="&groupid
conn.execute sql
'===输出上传成功信息===
icount=icount+1
end if
else
response.write strformelement & "对象为空!"
end if '--end if 对象为空
end if '--end if 是否是文件
next
set ofileup = nothing '删除此对象
end if '--end if 没有错误信息
response.write "<br>"&icount&" 个文件上传结束!"
response.write "<br><a href='photo_listphoto.asp?albumid="&albumid&"'><b>返回相册</b></a>"
'=====如果有错,输出错误信息=====
if errmsg<>"" then
response.write "<br>"&errmsg
response.write "<input type='button' onclick='history.go(-1)' value='返回' class='myinput'>"
end if
conn.close
set conn=nothing
%>
===================================================
看完了实例,下面对sa fileup的属性和方法进行简单的介绍,免得大家初次接触感到发晕。
这些是我觉得比较常用的,例句和注释都是按我的理解写的。
如果大家在应用中发现有什么问题,请指出。谢谢。
建立sa fileup 对象的方法:
set ofileup = server.createobject("softartisans.fileup")
取出表单所有项的方法:
for each strformelement in ofileup.form
用 ofileup.form(strformelement)就可以引用每个对象,文件也是这样
注意:如果是多选下拉框,则用ofileup.formex(strformelement)
可以这样来遍历它。
for each strsubitem in ofileup.formex(strformelement)
response.write( strsubitem & "<br>")
next
next
contenttype属性:
ofileup.form(strformelement).contenttype
可以得到文件的mime类型
isempty属性
ofileup.form(strformelement).isempty
可以知道用户是不是指定了一个无效的文件
maxbytes属性
ofileup.form(strformelement).maxbytes=30000
指定文件的限制,单位为byte,如果超过它,那么只存储maxbytes指定的大小。其余舍弃。
servername属性
ofileup.form(strformelement).servername
可以得到文件保存到服务器的完整路径。
shortfilename属性
ofileup.form(strformelement).shortfilename
可以得到客户端的文件名,注意只是文件名,我这里没有用,因为报错。呵呵。
userfilename属性
ofileup.form(strformelement).userfilename
可以得到客户端文件的完整路径。可以输出一下给用户看看。
totalbytes属性
ofileup.form(strformelement).totalbytes
可以得到文件的大小,单位为byte
saveinvirtual(路径)方法
ofileup.form(strformelement).saveinvirtual "upfile/"
ofileup.form(strformelement).saveinvirtual "upfile/aa.abc"
如果只指定了路径,则保留原文件名,否则按指定指定文件名保存
服务器管理员可以禁止掉其他所有方法,但这个一定会留的。
saveas (文件名)方法
ofileup.form(strformelement).saveas "c:\aa\a.tmp"
如果没有指定路径,只是指定了文件名,那么将用path属性指定的路径。
path属性一会介绍。
save方法
ofileup.path="d:\wwwroot\abc\upfile\"
注意必须是真实路径,可以用server.mappath来转换虚拟路径。
ofileup.form(strformelement).save
不能指定文件名喽。
注意:path属性必须在提到任何表单项之前,建议放在
set ofileup = server.createobject("softartisans.fileup")
的后面。前提是你用的话。
delete (文件名,可选)
ofileup.form(strformelement).delete
从服务器上删除文件,如果不指定文件名,则删除当前的文件。
如果指定的话,必须是文件的完整路径。
flush方法
ofileup.flush
当你不想保存任何东西的时候,可以用它来放弃全部的输入流。
============================================
利用稻香老农的无组件进行多文件上传
请见:
http://bbs.blueidea.com/viewthread.php?tid=1249535
==================================================
本例属于文件和表单项的混合提交。
简单说明:
虽然重点在处理页上,但我觉得有必要介绍一下表单的项目。
本例是相册里像片的上传。
其中的groupid是隐藏域传递的大类的id
其中的albumid是隐藏域传递的小类的id
file1-->>file5是文件
phototitle1-->>phototitle5 是像片的标题
photointro1-->>photointro5 是像片的简介
photowidth1-->>photowidth5 是像片的宽度
photoheight1-->>photoheigth5 是像片的高度
photosize1-->>photosize5 是像片的大小。
注意:因本人没有声明变量的习惯,所以大家要是强制声明变量的话,就得自己加了。
===========================================
提交页:主要代码如下
<form name="fileform" method="post" action="photo_savephoto.asp" enctype="multipart/form-data" onsubmit="return checkform();">
<input name="groupid" type="hidden" id="groupid" value="<%=groupid%>" size="10">
<input name="albumid" type="hidden" id="albumid" value="<%=albumid%>" size="10">
上传说明:最多可以同时上传五个文件,其中标题最多30字,简介200字。
<table width="96%" align="center" cellpadding="4" cellspacing="2">
<tr align="center" valign="middle">
<td align="left" bgcolor="#f4cece" id="upid">文件1</td>
<td height="29" align="left" bgcolor="#f4cece" id="upid"> <input name="file1" type="file" class="myinput" size="20">
宽: <input name="photowidth1" type="text" class="input-disabled" id="photowidth1" size="5" readonly="">
高: <input name="photoheight1" type="text" class="input-disabled" id="photoheight1" size="5" readonly="">
大小:<input name="filesize1" type="text" class="input-disabled" id="filesize1" size="5" readonly="">
</td>
</tr>
<tr align="center" valign="middle">
<td colspan="2" align="left" bgcolor="#fdf2f2" id="upid">
标题: <input name="phototitle1" type="text" class="myinput" id="phototitle1" size="40">
<br>
简介:<textarea name="photointro1" cols="60" rows="5" class="myinput" id="photointro1"></textarea>
</td>
</tr>
。。。。。。。
<tr align="center" valign="middle" bgcolor="#f4cece">
<td height="24" colspan="2">
<input name="submit" type="submit" class="mybutton" value="开始上传">
<input name="submit2" type="reset" class="mybutton" value="重新填写">
</td>
</tr>
</table>
</form>
下面的script是我用来检查图片属性的。其中检查了图片的宽度,高度,大小,是否是图片。
<script language="javascript">
<!--
var upfilesize=<%=upfilesize%>;
//-->
</script>
<script language="javascript" src="photo_addphoto.js"></script>
<table width="98%" border="0" cellpadding="0" cellspacing="0" style="table-layout:fixed;">
<tr>
<td height="1"><img src="images/add.gif" id="loadphoto" onload="if(!firstload)showinfo();"></td>
</tr>
</table>
photo_addphoto.js
提示:您可以先修改部分代码再运行
这里只写出了一个文件的相关输入框,其他四个类似。
说明一点,并不是有5 个的限制,只是我这里只写了5个。
其中的photo_addphoto.js在客户端对图片进行一遍检查,不是图片的会提示,大小超过限制的也会提示。
这样就避免了用户经过耐心的等待后才被告诉不合标准的烦恼。
=================================================
处理页代码:
<%@ codepage="936"%>
<%
server.scripttimeout=5000 '--脚本超时设置为5000
%>
<!--#include file="conn.asp" -->
<%
set ofileup = server.createobject("softartisans.fileup") '--建立sa fileup object
'ofileup.path = server.mappath("/upfile/") '--我这里要自己定义文件名,所以没有使用path属性,注意这句被注释掉了。
strrestrictby = "" '--文件验证方式,分为extension和contenttype
icount=0 '--文件上传数的计数变量
formpath="upfile/" '文件保存位置
'-----------检查是否有在此位置上传的权限-----------这里省略了。
groupid=trim(ofileup.form("groupid"))
albumid=trim(ofileup.form("albumid"))
'-----------检查权限完成------
if errmsg="" then '----如果到此还没有错误
for each strformelement in ofileup.form
if isobject(ofileup.form(strformelement)) then '如果是文件
if not ofileup.form(strformelement).isempty then '--文件不为空
flagok=1
'--不是文件或文件大于限制,设置错误信息
if ofileup.form(strformelement).totalbytes<100 then
flagok=0
elseif ofileup.form(strformelement).totalbytes> upfilesize then
flagok=0
errmsg=errmsg+"文件:"&ofileup.form(strformelement).userfilename&" 大于"&upfilesize\1024&"kb!<br>"
else
strshortfilename = mid(ofileup.form(strformelement).userfilename,instrrev(ofileup.form(strformelement).userfilename, "\")+1) '取得文件名
strextension = mid(strshortfilename, instrrev(strshortfilename, ".")) '取得扩展名
if strrestrictby = "extension" then '--验证方式为扩展名
'strshortfilename = ofileup.form(strformelement).shortfilename
'======检查后缀名====
select case lcase(strextension)
case ".jpg", ".gif", ".bmp",".png"
case else
flagok=0
ofileup.form(strformelement).delete
response.write("<b>错误:</b> 扩展名为 <i>" & strextension & "</i> 的文件不能被上传。<br>")
end select
else '--验证方式为mime类型
strcontenttype = ofileup.form(strformelement).contenttype
select case lcase(strcontenttype)
case "image/gif", "image/jpeg", "image/pjpeg"
case else
flagok=0
ofileup.form(strformelement).delete
response.write("<b>错误:</b> mime类型为 <i>" & strcontenttype & "</i> 的文件不能被上传。<br>")
end select
end if '--end if 验证方式
end if 'end if 文件大小判断
if flagok=1 then '如果文件通过检查,保存文件,并插入数据库纪录
randomize
rannum=int(900*rnd)+100
filename=year(now())&month(now())&day(now())&hour(now())&minute(now())&second(now())&rannum&lcase(strextension)
ofileup.form(strformelement).saveinvirtual formpath&filename '让文件名不重复,保存文件,这里用的是saveinvirtual方法
'--输出服务器上的文件路径
response.write ofileup.form(strformelement).servername & ":servername<br>"
'--输出客户端的文件路径
response.write "<br><b>文件:</b>"&ofileup.form(strformelement).userfilename & "<br>"
'--输出该文件的大小
response.write "<b>大小:</b>"&ofileup.form(strformelement).totalbytes & "<br>"
'===添加文件的信息到数据库里===
myindex=right(strformelement,1) '--取得文件的序号,如file1则取得为1,file2取得为2
temp_phototitle=ofileup.form("phototitle"+myindex) '--这四行取得对应的标题,简介,宽度,高度
temp_photointro=ofileup.form("photointro"+myindex)
temp_photowidth=ofileup.form("photowidth"+myindex)
temp_photoheight=ofileup.form("photoheight"+myindex)
'====检查输入,为空则给初值==
temp_phototitle=replace(trim(temp_phototitle),"'","''")
if temp_phototitle="" then
temp_phototitle="没有填写"
end if
temp_photointro=replace(trim(temp_photointro),"'","''")
if temp_photointro="" then
temp_photointro="没有填写"
end if
if temp_photowidth="" or not isnumeric(temp_photowidth) then
temp_photowidth=160
end if
if temp_photoheight="" or not isnumeric(temp_photoheight) then
temp_photoheight=120
end if
'===插入数据库===
filesize=ofileup.form(strformelement).totalbytes
sql="insert into tbl_photo(albumid,groupid,username,addtime,photofilename,phototitle,photointro,photoclick,photosize,photowidth,photoheight,locked,viewpassword) values("&albumid&","&groupid&",'"&session("username")&"','"&now()&"','"&filename&"','"&temp_phototitle&"','"&temp_photointro&"',1,"&filesize&","&temp_photowidth&","&temp_photoheight&",'no','')"
conn.execute sql
sql="update tbl_album set photocount=photocount+1 where albumid="&albumid
conn.execute sql
sql="update tbl_group set photocount=photocount+1 where groupid="&groupid
conn.execute sql
'===输出上传成功信息===
icount=icount+1
end if
else
response.write strformelement & "对象为空!"
end if '--end if 对象为空
end if '--end if 是否是文件
next
set ofileup = nothing '删除此对象
end if '--end if 没有错误信息
response.write "<br>"&icount&" 个文件上传结束!"
response.write "<br><a href='photo_listphoto.asp?albumid="&albumid&"'><b>返回相册</b></a>"
'=====如果有错,输出错误信息=====
if errmsg<>"" then
response.write "<br>"&errmsg
response.write "<input type='button' onclick='history.go(-1)' value='返回' class='myinput'>"
end if
conn.close
set conn=nothing
%>
===================================================
看完了实例,下面对sa fileup的属性和方法进行简单的介绍,免得大家初次接触感到发晕。
这些是我觉得比较常用的,例句和注释都是按我的理解写的。
如果大家在应用中发现有什么问题,请指出。谢谢。
建立sa fileup 对象的方法:
set ofileup = server.createobject("softartisans.fileup")
取出表单所有项的方法:
for each strformelement in ofileup.form
用 ofileup.form(strformelement)就可以引用每个对象,文件也是这样
注意:如果是多选下拉框,则用ofileup.formex(strformelement)
可以这样来遍历它。
for each strsubitem in ofileup.formex(strformelement)
response.write( strsubitem & "<br>")
next
next
contenttype属性:
ofileup.form(strformelement).contenttype
可以得到文件的mime类型
isempty属性
ofileup.form(strformelement).isempty
可以知道用户是不是指定了一个无效的文件
maxbytes属性
ofileup.form(strformelement).maxbytes=30000
指定文件的限制,单位为byte,如果超过它,那么只存储maxbytes指定的大小。其余舍弃。
servername属性
ofileup.form(strformelement).servername
可以得到文件保存到服务器的完整路径。
shortfilename属性
ofileup.form(strformelement).shortfilename
可以得到客户端的文件名,注意只是文件名,我这里没有用,因为报错。呵呵。
userfilename属性
ofileup.form(strformelement).userfilename
可以得到客户端文件的完整路径。可以输出一下给用户看看。
totalbytes属性
ofileup.form(strformelement).totalbytes
可以得到文件的大小,单位为byte
saveinvirtual(路径)方法
ofileup.form(strformelement).saveinvirtual "upfile/"
ofileup.form(strformelement).saveinvirtual "upfile/aa.abc"
如果只指定了路径,则保留原文件名,否则按指定指定文件名保存
服务器管理员可以禁止掉其他所有方法,但这个一定会留的。
saveas (文件名)方法
ofileup.form(strformelement).saveas "c:\aa\a.tmp"
如果没有指定路径,只是指定了文件名,那么将用path属性指定的路径。
path属性一会介绍。
save方法
ofileup.path="d:\wwwroot\abc\upfile\"
注意必须是真实路径,可以用server.mappath来转换虚拟路径。
ofileup.form(strformelement).save
不能指定文件名喽。
注意:path属性必须在提到任何表单项之前,建议放在
set ofileup = server.createobject("softartisans.fileup")
的后面。前提是你用的话。
delete (文件名,可选)
ofileup.form(strformelement).delete
从服务器上删除文件,如果不指定文件名,则删除当前的文件。
如果指定的话,必须是文件的完整路径。
flush方法
ofileup.flush
当你不想保存任何东西的时候,可以用它来放弃全部的输入流。
上一篇: jsp 编程之@WebServlet详解