<%
if session("User")="" then
	response.write"<SCRIPT language=JavaScript>alert('您尚未登录！');"
	response.write"location.href=""sd_admin_login.asp"";</SCRIPT>"
end if
%>
<%Response.Buffer=True
Server.ScriptTimeOut=9999999
On Error Resume Next%><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB2312" />
<meta http-equiv="Content-Language" content="zh-cn" />
<meta content="all" name="robots" /> 
<style type="text/css">body,form{margin:0px;padding:0px;}body,input{font-size:12px;}</style>
<meta name="author" content="雨过天晴工作室软件" />
<meta name="description" content="雨过天晴工作室" /> 
<meta name="keywords" content="雨过天晴工作室" />
<title>商道软件_文件编辑器</title>
</head>
<body id="body"><table width="100%" height="100%" border="0" cellspacing="0" cellpadding="0"><tr><td>
<%sdFileNames="jpg,gif,png,swf"'允许扩展名
sdSavePath="../../upload"'保存路径
sdBigSize=2000000'自定义文件上传的最大限制

sdDFormName=trim(request("nf"))'返回值所在表单名称
sdDInputName=trim(request("ni"))'返回值所在文本框
sdDYNReName=trim(request("nr"))'是否默认重命名
sdDPath=trim(request("nt"))'指定保存文件夹
sdDPathQ=trim(request("nq"))'相对图片查看前台，上传文件夹的位置

if sdDPath<>"" then
If Right(sdDPath,1)<>"/" Then
sdSavePath=sdDPath&"/"'在目录后加(/)
else
sdSavePath=sdDPath
end if
else
If Right(sdSavePath,1)<>"/" Then sdSavePath=sdSavePath&"/"'在目录后加(/)
end if

sdYNPath(sdSavePath)'判断是否存在文件夹，不存在就创建

sdFileSize=Request.TotalBytes'取得客户端全部内容，读取所由表单传来的数据总数

If(sdFileSize>0) Then
Set sdUpStm=Server.CreateObject("ADODB.Stream")
sdUpStm.Type=1
sdUpStm.Open
sdUpStm.Write Request.BinaryRead(sdFileSize)'由该方法返回的无符号数的数组
sdUpStm.Position=0
sdFormDataSize=sdUpStm.Read'读取指定长度的二进制内容
sdCrlf=chrB(13)&chrB(10)'回车符号代码
sdFormStart=InStrB(sdFormDataSize,sdCrlf)'InStrB 返回一个字符串在另一个字符串中首次出现的字节位置

sdFormEnd=InStrB(sdFormStart+1,sdFormDataSize,sdCrlf)

Set sdFormStm=Server.Createobject("ADODB.Stream")
sdFormStm.Type=1
sdFormStm.Open
sdUpStm.Position=sdFormStart + 1
sdUpStm.CopyTo sdFormStm,sdFormEnd-sdFormStart-3
sdFormStm.Position=0
sdFormStm.Type=2
sdFormStm.CharSet="GB2312"
sdFormStmText=sdFormStm.Readtext'读取指定长度的文本
sdFormStm.Close

