虚拟主机Asp无组件文件上传到租用服务器空间的实例

发布时间:2006-06-18 22:09:24   浏览人数: 656883 次  
  关键字: 无组件上传 - 本站搜索 - Google搜索 - Baidu搜索 - Yahoo搜索 - sougo搜索
  关键字: Asp无组件上传 - 本站搜索 - Google搜索 - Baidu搜索 - Yahoo搜索 - sougo搜索

1.库文件(upload.inc.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 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
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
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
%>


2.处理用户提交后的页面(upload.asp)
<!--#include file="upload.inc.asp"-->
<html>
<head>
<title>文件上传</title>
</head>
<body topmargin="0" leftmargin="0">
<table width=100% border=0 cellspacing="0" cellpadding="0"><tr><td class=tablebody1 width=100% height=100% >
<%
dim upload,file,formName,formPath,filename,fileExt
dim ranNum
call UpFile()
'===========无组件上传(upload_0)====================
sub UpFile()
set upload=new UpFile_Class '建立上传对象
upload.GetData (500*1024) '取得上传数据,此处即为500 K

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 sub
else
formPath=upload.form("filepath") '文件保存目录,此目录必须为程序可读写
if formPath="" then
formPath="rwdata/"
end if
'在目录后加(/)
if right(formPath,1)<>"/" then
formPath=formPath&"/"
end if

for each formName in upload.file '列出所有上传了的文件
set file=upload.file(formName) '生成一个文件对象
if file.filesize<100 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

'randomize
ranNum=int(90000*rnd)+10000
filename=formPath&year(now)&month(now)&day(now)&hour(now)&minute(now)&second(now)&ranNum&"."&fileExt
if file.FileSize>0 then '如果 FileSize > 0 说明有文件数据
result=file.SaveToFile(Server.mappath(filename)) '保存文件
if result="ok" then
response.write formName&" upload OK, had saved to "&filename&"<br>"
else
response.write formName&" upload Fail,"&result&"<br>"
end if
end if
set file=nothing
next
set upload=nothing
end if
end sub

'判断文件类型是否合格
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
%>
</td></tr></table>
</body>
</html>



3.HTML 表单(upload.html)
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>asp 无组件上传</title>
</head>

<body>
请选择要上传的文件
<form action="upfile.asp" method="post" enctype="multipart/form-data" name="form1">
<input type="file" name="file">
<!--<br>
<input type="file" name="file">
<br>
<input type="file" name="file">
<br>-->
<input type="submit" name="Submit" value="提交">
</form>
</body>
</html>




上一篇:PHP主机租用 PHP发邮件的例程
下一篇:ASP.net虚拟主机 连接access数据库例程
关于我们   |   联系我们   |   代理合作   |  汇款方式   |   网站地图   |   合作伙伴   |   友情链接   |   诚聘英才   |   企业OA办公平台
  中华人民共和国增值电信业务经营许可证:备案号:桂ICP备11003756
旗下网站:联城网络 - 联城社区 - 易通智能建站 - 联城通  - 网达科技 cdn加速服务 举报不良网站
联城网络 © 网达科技 版权所有 本站程序界面、源代码受相关法律保护,未经授权,严禁使用
Copyright © 2000-2019 Netto.cn,Inc. All rights reserved 
本站法律顾问:知名事务所 益诺众承律师事务所   
联城网络诚意为: 缂傚倸鍊搁崐鎼佸磹閹间礁纾归柟闂寸绾惧綊鏌熼梻瀵割槮缁炬儳缍婇弻鐔兼⒒鐎靛壊妲紒鐐劤濠€杈╂閹烘柡鍋撻敐搴′壕缂佹墎鏅滅换娑橆啅椤旇崵鐩庨柛銉︽尦濮婅櫣鍖栭弴鐕佲偓鍡涙煛閸涱喚鐭掓い銏$懇瀵噣宕掑⿰鍜冪闯濠电偠鎻徊鑺ョ珶婵犲洤纾块柟鎵閻撱儲绻涢幋鐐垫噭缂佲偓鐎n喗鐓欐い鏃€鍎抽崢瀛橆殽閻愯尙绠荤€殿噮鍣i崺鈧い鎺嗗亾妞ゎ偄绻楅妵鎰板箳閹寸媭鍞甸梻浣告啞缁嬫帒岣垮▎鎴犵焼闁稿瞼鍋為悡鏇熺節闂堟稑顏╅柛鏃€绮庨惀顏堫敇閵娧呮殸闂佸疇顫夐崹鍧楀极瀹ュ绀嬫い鎰ㄥ墲濠⑩偓闂傚倷鑳舵灙闁挎洏鍎甸幃褍饪伴崼婵囩€銈嗘磵閸嬫挻顨ラ悙鏉戠瑨閾绘牠鏌嶈閸撶喖骞冮崸妤婃晩濠殿喗鍔掔花濠氭⒑閸濆嫮鈻夐柛瀣缁傛帒饪伴崨顖涳紡闂佺顫夐崝鏍兜閸洘鐓冪憸婊堝礈濞嗘垹涓嶇€广儱顦壕鍧楁⒑椤掆偓閸楁洟宕堕妸銉殼闂佸搫顦伴崹褰掓晬濞戙垺鈷戦柟绋挎捣缁犳挻銇勯敂鍨祮闁诡噯绻濆鎾閿涘嫬甯惧┑鐘灱濞夋盯鎮ч崱娑欏€舵い蹇撶墛閻撴盯鏌涢埄鍐╃闁稿繐鐭傞弻鐔煎矗婢跺鈧劙鏌熼銊ユ搐闁卞洦绻濋棃娑氬⒈濞村吋绻堝缁樼瑹閳ь剟鍩€椤掑倸浠滈柤娲诲灦瀹曘垽宕妷褏锛滈梺鍏兼倐濞佳囧传閾忓厜鍋撳▓鍨珮闁稿锕悰顕€宕堕鈧痪褔鏌熺粙璺ㄦ槀濞寸姰鍨归埞鎴︽倷閼搁潧娑х紓浣藉紦缁瑩鐛径鎰伋闁哄倶鍎查悗鐑樼節閻㈤潧孝婵炶绠撻崺娑㈠箳閹炽劌缍婇弫鎰板醇閻曚椒鍝楅梺姹囧焺閸ㄩ亶鎯勯鐐茶摕婵炴垯鍨圭猾宥夋煃瑜滈崜娆戝弲闂佺粯姊婚崢褏绮诲鑸电厱闁靛鍠栨晶顖炴煕鐎n亶鍎旈柡灞剧洴椤㈡洟鎮╅懠顒€濮舵繝鐢靛仜瀵爼骞冮崒姘兼綎婵炲樊浜滅粻浼村箹鏉堝墽鎮奸柣锝夋涧閳规垿鍩ラ崱妞剧凹缂備礁顑嗛幐楣冨礆閹烘挾绡€婵﹩鍓涢悾楣冩偡濠婂嫮鐭掗柟顔哄灲瀹曞崬鈽夊▎蹇庡寲濠德板€ч梽鍕偓绗涘浂鏁傞柣妯烘▕閻斿棛鎲告惔鈭舵椽鎮㈤悡搴ゆ憰闂佹寧绻傞悧蹇涙偪閳ь剚绻濋悽闈浶㈤柛鐕佸灦閹潧顫濋懜纰樻嫼闂佸憡绻傜€氼垶锝為敃鍌涚厱闁哄倽娉曢悞鎼佹煕閳瑰灝鍔滅€垫澘瀚换婵囨償閵忕姷绱﹂梻鍌欒兌绾爼宕滃┑瀣ㄢ偓鍐川椤斿墽褰炬繝銏e煐閸旀牠鍩涢幒鎴欌偓鎺戭潩閿濆懍澹曟繝鐢靛仒閸栫娀宕楅悩铏仢濠碘€崇埣瀹曞爼鏁愰崨顒€顥氭繝娈垮枟閿曗晠宕㈤悾灞绢偨闁哄被鍎查悡鐔兼煥濠靛棙澶勯柣鎺戞健閺屾洟宕堕妸銉ヮ潚濡ょ姷鍋炵敮鎺楀煘閹达箑鐐婇柕濠忓瑜扮喎鈹戦悩鍨毄濠电偐鍋撳┑鐐板尃閸パ咃紮闂佸綊妫跨粈浣虹不閻斿吋鐓ラ柣鏂挎惈椤庡矂鏌℃担闈╄含闁哄本鐩獮鍥煛娴h倽銊╂⒑鐞涒€充壕婵炲濮撮鍛不閹惰姤鐓欓柟顖滃椤ュ鐥幑鎰垫綈濞e洤锕俊鍫曞川椤斿吋顏為梻渚€鈧偛鑻崢鎼佹煟閹虹偛顩柟骞垮灩閳规垹鈧綆浜為敍婊冣攽閻樿宸ラ悗姘煎弮椤㈡瑩寮撮姀鈾€鎷婚梺绋挎湰閼归箖鍩€椤掍焦鍊愰柟顔ㄥ嫮绡€闁告洦鍘鹃悡鎾绘椤愩垺澶勭紒瀣灴閹偤宕归鐘辩盎闂佺懓鎼粔鍫曀囬敂鐣岀闁圭⒈鍘煎▍宥夋煛瀹€鈧崰鏍€佸▎鎾村仼鐎光偓閳ь剟鎮℃径鎰拺闁荤喐澹嗛敍宥夋煕鐎c劌鈧牗绌辨繝鍥ㄥ€婚柦妯猴級閵娾晜鐓忛柛顐g箖閸f椽鏌熺仦鍌氱伈婵﹤顭峰畷鎺戭潩椤戣棄浜鹃柣鎴eГ閸ゅ嫰鏌涢幘鑼妽闁稿繑绮撻弻娑㈩敃閿濆棛顦ョ紓浣插亾闁割偆鍠撶弧鈧梻鍌氱墛娓氭宕曡箛鎿冩禆闁告稑鐡ㄩ埛鎴犵磽娴e顏呮叏閸ヮ剚鐓熼煫鍥ㄦ煥閸濇椽鏌ㄥ┑鍫濅槐鐎殿喗鎸虫慨鈧柨娑樺瀵櫕绻濋悽闈涒枅婵炰匠鍏炬盯顢橀悙宥忕秬閵囨劙骞掗幘璺哄箞濠电姷鏁告慨鎾磹缂佹ḿ顩锋繛鎴炴皑绾捐偐绱撴担璐細婵炴彃鐡ㄩ〃銉╂倷瀹割喖鍓伴梺瀹狀潐閸ㄥ灝鐣锋總鍓叉晢闁稿本绮犲Σ鎼佹⒒閸屾艾鈧嘲顪冮幒鏃€顫曟い鏃€鍎崇欢銈吤归悩宸剱闁稿﹨鍩栭幈銊ノ熼崸妤€鎽甸悗瑙勬礀瀵墎鎹㈠☉銏犵婵炲棗绻掓禒鐓庘攽閿涘嫬浠掔紒顔界懇瀵鈽夐姀鐘电杸闂侀潧饪甸梽鍕偟閺傛娓婚柕鍫濇嚇閻涙粓鏌熼崙銈嗗 用户提供网络服务
以下js为测试信息51La