主题: 看了之前的asp版的代码 很少 并且我试了都不好用,自己改了一个 奉献一下
作者: 超超, 发布日期: 2009-11-21 15:42:51, 浏览数: 4804
两个文件  upload.asp  和 upfile_class.asp

upload.asp 文件代码

<!--#include file="upfile_class.asp"-->
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<%
server.scripttimeout=3000 '指定更长时间,以防网速过慢造成服务器脚本超时!
dim fpath,EnableUpload
dim upload,file,formName,formPath,filename,fileExt,id,imgTitle,imgWidth,imgHeight,imgBorder,ming,txt
dim ranNum
dim ok
call upload_0()
'===========无组件上传(upload_0)====================
sub upload_0()
set upload=new UpFile_Class ''建立上传对象
upload.GetDate (10*1024*1024) '取得上传数据,是大数10M
id=replace(trim(upload.form("id")),"'","")
imgTitle=replace(trim(upload.form("imgTitle")),"'","")
imgWidth=replace(trim(upload.form("imgWidth")),"'","")
imgHeight=replace(trim(upload.form("imgHeight")),"'","")
imgBorder=replace(trim(upload.form("imgBorder")),"'","")
fpath=trim(upload.form("imgFile"))
if upload.err > 0 then
select case upload.err
case 1
ok=0
txt="请先选择您要上传的文件!"
case 2
ok=0
txt="文件大小超过了限制 10M!"
end select
exit sub
else
formpath="../attached/" '存放上传文件的目录,在目录后加(/)
savefilename="/edit/attached/"
for each formName in upload.file ''列出所有上传了的文件
set file=upload.file(formName) ''生成一个文件对象
if file.filesize<100 then
ok=0
txt="请先选择您要上传的文件!"
end if
fileExt=lcase(file.FileExt)
if CheckFileExt(fileEXT)=false then
ok=0
txt="文件格式不正确!"
end if
randomize
ranNum=int(90000*rnd)+10000
formPath=""&formPath
ming=year(now)&month(now)&day(now)&hour(now)&minute(now)&second(now)&ranNum&"."&fileExt
filename=formPath&ming
savefilename=savefilename&ming
if file.FileSize>0 then '如果 FileSize > 0 说明有文件数据
file.SaveToFile Server.mappath(filename) '保存文件
ok=1
end if
set file=nothing
next
set upload=nothing
end if
if ok=0 then
txt="<script language=javascript>alert('"&txt&"');javascript:history.go(-1)</SCRIPT>"
else
txt="<script language='Javascript'>parent.KE.plugin[""image""].insert('"&id&"', '"&savefilename&"','"&imgTitle&"','"&imgWidth&"','"&imgHeight&"','"&imgBorder&"');</script>"
end if
Response.Write txt
end sub
'判断文件类型是否合格
Private Function CheckFileExt (fileEXT)
dim Forumupload,i
Forumupload="jpeg|bmp|png|gif|jpg" '允放上传的文件格式
Forumupload=split(Forumupload,"|")
for i=0 to ubound(Forumupload)
if lcase(fileEXT)=lcase(trim(Forumupload(i))) then
CheckFileExt=true
exit Function
else
CheckFileExt=false
end if
next
End Function
%>


upfile_class.asp文件代码

<%

Dim oUpFileStream

Class UpFile_Class

Dim Form,File,Version,Err 

Private Sub Class_Initialize
Version = "无惧上传类 Version V1.0"
Err = -1
End Sub

Private Sub Class_Terminate 
'清除变量及对像
If Err < 0 Then
Form.RemoveAll
Set Form = Nothing
File.RemoveAll
Set File = Nothing
oUpFileStream.Close
Set oUpFileStream = Nothing
End If
End Sub

Public Sub GetDate (RetSize)
'定义变量
Dim RequestBinDate,sSpace,bCrLf,sInfo,iInfoStart,iInfoEnd,tStream,iStart,oFileInfo
Dim iFileSize,sFilePath,sFileType,sFormValue,sFileName
Dim iFindStart,iFindEnd
Dim iFormStart,iFormEnd,sFormName
'代码开始
If Request.TotalBytes < 1 Then
Err = 1
Exit Sub
End If
If RetSize > 0 Then 
If Request.TotalBytes > RetSize Then
Err = 2
Exit Sub
End If
End If
Set Form = Server.CreateObject ("Scripting.Dictionary")
Form.CompareMode = 1
Set File = Server.CreateObject ("Scripting.Dictionary")
File.CompareMode = 1
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)
'取得每个项目之间的分隔符
sSpace = MidB (RequestBinDate,1, InStrB (1,RequestBinDate,bCrLf)-1)
iStart = LenB (sSpace)
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,sSpace)-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_Class
'取得文件属性
iFindStart = InStr (iFindEnd,sInfo,"filename=""",1)+10
iFindEnd = InStr (iFindStart,sInfo,"""",1)
sFileName = Mid (sinfo,iFindStart,iFindEnd-iFindStart)
oFileInfo.FileName = Mid (sFileName,InStrRev (sFileName, "\")+1)
oFileInfo.FilePath = Left (sFileName,InStrRev (sFileName, "\")+1)
oFileInfo.FileExt = Mid (sFileName,InStrRev (sFileName, ".")+1)
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
If Form.Exists (sFormName) Then
Form (sFormName) = Form (sFormName) & ", " & sFormValue
else
form.Add sFormName,sFormValue
End If
End If
tStream.Close
iFormStart = iFormStart+iStart+2
'如果到文件尾了就退出
Loop Until (iFormStart+2) = iFormEnd 
RequestBinDate = ""
Set tStream = Nothing
End Sub
End Class

'文件属性类
Class FileInfo_Class
Dim FormName,FileName,FilePath,FileSize,FileType,FileStart,FileExt
'保存文件方法
Public Function SaveToFile (Path)
On Error Resume Next
Dim oFileStream
Set oFileStream = CreateObject ("ADODB.Stream")
oFileStream.Type = 1
oFileStream.Mode = 3
oFileStream.Open
oUpFileStream.Position = FileStart
oUpFileStream.CopyTo oFileStream,FileSize
oFileStream.SaveToFile Path,2
oFileStream.Close
Set oFileStream = Nothing 
End Function

'取得文件数据
Public Function FileDate
oUpFileStream.Position = FileStart
FileDate = oUpFileStream.Read (FileSize)
End Function
End Class
%>
作者: Roddy, 发布日期: 2009-11-21 15:52:50
感谢你的分享,移到【经验分享栏目】了。
回复
作者: 不懂就问, 发布日期: 2009-12-21 18:05:07

将IMAGE.HTML调用的上传改成这个无法上传!

回复
作者: huangyun, 发布日期: 2010-08-14 23:35:40
请问如何使用呢?
回复
发表新帖 发表回复