28,406
社区成员
发帖
与我相关
我的任务
分享
Dim oUpFileStream
Class UpFile_Class
Dim Form,File,Version,Err
Private Sub Class_Initialize
Version = "无组件上传类 Asp无组件文件上传到租用服务器空间的实例 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 GetData (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
End If
If int(RetSize) > 0 Then
If int(Request.TotalBytes) > int(RetSize) Then
Response.write "图片大小超过了限制 "&RetSize/1024&" K [ <a href=# onclick=history.go(-1)>重新上传</a> ]"
Err = 2
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) & ", " & SafeValue(sFormValue)
else
form.Add sFormName,SafeValue(sFormValue)
End If
End If
tStream.Close
iFormStart = iFormStart+iStart+2
'如果到文件尾了就退出
Loop Until (iFormStart+2) = iFormEnd
RequestBinDate = ""
Set tStream = Nothing
End Sub
End Class
Function SafeValue(str)
if str <> "" then
SafeValue = Replace(str,"'","''")
SafeValue = Replace(str,"""","“")
end if
End Function
'文件属性类
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
if Err.Number<>0 then
SaveToFile=err.number&"**"&Err.descripton
else
SaveToFile="ok"
end if
End Function
'取得文件数据
Public Function FileDate
oUpFileStream.Position = FileStart
FileDate = oUpFileStream.Read (FileSize)
End Function
End Class
dim upload,file,formName,SavePath,filename,fileExt
dim ranNum
'===========无组件上传(upload_0)====================
Function UpFile(SavePath,SaveFolder,SaveName)
set upload=new UpFile_Class '建立上传对象
upload.GetData (500*1024) '取得上传数据,此处即为500 K
Response.write upload.err
if upload.err > 0 then
select case upload.err
case 1
Response.Write "请先选择你要上传的文件 [ <a href=# onclick=history.go(-1)>重新上传</a> ]"
case 2
Response.Write "图片大小超过了限制 500 K [ <a href=# onclick=history.go(-1)>重新上传</a> ]"
end select
exit Function
else
'在目录后加(/)
if right(SavePath,1)<>"/" then
SavePath=SavePath&"/"
end if
If Right(SaveFolder,1) <> "/" then
SaveFolder = SaveFolder & "/"
End If
If Left(SaveFolder,1) = "/" then
SaveFolder = Right(SaveFolder,Len(SaveFolder)-1)
End If
'创建文件夹
call CheckDir(SavePath&SaveFolder)
for each formName in upload.file '列出所有上传了的文件
set file=upload.file(formName) '生成一个文件对象
if file.filesize<1 then
response.write "请先选择你要上传的图片 [ <a href=# onclick=history.go(-1)>重新上传</a> ]"
response.end
end if
fileExt=lcase(file.FileExt)
if CheckFileExt(fileEXT)=false then
response.write "文件格式不正确 [ <a href=# onclick=history.go(-1)>重新上传</a> ]"
response.end
end if
If SaveName <> "" then
ToName = SaveName & "." & fileExt
Else
'randomize
ranNum=int(90000*rnd)+10000
ToName = year(now)&month(now)&day(now)&hour(now)&minute(now)&second(now)&ranNum & "." & fileExt
End If
' If Instr(ToName,".") = 0 Then
' ToName = ToName & "." & fileExt
' End If
filename = SavePath & SaveFolder & ToName
if file.FileSize>0 then '如果 FileSize > 0 说明有文件数据
result=file.SaveToFile(Server.mappath(filename)) '保存文件
if result="ok" then
UpFile = SaveFolder & ToName
end if
end if
set file=nothing
next
set upload=nothing
end if
end Function
'检查是否存在目录,生成目录
Private Function CheckDir(FolderPath)
Dim fs0
Folder=Server.MapPath(FolderPath)'&"\"&folderpath
Set fso= CreateObject("Scripting.FileSystemObject")
If fso.FolderExists(Folder) then
Else
Call MakeFolder(FolderPath)
End if
Set fso= nothing
End Function
'建立文件夹
Private Sub MakeFolder(FolderPath)
Dim fs0
FolderPath=Server.MapPath(FolderPath)'&"\"&folderpath
Set fso= CreateObject("Scripting.FileSystemObject")
fs0 = fso.CreateFolder(FolderPath)
Set fso= nothing
End Sub
'删除文件
Function DeleteFile(FolderPath)
Set fso = CreateObject("Scripting.FileSystemObject")
if fso.FileExists(server.MapPath(FolderPath)) then
fso.DeleteFile server.MapPath(FolderPath)
End If
Set fso = nothing
End Function
'判断文件类型是否合格
Private Function CheckFileExt (fileEXT)
dim Forumupload
Forumupload="gif,jpg,bmp,jpeg"
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
Set up = new UpFile_Class
up.form("text") '获得非file类型
up.file("file") '获得FILE类型
<!--#include file="upload.asp"-->
<%
if action = "up" then
Set up = new UpFile_Class
up.GetData (SaveSize)
SaveSize = 2048000'限制上传字节大小
title = up.form("title") '获得非file类型
'上传文件
Size = up.file("file").FileSize'上传文件的大小
Ext = up.file("file").FileExt'上传文件的类型
up.file("file").SaveToFile(Server.MapPath("保存的图径"))
rs.open "select * from file",conn,1,3
rs.addnew
rs("title") = title
rs.update
rs.close
end if
%>
<form action="?action=up" method="post" enctype="multipart/form-data" name="form1" id="form1" >
<input type="text" name="title" />
<input type="file" name="file" />
</form>