asp实现上传功能的源码演示(简单易懂)
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
asp无组件上传图片的源码分析及下载
要实现图片的上传功能其实非常简单,只需要四个文件与一个文件夹即可实现。
第一个文件:Upload.asp 这个文件的源代码如下:
<form method="POST" name="myform" target="_self">
<input type="text" id="DefaultPic Url">
<iframe style="top:2px" ID="UploadFiles" src="upload_Photo.asp?PhotoUrlID=1" frameborder=0 scrolling=no width="300" height="325"></iframe>
</form>
说明:这个文件将引用一个名为upload_Photo.asp的文件也就是下面要讲的第二个文件作为框架,同时这里传递一个PhotoUrlID参数给pload_Photo.asp这个文件的form1表单中的名为PhotoUrlID的隐藏域名,其值为1
第二个文件:Upload_Photo.asp,这个文件的源代码如下:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<style type="text/css">
<!--
BODY{
BACKGROUND-COLOR: #E1F4EE;
font-size:9pt
}
.tx1 { height: 20px;font-size: 9pt; border: 1px solid; border-color: #000000; color: #0000FF}
-->
</style>
<SCRIPT language=javascript>
function check()
{
var strFileName=document.form1.FileName.value;
if (strFileName=="")
{
alert("请选择要上传的文件");
document.form1.FileName.focus();
return false;
}
}
</SCRIPT>
</head>
<body leftmargin="0" topmargin="0">
<form action="Upfile_Photo.asp" method="post" name="form1" onSubmit="return check()" enctype="multipart/form-data">
<input name="FileName" type="FILE" class="tx1" size="30">
<input type="submit" name="Submit" value="上传" style="border:1px double rgb(88,88,88);font:9pt">
<input name="PhotoUrlID" type="hidden" id="PhotoUrlID" value="<%=Clng(trim(request("PhotoUrlID")))%>">
</form>
</body>
</html>
说明:这个是作为第一个文件Upload.asp的框架文件,注意这里的FileName的文本框是用于接受上传成功之后图片的地址
第三个文件:Upfile_Photo.asp,其源代码如下:
<!--#include file="upfile_class.asp"-->
<%
const upload_type=0 '上传方法:0=无惧无组件上传类,1=FSO上传2=lyfupload,3=aspupload,4=chinaaspupload
dim upload,oFile,formName,SavePath,filename,fileExt,oFileSize
dim EnableUpload
dim arrUpFileType
dim ranNum
dim msg,FoundErr
dim PhotoUrlID
msg=""
FoundErr=false
EnableUpload=false
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<style type="text/css">
<!--
BODY {
BACKGROUND-COLOR: #E1F4EE;
font-size:9pt
}
.tx1 {
height: 20px;
font-size: 9pt;
border: 1px solid;
border-color: #000000;
color: #0000FF
}
-->
</style>
</head>
<body leftmargin="2" topmargin="5" marginwidth="0" marginheight="0" >
<%
if EnableUploadFile="No" then
response.write "系统未开放文件上传功能"
else
select case upload_type
case 0
call upload_0() '使用化境无组件上传类
case else
'response.write "本系统未开放插件功能"
'response.end
end select
end if
%>
</body>
</html>
<%
sub upload_0() '使用化境无组件上传类
set upload=new upfile_class ''建立上传对象
upload.GetData(104857600) '取得上传数据,限制最大上传100M
if upload.err > 0 then '如果出错
select case upload.err
case 1
response.write "请先选择你要上传的文件!"
case 2
response.write "你上传的文件总大小超出了最大限制(200k)"
end select
response.end
end if
PhotoUrlID=Clng(trim(upload.form("PhotoUrlID")))
if PhotoUrlID>0 then
SavePath = "UploadFiles" '存放上传文件的目录
else
SavePath = "UploadFiles" '存放上传文件的目录
end if
if right(SavePath,1)<>"/" then SavePath=SavePath&"/" '在目录后加(/)
for each formName in upload.file '列出所有上传了的文件
set ofile=upload.file(formName) '生成一个文件对象
oFileSize=ofile.filesize
if oFileSize<100 then
msg="请先选择你要上传的文件!"
FoundErr=True
else
select case PhotoUrlID
case 0
if oFileSize>(MaxFileSize*1024) then
msg="文件大小超过了限制,最大只能上传" & CStr(MaxFileSize) & "K的文件!"
FoundErr=true
end if
case 1
if oFileSize>(10000*1024) then
msg="文件大小超过了限制,最大只能上传10M的文件!"
FoundErr=true
end if
end select
end if
fileExt=lcase(ofile.FileExt)
arrUpFileType=split("gif|jpg|bmp|png|swf|doc|rar|xls","|")'如需添加其它类型的文档请在这里添加文档后缀名
for i=0 to ubound(arrUpFileType)
if fileEXT=trim(arrUpFileType(i)) then
EnableUpload=true
exit for
end if
next
if fileEXT="asp" or fileEXT="asa" or fileEXT="aspx" then
EnableUpload=false
end if
if EnableUpload=false then
msg="这种文件类型不允许上传!\n\n只允许上传这几种文件类型:" & "gif|jpg|bmp|png|swf|doc|rar|xls"
FoundErr=true
end if
strJS="<SCRIPT language=javascript>" & vbcrlf
if FoundErr<>true then
randomize
ranNum=int(900*rnd)+100
filename=SavePath&year(now)&month(now)&day(now)&hour(now)&minute(now)&second (now)&ranNum&"."&fileExt
ofile.SaveToFile Server.mappath(FileName) '保存文件
response.write "文件上传成功!文件大小为:" & cstr(round(oFileSize/1024)) & "K"
select case PhotoUrlID
case 0
strJS=strJS & "parent.document.myform.PhotoSize1.value='" & cstr(round(oFileSize/1024)) & "';" & vbcrlf
case 1
strJS=strJS & "parent.document.myform.DefaultPicUrl.value='" & fileName & "';" & vbcrlf
end select
else
strJS=strJS & "alert('" & msg & "');" & vbcrlf
strJS=strJS & "history.go(-1);" & vbcrlf
end if
strJS=strJS & "</script>" & vbcrlf
response.write strJS
set file=nothing
next
set upload=nothing
end sub
%>
第四个文件upfile_class.asp,源代码如下
<%
Dim oUpFileStream
'----------------------------------------------------------------------
'文件上传类
Class UpFile_Class
Dim Form,File,V ersion,Err
Private Sub Class_Initialize
V ersion = "无惧上传类V ersion V1.2"
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 (MaxSize)
'定义变量
Dim RequestBinData,sSpace,bCrLf,sInfo,iInfoStart,iInfoEnd,tStream,iStart,oFileInfo Dim iFileSize,sFilePath,sFileType,sFormV alue,sFileName
Dim iFindStart,iFindEnd
Dim iFormStart,iFormEnd,sFormName
'代码开始
If Request.TotalBytes < 1 Then '如果没有数据上传
Err = 1
Exit Sub
End If
If MaxSize > 0 Then '如果限制大小
If Request.TotalBytes > MaxSize Then
Err = 2 '如果上传的数据超出限制
Exit Sub
End If
End If
Set Form = Server.CreateObject ("Scripting.Dictionary")
pareMode = 1
Set File = Server.CreateObject ("Scripting.Dictionary")
pareMode = 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
RequestBinData = oUpFileStream.Read
iFormEnd = oUpFileStream.Size
bCrLf = ChrB (13) & ChrB (10)
'取得每个项目之间的分隔符
sSpace = MidB (RequestBinData,1, InStrB (1,RequestBinData,bCrLf)-1)
iStart = LenB (sSpace)
iFormStart = iStart+2
'分解项目
Do
iInfoEnd = InStrB (iFormStart,RequestBinData,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,RequestBinData,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, "\")) 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"
sFormV alue = tStream.ReadText
If Form.Exists (sFormName) Then
Form (sFormName) = Form (sFormName) & ", " & sFormV alue
else
form.Add sFormName,sFormV alue
End If
End If
tStream.Close
iFormStart = iFormStart+iStart+2
'如果到文件尾了就退出
Loop Until (iFormStart+2) >= iFormEnd
RequestBinData = ""
Set tStream = Nothing
End Sub
End Class
'------------------------------------------------------------------------------
'文件属性类
Class FileInfo_Class
Dim FormName,FileName,FilePath,FileSize,FileType,FileStart,FileExt
'保存文件方法
Public Function SaveToFile (Path)
if lcase((right(Path,3))<>lcase(FileExt)) then '经典的上传漏洞^_^
response.Write ("<script language=javascript>alert('警告:不允许上传这种文件!');</script>")
response.end
end if
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 FileData
oUpFileStream.Position = FileStart
FileData = oUpFileStream.Read (FileSize)
End Function
End Class
%>
除了上面四个文件夹之外还需要一个名为UploadFiles的文件夹,注意以上的四个文件与文件夹都是放在网站根目录下,也就是同极目录下的,如果目录不同级,请修改路径,同时本程序不仅可以包括jpg,png,gif等类型的图片,还可以上传word、EXECL、记事本、RAR压缩包等,当然上传对大小是有限的,只限定在200K以内,如果超过200K将会出错.。