Page is a not externally linkable
picassoxw - 2:20 pm on Apr 16, 2006 (gmt 0)
<% '---------------------------------------------------------------------- Private Sub Class_Terminate '取得文件路径 '取得文件名 '取得扩展名 '文件属性类 '保存文件方法 the Form <form name="form" method="post" action="upfile.asp" enctype="multipart/form-data" onsubmit="return check();"> upfile.asp <!--#include file="conn.asp"--> These works well,for small files
upclass.inc
'--------------------------------------------------------------------
'File:c:\myfile\doc.txt
'FileName "doc.txt"
'FileSize 1210 (kb)
'FileType "text/plain"
'FileExt ".txt"
'FilePath "c:\myfile"
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
'清除变量及对像
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
%>
<input type="hidden" name="act" value="upload">
<input type="hidden" name="upcount" value="1">
<input type="hidden" name="filepath" value="images/">
name:<input type="text" name="picname" id="picname"><br/>
<br/>
<textarea cols="12" rows="8" name="description"></textarea>
<br/>
File:<br/>
<input type="file" name="file1" value="" width="12px" size="12" id="file1"><br/>
<input type="submit" name="Submit" value=" submit" > <input type="reset" value=" reset" >
</form>
<!--#include FILE="upclass.inc"-->
<html>
<head>
<title></title>
<style type="text/css">
body{font-size:12px;LINE-HEIGHT: 1.5em}
a { color: #000000; text-decoration: underline}
a:hover { text-decoration: underline}
</style>
</head>
<body bgcolor="#D7D7C3">
<br>图片上传结果<hr size=1 noshadow width=150 align=left><br>
<%
dim upload,file,formName,formPath,iCount,filename
set upload=new upload_file ''建立上传对象
if upload.form("picname")="" 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>A_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())&"nkplant"&fileExt
file.Savetofile Server.mappath(formPath&filename) ''保存文件
response.write file.FilePath&file.FileName&" ("&file.FileSize&"kb)"&" 上传成功!<br>"
iCount=iCount+1
end if
'开始写入数据库
set rs2=server.createobject("adodb.recordset")
insertsql="select * from views"
rs2.open insertsql,db,1,3
rs2.addnew
rs2("url")=filename
rs2("picname")=trim(upload.form("picname"))
rs2("description")=trim(upload.form("description"))
rs2.update
set rs2=nothing
db.close
'写入数据库结束
set file=nothing
next
set upload=nothing ''删除此对象
Htmend "文件上传成功!"
sub HtmEnd(Msg)
set upload=nothing
response.write "<br>"&Msg&" <br/>[<a href=""javascript:history.back();"">返回</a>]<br><p>感谢您的参与,上传成功的图片通过审核后将在本站展出。A_Nice_Idea</p></body></html>"
response.end
end sub
%>
</body>
</html>
the remarks are Chinese