<%
Response.Charset="gb2312"
Response.Buffer = True 
Response.ExpiresAbsolute = Now() - 1 
Response.Expires = 0 
Response.CacheControl = "no-cache" 
Response.AddHeader "Pragma", "No-Cache" 

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 0.96"
  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, ".")+1)
    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

Function CheckFileType(files)
'//判断图片是否安全
const adTypeBinary=1
dim jpg(1):jpg(0)=CByte(&HFF):jpg(1)=CByte(&HD8)
dim bmp(1):bmp(0)=CByte(&H42):bmp(1)=CByte(&H4D)
dim png(3):png(0)=CByte(&H89):png(1)=CByte(&H50):png(2)=CByte(&H4E):png(3)=CByte(&H47)
dim gif(5):gif(0)=CByte(&H47):gif(1)=CByte(&H49):gif(2)=CByte(&H46):gif(3)=CByte(&H39):gif(4)=CByte(&H38):gif(5)=CByte(&H61)

on error resume next
CheckFileType=false
dim fstream,fileExt,stamp,i
fileExt=mid(files,InStrRev(files,".")+1)
set fstream=Server.createobject("ADODB.Stream")
fstream.Open
fstream.Type=adTypeBinary
fstream.LoadFromFile files
fstream.position=0
select case fileExt
case "jpg","jpeg"
  stamp=fstream.read(2)
  for i=0 to 1
   if ascB(MidB(stamp,i+1,1))=jpg(i) then CheckFileType=true else CheckFileType=false
  next
case "gif"
   stamp=fstream.read(6)
   for i=0 to 5
   if ascB(MidB(stamp,i+1,1))=gif(i) then CheckFileType=true else CheckFileType=false
   next
case "png"
   stamp=fstream.read(4)
   for i=0 to 3
   if ascB(MidB(stamp,i+1,1))=png(i) then CheckFileType=true else CheckFileType=false
   next
case "bmp"
stamp=fstream.read(2)
for i=0 to 1
if ascB(MidB(stamp,i+1,1))=bmp(i) then CheckFileType=true else CheckFileType=false
next
end select
fstream.Close
set fseteam=nothing
if err.number<>0 then CheckFileType=false
End Function
'//判断上传目录是否存在，然后建立
Function fileold(fold)
If fold<> "" Then 
set fso = server.CreateObject("scripting.filesystemobject") 
  if not fso.folderexists(Server.MapPath(fold)) then 
     fso.createfolder(server.MapPath(fold)) 
  end if 
End if 
End function

Function up_err(ss)
  if ss=1 or ss=4 then
    response.Write("{""error"":1,""message"":"" 不支持上传的文件类型！ ""}")			
  else
	response.Write("<script>alert('不支持上传的文件类型');history.back();</script>")
  end if
  response.End()
End function
'先接收GET传输
pid=request.QueryString("pid")
act=request.QueryString("act")
lei=request.QueryString("lei")
fid=request.QueryString("fid")
'定义上传类
Set upload=new upload_file
'当GET接收为空时接收FORM
if act="" then act=trim(upload.form("act"))
if pid="" then pid=trim(upload.form("pid"))
if lei="" then lei=trim(upload.form("lei"))
if fid="" then fid=trim(upload.form("fid"))
lei=cint(lei)
if act="uploadfile" then
%>
<!--#include file="upfile.asp"-->
<!--#include file="jpeg.asp"-->
<!--#include file="config.asp"-->
<%
i=0
for each formName in upload.File
set file=upload.File(formName)
'//上传类型验证
fileExt=lcase(file.FileExt)	'得到的文件扩展名不含有.
if file.filesize<10 then
   if lei=1 or lei=4 then
   response.Write("{""error"":1,""message"":"" 请选择你要上传的文件！""}")
   else
   response.Write("<script>alert('请选择你要上传的文件！');history.back();</script>")
   end if
   response.End()
