===============
这里只写出了一个文件的相关输入框,其他四个类似。
说明一点,并不是有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的属性和方法进行简单的介绍,免得大家初次接触感到发晕。
这些是我觉得比较常用的,例句和注释都是按我的理解写的。
如果大家在应用中发现有什么问题,请指出。谢谢。
[b]建立SA FileUp 对象的方法:[/b]
Set oFileUp = Server.CreateObject("SoftArtisans.FileUp")
[b]取出表单所有项的方法:[/b]
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
[b]ContentType属性:[/b]
oFileUp.Form(strFormElement).ContentType
可以得到文件的MIME类型
[b]IsEmpty属性[/b]
oFileUp.Form(strFormElement).IsEmpty
可以知道用户是不是指定了一个无效的文件
[b]MaxBytes属性[/b]
oFileUp.Form(strFormElement).MaxBytes=30000
指定文件的限制,单位为Byte,如果超过它,那么只存储MaxBytes指定的大小。其余舍弃。
[b]ServerName属性[/b]
oFileUp.Form(strFormElement).ServerName
可以得到文件保存到服务器的完整路径。
[b]ShortFilename属性[/b]
oFileUp.Form(strFormElement).ShortFilename
可以得到客户端的文件名,注意只是文件名,我这里没有用,因为报错。呵呵。
[b]UserFilename属性[/b]
oFileUp.Form(strFormElement).UserFileName
可以得到客户端文件的完整路径。可以输出一下给用户看看。
[b]TotalBytes属性[/b]
oFileUp.Form(strFormElement).TotalBytes
可以得到文件的大小,单位为Byte
[b]SaveInVirtual(路径)方法[/b]
oFileUp.Form(strFormElement).SaveInVirtual "upfile/"
oFileUp.Form(strFormElement).SaveInVirtual "upfile/aa.abc"
如果只指定了路径,则保留原文件名,否则按指定指定文件名保存
服务器管理员可以禁止掉其他所有方法,但这个一定会留的。
[b]SaveAs (文件名)方法[/b]
oFileUp.Form(strFormElement).SaveAs "C:\aa\a.tmp"
如果没有指定路径,只是指定了文件名,那么将用Path属性指定的路径。
Path属性一会介绍。
[b]Save方法[/b]
oFileUp.Path="D:\wwwroot\abc\upfile\"
注意必须是真实路径,可以用Server.MapPath来转换虚拟路径。
oFileUp.Form(strFormElement).Save
不能指定文件名喽。
注意:Path属性必须在提到任何表单项之前,建议放在
Set oFileUp = Server.CreateObject("SoftArtisans.FileUp")
的后面。前提是你用的话。
[b]Delete (文件名,可选)[/b]
oFileUp.Form(strFormElement).Delete
从服务器上删除文件,如果不指定文件名,则删除当前的文件。
如果指定的话,必须是文件的完整路径。
[b]Flush方法[/b]
oFileUp.Flush
当你不想保存任何东西的时候,可以用它来放弃全部的输入流。