sdUpFileNAll=Mid(sdFormStmText,InstrRev(sdFormStmText,"\")+1,sdFormEnd)'InstrRev 函数 返回某字符串在另一个字符串中出现的从结尾计起的位置。

if(sdFileSize>sdBigSize) then
Response.Write"<span style=""color:red;"">大小超过"&sdBigSize&"B!</span> <a href="""&Request.ServerVariables("URL")&"?"&Request.ServerVariables("QUERY_STRING")&""">重新上传</a><br />"'判断文件大小是否超过最大限制
else If(sdCheckFileCZM(sdUpFileNAll,sdFileNames)) Then
sdUpFileCZM=mid(sdUpFileNAll,InstrRev(sdUpFileNAll,"."))'获取文件的扩展名
If Err Then
Response.Write"<span style=""color:red;"">文件上传出错!</span> <a href="""&Request.ServerVariables("URL")&"?"&Request.ServerVariables("QUERY_STRING")&""">重新上传</a><br />"
Err.Clear
Else
if sdDYNReName<>"ok" then
sdUpFileName=sdUpFileNAll
else
sdUpFileName=Year(now)&right("0"&Month(now),2)&right("0"&Day(now),2)&right("0"&Hour(now),2)&right("0"&Minute(now),2)&right("0"&Second(now),2)&right(replace(Request.ServerVariables("REMOTE_ADDR"),".",""),4)&sdUpFileCZM'按上传时间+IP最后四位数字重命名文件名
end if
sdSaveFile=Server.MapPath(sdSavePath & sdUpFileName)'保存文件

if sdDYNReName="ok" then
sdSaveFile=sdCheckFileRe(sdSaveFile)'判断文件是否存在
end if
k=Instrb(sdFormDataSize,sdCrlf&sdCrlf)+4
l=Instrb(k+1,sdFormDataSize,leftB(sdFormDataSize,sdFormStart-1))-k-2
sdFormStm.Type=1
sdFormStm.Open
sdUpStm.Position=k-1
sdUpStm.CopyTo sdFormStm,l
sdFormStm.SaveToFile sdSaveFile,2
sdSaveFileName=Mid(sdSaveFile,InstrRev(sdSaveFile,"\")+1)'保留文件最后的文件名

Response.write"<script language=javascript>parent.document."&sdDFormName&"."&sdDInputName&".value='"&sdDPathQ&sdSavePath&sdSaveFileName&"';</script>"
Response.write"<a href="""&sdDPathQ&sdSavePath&sdSaveFileName&""" target=""_blank"" title=""文件名称："&sdDPathQ&sdSavePath&sdSaveFileName&""&vbcrlf&"文件大小："&sdFileSize&"KB"&vbcrlf&"友情提示：点击打开预览上传图片""><span style=""color:red;text-decoration:none;"">"&left(sdSaveFileName,3)&"**"&sdUpFileCZM&"</span></a>成功，请点击<span style=""color:red;text-decoration:none;"">获取</span>按钮! <a href="""&Request.ServerVariables("URL")&"?"&Request.ServerVariables("QUERY_STRING")&""">重新上传</a><br />"
End If

Else
Response.write"<span style=""color:red;"">文件格式不正确!</span> <a href="""&Request.ServerVariables("URL")&"?"&Request.ServerVariables("QUERY_STRING")&""">重新上传文件</a><br />"
End If

end if

Else%><script language="Javascript">
function sdCheckFile()
{if(document.sdUpForm.sdUpFile.value=="") {
alert("请选择上传文件！")
document.sdUpForm.sdUpFile.focus()
return false
}return true}
</script>
<form action="" method="post" name="sdUpForm" onsubmit="return sdCheckFile()"enctype="multipart/form-data">
<input type="file" name="sdUpFile" size="15">
<input type="submit" value="上传">
</form>
<%End if
Set sdFormStm=Nothing
UploadStream.Close
Set UploadStream=Nothing

sdCheckFileCZM()

'判断文件类型是否合格
Function sdCheckFileCZM(FileName,sdFileNames)'文件名,允许上传文件类型
FileType=sdFileNames 
FileType=Split(FileType,",")'split 从字符串中返回数组
For i=0 To Ubound(FileType)
If LCase(Right(FileName,3))=LCase(FileType(i)) then
sdCheckFileCZM=True
Exit Function
Else
sdCheckFileCZM=False
End if
Next
End Function

'检查上传文件夹是否存在，不存在则创建文件夹
Function sdYNPath(FolderName)
sdFolderM=Server.Mappath(FolderName)
Set Fso=CreateObject("Scripting.FileSystemObject")
If Not Fso.FolderExists(sdFolderM) Then
Fso.CreateFolder(sdFolderM)
End If
Set Fso=Nothing
End Function

'检查文件是否存在，重命名存在文件
Function sdCheckFileRe(FileName)
Set Fso=Server.CreateObject("Scripting.FileSystemObject")
If Fso.FileExists(sdSaveFile) Then
i=1
msg=True
Do While msg
sdCheckFileRe=Replace(sdSaveFile,Right(sdSaveFile,4),"_" & i & Right(sdSaveFile,4))
If not Fso.FileExists(sdCheckFileRe) Then
msg=False
End If
i=i+1
Loop
Else
sdCheckFileRe=FileName
End If
Set Fso=Nothing
End Function%></td></tr></table>
</body>
</html>