end if
'//大小及类型验证
filelx="|"&filetp&"|" '可支持上传的文件类型
if inStr(filelx,"|"&fileExt&"|")>0 then'后台允许上传的文件类型
   if file.filesize>(1024*filesz) then
      if lei=1 then
	  response.Write("{""error"":1,""message"":"" 最大只能上传"&filesz&"KB的文件！ ""}")
	  else
	  response.Write("<script>alert('最大只能上传"&filesz&"KB的文件');history.back();</script>")
	  end if
	  response.End()
   end if
   select case pid'允许上传文件类型
      case "pic"
	      if instr("|jpg|bmp|gif|png|","|"&fileExt&"|")=0 then call up_err(lei)
      case "flash"
	      if instr("|swf|","|"&fileExt&"|")=0 then call up_err(lei)
	  case "media"
	      if instr("|rm|rmvb|mp4|wmv|mpg|flv|avi|mpeg|mov|mkv|asf|mp3|wav|wma|","|"&fileExt&"|")=0 then call up_err(lei)
	  case "file"
	      if instr("|zip|rar|txt|doc|xls|pdf|docx|ppt|","|"&fileExt&"|")=0 then call up_err(lei)
	  case else
	      call up_err(lei)
   end select 
else   
   if lei=1 or lei=4 then
	response.Write("{""error"":1,""message"":"" 不支持的文件类型，检查上传设置！ ""}")
   else
    response.Write("<script>alert('不支持的文件类型，检查上传设置！');history.back();</script>")
   end if
   response.End()
end if
'//重命名
 randomize
 ranNum=int(90000*rnd)+10000
 file_f="../"&filepath&"/"
 call fileold(file_f)'验证上传目录是否存在
 file_n=year(now)&month(now)&day(now)&hour(now)&minute(now)&second(now)&ranNum&"."&fileExt
 file_ss=replace(file_f,"../","")&file_n  '添加的内容的地址
 filename=file_f&file_n'上传的地址 
'//保存文件
if file.FileSize>0 then file.SaveToFile Server.mappath(FileName)
'//判断图片是否安全
if pid="pic" then
   if CheckFileType(Server.MapPath(filename))=false then
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set ficn = fso.GetFile(Server.mappath(filename))
    ficn.delete
    set ficn=nothing
    set fso=nothing
    call up_err(lei)
    response.end()
   end if
end if
'//加水印
if Cw_jpeg_t0=1 then call Cw_jpeg(filename)
'//返回路径
'//编辑器图片上传返回
if lei=1 then
response.Write("<script>parent.document.getElementById('"&pid&"').value='"&file_ss&"';parent.addItem('"&file_ss&"');</script>")
response.Write("{""error"":0,""url"":"""&cf_root&file_ss&"""}") '返回正确
'response.Write("{""error"":1,""message"":"" 最大只能上传 "&filesz&"KB 的文件！ ""}") 返回错误
end if
'//直接添加到input
if lei=2 then
response.Write("<script>parent.document.getElementById('"&pid&"').value='"&file_ss&"';location.href='upload.asp?lei=2&pid="&pid&"';</script>")
end if
'//模拟窗口返回,用以模板选择调用
if lei=3 then
response.Write("<script>parent.document.getElementById('"&pid&"').value='"&file_ss&"'; parent.document.getElementById('ifr').style.display='none'; parent.document.getElementById('"&pid&"').style.display='block';</script>")
end if
'//编辑器其它上传
if lei=4 then
response.Write("{""error"":0,""url"":"""&cf_root&file_ss&"""}")
end if
'//上传图片用
if lei=5 then'
  response.Write("<script>parent.document.getElementById('"&fid&"').style.display='none';parent.document.getElementById('"&pid&"').style.display='inline';parent.document.getElementById('"&pid&"').value='"&file_ss&"';</script>")
end if
set file=nothing
next
set upload=nothing
else
%>
<script type="text/javascript">
function checkup()
{
 if (document.form1.file1.value=="")
 {
    alert('请选择你要上传的图片！');
	return false
  }
  return true
}
</script>
<style type="text/css">
body{margin:0; padding:0}
</style>
<form action="" method="post" enctype="multipart/form-data" name="form1" onsubmit="return checkup()" >
      <input name="file1" type="file"  class="tx1" id="file1" value="" size="25">
      &nbsp;
	  <input type="submit" name="Submit" value="上传图片"   >	  
	  <input name="lei" type="hidden" id="lei" value="<%=request.QueryString("lei")%>">
      <input name="pid" type="hidden" id="pid" value="<%=request.QueryString("pid")%>">
<input type="hidden" name="act" value="uploadfile">
<input name="fid" type="hidden" id="fid" value="<%=request.QueryString("fid")%>" />
</form>
<%end if%>