目 录
1.关于 upload_5xsoft
2.运行平台与注意事项
2.类的成员与对象
3.使用示例
关于 upload_5xsoft
一直以来,由于FileSystemObject的局限,所以ASP最大的难题就是文件上传,大多解决法就是安装
第三方上传组件。可第三方组件有很多问题,有的组件要注册,有的组件要在表单中加上他的版权信息。
还有的就是组件的兼容问题。
在网上也流传了很多无组件上传的代码,但都是只能上传文本文件,或是只能将文件上传到数据库中。
我这段时间在研究ASP,发现可以不用第三方组件上传任意类型的文件。就写了这个类,给大家一
个方便,整个类放在一个文件中: upload_5xsoft.inc 在 Example 目录下还有一个完整的多文件上传示
例程序,可以直接使用。
申明:源代码是完全开放的,可能随意传播,但请保留其完整性,未经作者同意,不得用于商业。
运行平台与注意事项
a)只能运行于 Windows2000+IIS 5,不支持 NT4+IIS4 或是 Win98+PWS, 只要在ASP中加上:
就行了
b) 在使用文件上传时, 表单 form 要加上 enctype="multipart/form-data" 即:
upload_5xsoft的对象
如定义一个上传对象
<%
set upload=new upload_5xsoft 'upload就是一个对象
%>
upload_5xsoft 对象成员
file 文件对象集,(是个dictionary对象)
文件对象成员:
Count 属性,文件表单的个数
FileName 属性,上传文件的名字
FileSize 属性,上传文件的大小(为0是表示没有文件)
FilePath 属性,上传前文件所在的路径
FormName 属性,文件表单的名字
SaveAs 方法,储存上传后文件,有一个参数,路径要为真实路径如:
例子: set file=upload.file("file1") 'file1为表单名
response.write "
文件名:"&file.FileName
response.write "
文件大小:"&file.FileSize
response.write "
文件路径:"&file.FilePath
set file=nothing
form 表单数据集,(是个dictionary对象)用来代替 Request.Form
count 属性,表单数
exists 方法,检查是否有指定的表单名
更多的用法可看 vbscript 的dictionary对象帮助
例子:
'得到text1表单的数据,uplaod就是一开始创建的对象
sText=upload.form("text1")
Version 属性,upload_5xsoft类的版本号,如:
response.write upload.Version
使用示例
1.上传一个jpg文件的示例:
文件1: upload.htm
文件2: upload.asp
<%
set upload=new upload_5xsoft
set file=upload.file("file1")
if file.fileSize>0 then
response.write "
上传文件
response.write "
文件大小:"&file.FileSize
set file=nothing
end if
set upload=nothing
%>
2.列表出有文件表单(多文件上传)
<%
set upload=new upload_5xsoft
for each formName in upload.file
set file=upload.file(formName)
if file.FileSize>0 then
file.SaveAs Server.mappath(file.FileName)
response.write file.FilePath&file.FileName&" ("&file.FileSize&") => "
response.write file.FileName&" 成功!
"
end if
set file=nothing
next
set upload=nothing
%>
你还可能直接使用作者写好了的上传程序在example目录中
立即下载
若程序有问题,请写作者联系 getc@163.com
-----------------------------------------------------------------------------------------------------------------------------
稻香老农
[ASP]利用稻香老农的无组件上传类进行多文件上传
本例使用稻香老农的无组件上传类进行多文件和表单项的混合上传。
大家可以根据自己的实际情况进行修改。
简单说明:
本例是相册里像片的上传。
其中的groupID是隐藏域传递的大类的ID
其中的albumID是隐藏域传递的小类的ID
file1-->>file5是文件
photoTitle1-->>photoTitle5 是像片的标题
photoIntro1-->>photoIntro5 是像片的简介
photoWidth1-->>photoWidth5 是像片的宽度
photoHeight1-->>photoHeigth5 是像片的高度
photoSize1-->>photoSize5 是像片的大小。
提交页面
=============
请见
www.blueidea.com/bbs/newsdetail.asp?id=1249224&posts=current
==================================================
处理页面
====================================
<%@ CODEPAGE="936"%>
<%
Server.ScriptTimeOut=5000 '--脚本超时设置为5000
%>
<%
set upload=new upload_5xsoft '建立上传对象
formPath="upfile/" '--上传路径
upFileSize=1048576 '设置文件为1M
iCount=0 '--上传文件个数的计数
'-----------检查是否有在此位置上传的权限--------这里省略。。
groupID=trim(upload.form("groupID"))
albumID=trim(upload.form("albumID"))
'-----------检查权限完成------
if errMsg="" then '--如果以上的检查没有错误,那么继续
for each formName in upload.objFile '列出所有上传了的文件
set file=upload.file(formName) '生成一个文件对象
if file.FileSize>0 then '如果 FileSize > 0 说明有文件数据
fileOK=1
fileExt=lcase(right(file.Filename,4))
'===================检查后缀名=====
errMsg=errMsg+"文件:"&file.Filename&" 不是图片文件!
"
fileOK=0
end if
'==========检查文件大小=====
if file.FileSize>upFileSize then
errMsg=errMsg+"文件:"&file.Filename&" 的大小大于"&upFileSize\\1024&"KB!
"
fileOK=0
end if
if fileOK=1 then '--如果通过检查,那么保存文件
randomize
ranNum=int(900*rnd)+100
filename=year(now())&month(now())&day(now())&hour(now())&minute(now())&second(now())&ranNum&fileExt
file.SaveAs Server.mappath(formPath&filename) '让文件名不重复,保存文件
'===添加文件的信息到数据库里===
myIndex=right(formName,1) '--取得序号,如file1取得为1,file2取得为2
temp_photoTitle=upload.form("photoTitle"+myIndex) '--这四行取得文件对应的标题,简介,宽度,高度等
temp_photoIntro=upload.form("photoIntro"+myIndex)
temp_photoWidth=upload.form("photoWidth"+myIndex)
temp_photoHeight=upload.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
'=======插入数据库===
sql="insert into TBL_PHOTO(albumID,groupID,userName,addTime,photoFilename,photoTitle,photoIntro,photoClick,photoSize,photoWidth,photoHeight,locked,viewPassword) values("&albumID&
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
'======输出上传成功信息===
response.write file.FilePath&file.FileName&" ("&file.FileSize&") => "&filename&" 成功!
"
iCount=iCount+1
end if
end if
set file=nothing
next
end if
set upload=nothing ''删除此对象
response.write "
"&iCount&" 个文件上传结束!"
response.write "
返回相册"
'=============如果有错,输出错误信息=========
if errMsg<>"" then
response.write "
"&errMsg
response.write ""
end if
conn.close
set conn=nothing
%>