热门文章
 
fso操作文件 实例函数
利用FSO取得BMP,JPG
FileSystemObje
fso使用:drive 读取
fso:folder 文件系
fso:file 文件对象
scripting.Text
ASP实现结构化列举并查看某
Adodb.Stream
ASP 批量文件改名
 推荐文章
 
asp无组件读取图片文件信息
限制(禁用)FSO组件的方法
fso使用例子:创建文件夹
fso:file 文件对象
fso应用中的几个小函数
ASP FSO相关的一些例子
fso的一些特殊功能
FSO读取网站系统使用空间的
使用FSO修改文件夹的名称
fso复制文件夹所有内容和删
FSO操作文件系统代码
稻香老农的无组件上传类(完整
一个基于Jscript和fs
无惧 无组件上传完整代码
asp静态模版技术之标签参数
无组件上传文件被杀毒软件误报
 
你现在的位置:您现在的位置是: 中国ASP>>ASP教程>>fso组件
无惧 无组件上传完整代码

很好用的一个基本的无组件上传的类

 

首先是class文件  upclass.inc

<%
"----------------------------------------------------------------------
"******************* 无组件上传类 ********************************
"类属性包括
"文件属性:例如上传文件为c:\myfile\doc.txt
"FileName 文件名 字符串 "doc.txt"
"FileSize 文件大小 数值 1210
"FileType 文件类型 字符串 "text/plain"
"FileExt 文件扩展名 字符串 ".txt"
"FilePath 文件原路径 字符串 "c:\myfile"
"使用时注意事项:
"由于Scripting.Dictionary区分大小写,所以在网页及ASP页的项目名都要相同的大小写
"----------------------------------------------------------------------
dim oUpFileStream
Class upload_file
dim Form,File,Version
Private Sub Class_Initialize
"定义变量
dim RequestBinDate,sStart,bCrLf,sInfo,iInfoStart,iInfoEnd,tStream,iStart,oFileInfo
dim iFileSize,sFilePath,sFileType,sFormvalue,sFileName
dim iFindStart,iFindEnd
dim iFormStart,iFormEnd,sFormName
"代码开始
Version="无组件上传类 Version 1.00"
set Form = Server.CreateObject("Scripting.Dictionary")
set File = Server.CreateObject("Scripting.Dictionary")
if Request.TotalBytes < 1 then Exit Sub
set tStream = Server.CreateObject("adodb.stream")
set oUpFileStream = Server.CreateObject("adodb.stream")
oUpFileStream.Type = 1
oUpFileStream.Mode = 3
oUpFileStream.Open
oUpFileStream.Write Request.BinaryRead(Request.TotalBytes)
oUpFileStream.Position=0
RequestBinDate = oUpFileStream.Read
iFormEnd = oUpFileStream.Size
bCrLf = chrB(13) & chrB(10)
"取得每个项目之间的分隔符
sStart = MidB(RequestBinDate,1, InStrB(1,RequestBinDate,bCrLf)-1)
iStart = LenB (sStart)
iFormStart = iStart+2
"分解项目
Do
iInfoEnd = InStrB(iFormStart,RequestBinDate,bCrLf & bCrLf)+3
tStream.Type = 1
tStream.Mode = 3
tStream.Open
oUpFileStream.Position = iFormStart
oUpFileStream.CopyTo tStream,iInfoEnd-iFormStart
tStream.Position = 0
tStream.Type = 2
tStream.Charset ="gb2312"
sInfo = tStream.ReadText
"取得表单项目名称
iFormStart = InStrB(iInfoEnd,RequestBinDate,sStart)-1
iFindStart = InStr(22,sInfo,"name=""",1)+6
iFindEnd = InStr(iFindStart,sInfo,"""",1)
sFormName = Mid (sinfo,iFindStart,iFindEnd-iFindStart)
"如果是文件
if InStr (45,sInfo,"filename=""",1) > 0 then
set oFileInfo= new FileInfo
"取得文件属性
iFindStart = InStr(iFindEnd,sInfo,"filename=""",1)+10
iFindEnd = InStr(iFindStart,sInfo,"""",1)
sFileName = Mid (sinfo,iFindStart,iFindEnd-iFindStart)
oFileInfo.FileName = GetFileName(sFileName)
oFileInfo.FilePath = GetFilePath(sFileName)
oFileInfo.FileExt = GetFileExt(sFileName)
iFindStart = InStr(iFindEnd,sInfo,"Content-Type: ",1)+14
iFindEnd = InStr(iFindStart,sInfo,vbCr)
oFileInfo.FileType = Mid (sinfo,iFindStart,iFindEnd-iFindStart)
oFileInfo.FileStart = iInfoEnd
oFileInfo.FileSize = iFormStart -iInfoEnd -2
oFileInfo.FormName = sFormName
file.add sFormName,oFileInfo
else
"如果是表单项目
tStream.Close
tStream.Type = 1
tStream.Mode = 3
tStream.Open
oUpFileStream.Position = iInfoEnd
oUpFileStream.CopyTo tStream,iFormStart-iInfoEnd-2
tStream.Position = 0
tStream.Type = 2
tStream.Charset = "gb2312"
sFormvalue = tStream.ReadText
form.Add sFormName,sFormvalue
end if
tStream.Close
iFormStart = iFormStart+iStart+2
"如果到文件尾了就退出
loop until (iFormStart+2) = iFormEnd
RequestBinDate=""
set tStream = nothing
End Sub

Private Sub Class_Terminate
"清除变量及对像
if not Request.TotalBytes<1 then
oUpFileStream.Close
set oUpFileStream =nothing
end if
Form.RemoveAll
File.RemoveAll
set Form=nothing
set File=nothing
End Sub

"取得文件路径
Private function GetFilePath(FullPath)
If FullPath <> "" Then
GetFilePath = left(FullPath,InStrRev(FullPath, "\"))
Else
GetFilePath = ""
End If
End function

"取得文件名
Private function GetFileName(FullPath)
If FullPath <> "" Then
GetFileName = mid(FullPath,InStrRev(FullPath, "\")+1)
Else
GetFileName = ""
End If
End function

"取得扩展名
Private function GetFileExt(FullPath)
If FullPath <> "" Then
GetFileExt = mid(FullPath,InStrRev(FullPath, "."))
Else
GetFileExt = ""
End If
End function
End Class

"文件属性类
Class FileInfo
dim FormName,FileName,FilePath,FileSize,FileType,FileStart,FileExt
Private Sub Class_Initialize
FileName = ""
FilePath = ""
FileSize = 0
FileStart= 0
FormName = ""
FileType = ""
FileExt = ""
End Sub

"保存文件方法
Public function SaveToFile(FullPath)
dim oFileStream,ErrorChar,i
SaveToFile=1
if trim(fullpath)="" or right(fullpath,1)="/" then exit function
set oFileStream=CreateObject("Adodb.Stream")
oFileStream.Type=1
oFileStream.Mode=3
oFileStream.Open
oUpFileStream.position=FileStart
oUpFileStream.copyto oFileStream,FileSize
oFileStream.SaveToFile FullPath,2
oFileStream.Close
set oFileStream=nothing
SaveToFile=0
end function
End Class
%>

 

上传表单所在的页面

<html>
<head>
<title>文件上传</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<style type="text/css">
<!--
td {  font-size: 9pt}
a {  color: #000000; text-decoration: none}
a:hover {  text-decoration: underline}
.tx {  height: 16px; width: 30px; border-color: black black #000000; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 1px; border-left-width: 0px; font-size: 9pt; background-color: #eeeeee; color: #0000FF}
.bt {  font-size: 9pt; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; height: 16px; width: 80px; background-color: #eeeeee; cursor: hand}
.tx1 { height: 20px; width: 30px; font-size: 9pt; border: 1px solid; border-color: black black #000000; color: #0000FF}
-->
</style>
</head>
<body bgcolor="#FFFFFF" text="#000000">
<form name="form1" method="post" action="upfile.asp" enctype="multipart/form-data">
<br>
<input type="hidden" name="act" value="upload">
<br>
<table width="71%" border="1" cellspacing="0" cellpadding="5" align="center" bordercolordark="#CCCCCC" bordercolorlight="#000000">
<tr bgcolor="#CCCCCC">
      <td height="22" align="left" valign="middle" bgcolor="#CCCCCC">http文件上传</td>
</tr>
<tr align="left" valign="middle" bgcolor="#eeeeee">
<td bgcolor="#eeeeee" height="92">
<script language="javascript">
   function setid()
   {
   str="<br>";
   if(!window.form1.upcount.value)
    window.form1.upcount.value=1;
    for(i=1;i<=window.form1.upcount.value;i++)
      str+="文件"+i+":<input type="file" name="file"+i+"" style="width:400" class="tx1"><br><br>";
   window.upid.innerHTML=str+"<br>";
   }
</script>
        <li> 需要上传的个数
          <input type="text" name="upcount" class="tx" value="1">
          <input type="button" name="Button" class="bt" onclick="setid();" value="· 设定 ·">
        </li>
        <br>
        <br>
        <li>上传到:
          <input type="text" name="filepath" class="tx" style="width:350" value="upload/">
        </li>
      </td>
    </tr>
    <tr align="center" valign="middle">
      <td align="left" id="upid" height="122"> 文件1:
        <input type="file" name="file1" style="width:400" class="tx1" value="">
      </td>
    </tr>
    <tr align="center" valign="middle" bgcolor="#eeeeee">
      <td bgcolor="#eeeeee" height="24">
        <input type="submit" name="Submit" value="· 提交 ·" class="bt">
        <input type="reset" name="Submit2" value="· 重执 ·" class="bt">
      </td>
    </tr>
  </table>
</form>
</body>
</html>
<script language="javascript">
setid();
</script>

 

处理表单的页面

<!--#include FILE="upclass.inc"-->
<html>
<head>
<title>文件上传结果</title>
</head>
<body>
<br><hr size=1 noshadow width=300 align=left><br><br>
<%
dim upload,file,formName,formPath,iCount
set upload=new upload_file ""建立上传对象
if upload.form("filepath")="" then   ""得到上传目录
HtmEnd "请输入要上传至的目录!"
set upload=nothing
response.end
else
formPath=upload.form("filepath")
""在目录后加(/)
if right(formPath,1)<>"/" then formPath=formPath&"/"
end if
iCount=0
for each formName in upload.file ""列出所有上传了的文件
set file=upload.file(formName)  ""生成一个文件对象
"开始检查后缀
fileExt=file.fileExt
if LCase(fileExt)<>".gif" and LCase(fileExt)<>".jpg" and LCase(fileExt)<>".bmp" and LCase(fileExt)<>".png" then
response.write "<br>"&" 文件不是图片[<a href=""javascript:history.back();"">返回</a>]<br><p>允许的格式为:gif;jpg;bmp;png!</p><p>Nice_Idea.</p></body></html>"
response.end 
end if
"后缀检查结束
if file.FileSize>0 then   ""如果 FileSize > 0 说明有文件数据
 "更改文件名,防止文件覆盖
filename=year(now())&month(now())&day(now())&hour(now())&minute(now())&second(now())&"niceidea"&file.fileExt
file.SaveToFile Server.mappath(formPath&filename)   ""保存文件
response.write file.FilePath&file.FileName&" ("&file.FileSize&"KB"&") => "&formPath&File.FileName&" 成功!<br>"
iCount=iCount+1
end if
set file=nothing
next
set upload=nothing  ""删除此对象
Htmend iCount&" 个文件上传结束!"
sub HtmEnd(Msg)
set upload=nothing
response.write "<br>"&Msg&" [<a href=""javascript:history.back();"">返回</a>]</body></html>"
response.end
end sub
%>
</body>
</html>

这个是我用的第一个无组件上传了。我在iis5.0下面测试的时候,可以实现文件和表单数据的分别提交。


相关信息:

fso操作文件 实例函数
利用FSO取得BMP,JPG,PNG,GIF文件信息
FileSystemObject对象成员概要
fso使用:drive 读取驱动器信息
fso:folder 文件系统
fso:file 文件对象
scripting.TextStream对象-fso读写文本
ASP实现结构化列举并查看某路径下所有文件

 

中国ASP技术 ASP.ORG.CN 版权所有 2004-2008