一个函数无组件上传文件
类别: ASP教程
<%
\'\'\'\'\'==============================
\'函数名:upfile
\'作用: 使用"化境上传组件"上传文件到服务器上
\'参数: file1 文件对象
\' savepath 文件要保存的相对路径,如"../"上一级上录,""同目录
\' maxsize 允许上传文件的最大值,单位KB.为0不限大小.
\' savetype 允许上传文件的类型,0不限制,限制的格式.如.jpg|.bmp|.zip
\'返回值:返回上传信息,也可自己根据须要设定返回值
\'前提:set upload=new upload_5xsoft \'\'建立上传对象
\' set file1=upload.file("file1") \'\'生成一个文件对象
\'Designer:suercool
function upfile(file1,savepath,maxsize,savetype)
if file1.filename="" and file1.filesize<=0 then
upfile="<script language=\'javascript\'>alert(\'文件不存在!\')</script>"
exit function
end if
if maxsize<>"0" and file1.filesize>clng(maxsize)*1024 then
upfile="<script language=\'javascript\'>alert(\'文件大小超过了限制,最大只能上传" & cstr(maxsize) & "Kb的文件!\')</script>"
exit function
end if
dim filename,filetype
filename=file1.filename
filetype=getfiletype(filename)
if savetype<>"0" then
dim arrtype,i,foundtype
arrtype=split(savetype,"|")
foundtype=false
for i = 0 to ubound(arrtype)
if lcase(arrtype(i))=filetype then
foundtype=true
exit for
end if
next
if not foundtype then
upfile="<script language=\'javascript\'>alert(\'文件格式不下确,只允许上传" & savetype &"格式的文件!\')</script>"
exit function
end if
end if
randomize()
filepath=savepath&year(now)&month(now)&day(now)&hour(now)&minute(now)&second(now)&int(rnd*9999)&filetype
file1.saveas server.mappath(filepath)
if err.description<>"" then
upfile="<script language=\'javascript\'>alert(\'出现未知错误,上传失败,请重试!\')</script>"
err.clear
else
upfile="<script language=\'javascript\'>alert(\'上传成功!"+filepath+"\');</script>"
end if
set file1=nothing
end function
\'\'\'\'\'\'\'\'===========
\'函数名:getfiletype
\'作用: 得到文件的类型
\'参数: filename文件名
\'返回值:文件类型,无类型返回""
\'Designer:suercool
function getfiletype(filename)
dim i
for i=(len(filename)-1) to 1 step -1
if mid(filename,i,1)="." then
getfiletype=lcase(mid(filename,i))
exit function
end if
next
getfile=""
end function
%>
实例(upfile3.asp):
<!--#include FILE="upload_5xsoft.inc"-->
<html>
<head>
<title>Upfile</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body>
<form action="upfile3.asp" name="frmupfile" id="frmupfle" enctype="multipart/form-data" method=post>
<input type=file name="file1">
<input type=submit value="上传" name=subupfile>
<input type=hidden name="upfilename" value="">
<% set upload=new upload_5xsoft \'\'建立上传对象
if upload.form("subupfile")="上传" then
set file1=upload.file("file1")
dim re
savetype=".jpg|.htm|.bmp|"
re=upfile(file1,"","0",savetype)
response.write re
end if
%>
</form>
</body>
</html>
\'\'\'\'\'==============================
\'函数名:upfile
\'作用: 使用"化境上传组件"上传文件到服务器上
\'参数: file1 文件对象
\' savepath 文件要保存的相对路径,如"../"上一级上录,""同目录
\' maxsize 允许上传文件的最大值,单位KB.为0不限大小.
\' savetype 允许上传文件的类型,0不限制,限制的格式.如.jpg|.bmp|.zip
\'返回值:返回上传信息,也可自己根据须要设定返回值
\'前提:set upload=new upload_5xsoft \'\'建立上传对象
\' set file1=upload.file("file1") \'\'生成一个文件对象
\'Designer:suercool
function upfile(file1,savepath,maxsize,savetype)
if file1.filename="" and file1.filesize<=0 then
upfile="<script language=\'javascript\'>alert(\'文件不存在!\')</script>"
exit function
end if
if maxsize<>"0" and file1.filesize>clng(maxsize)*1024 then
upfile="<script language=\'javascript\'>alert(\'文件大小超过了限制,最大只能上传" & cstr(maxsize) & "Kb的文件!\')</script>"
exit function
end if
dim filename,filetype
filename=file1.filename
filetype=getfiletype(filename)
if savetype<>"0" then
dim arrtype,i,foundtype
arrtype=split(savetype,"|")
foundtype=false
for i = 0 to ubound(arrtype)
if lcase(arrtype(i))=filetype then
foundtype=true
exit for
end if
next
if not foundtype then
upfile="<script language=\'javascript\'>alert(\'文件格式不下确,只允许上传" & savetype &"格式的文件!\')</script>"
exit function
end if
end if
randomize()
filepath=savepath&year(now)&month(now)&day(now)&hour(now)&minute(now)&second(now)&int(rnd*9999)&filetype
file1.saveas server.mappath(filepath)
if err.description<>"" then
upfile="<script language=\'javascript\'>alert(\'出现未知错误,上传失败,请重试!\')</script>"
err.clear
else
upfile="<script language=\'javascript\'>alert(\'上传成功!"+filepath+"\');</script>"
end if
set file1=nothing
end function
\'\'\'\'\'\'\'\'===========
\'函数名:getfiletype
\'作用: 得到文件的类型
\'参数: filename文件名
\'返回值:文件类型,无类型返回""
\'Designer:suercool
function getfiletype(filename)
dim i
for i=(len(filename)-1) to 1 step -1
if mid(filename,i,1)="." then
getfiletype=lcase(mid(filename,i))
exit function
end if
next
getfile=""
end function
%>
实例(upfile3.asp):
<!--#include FILE="upload_5xsoft.inc"-->
<html>
<head>
<title>Upfile</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body>
<form action="upfile3.asp" name="frmupfile" id="frmupfle" enctype="multipart/form-data" method=post>
<input type=file name="file1">
<input type=submit value="上传" name=subupfile>
<input type=hidden name="upfilename" value="">
<% set upload=new upload_5xsoft \'\'建立上传对象
if upload.form("subupfile")="上传" then
set file1=upload.file("file1")
dim re
savetype=".jpg|.htm|.bmp|"
re=upfile(file1,"","0",savetype)
response.write re
end if
%>
</form>
</body>
</html>
- 上一篇: 无组件上传图片到数据库中,最完整解决方案
- 下一篇: 无组件上传例程
-= 资 源 教 程 =-
文 章 搜 索