虛擬主機Asp無組件文件上傳到租用服務器空間的實例

      發布時間:2006-06-18 22:09:24   瀏覽人數: 603365 次  
        關鍵字: 無組件上傳 - 本站搜索 - 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數據庫例程

      旗下網站:耐思智慧 - 淘域網 - 我的400電話 - 中文域名:耐思尼克.cn 耐思尼克.top

      耐思智慧 © 版權所有 Copyright © 2000-2024 IISP.COM,Inc. All rights reserved

      備案號碼: 粵ICP備09063828號  公安備案號: 公安備案 粵公網安備 44049002000123號  域名注冊服務機構許可:粵D3.1-20240003 CN域名代理自深圳萬維網

      聲明:本網站中所使用到的其他各種版權內容,包括但不限于文章、圖片、視頻、音頻、字體等內容版權歸原作者所有,如權利所有人發現,請及時告知,以便我們刪除版權內容

      本站程序界面、源代碼受相關法律保護, 未經授權, 嚴禁使用; 耐思智慧 © 為我公司注冊商標, 未經授權, 嚴禁使用

      法律顧問:珠海知名律師 廣東篤行律師事務所 夏天風 律師

      主站蜘蛛池模板: 日本一区二区三区在线网| 午夜视频在线观看一区| 亚洲日韩精品国产一区二区三区| 国产一区二区影院| 无码国产精品一区二区免费| 免费国产在线精品一区| 欲色aV无码一区二区人妻| 国产亚洲一区二区三区在线观看| 好吊妞视频一区二区| 激情内射亚洲一区二区三区爱妻| 中文字幕无线码一区| 国产精品亚洲不卡一区二区三区 | 无码人妻精品一区二区蜜桃| 秋霞午夜一区二区| 中文字幕在线观看一区 | 午夜精品一区二区三区免费视频| 亚洲午夜精品第一区二区8050| 精品国产区一区二区三区在线观看| 少妇无码一区二区三区| 久久亚洲AV午夜福利精品一区| 久久精品人妻一区二区三区| 亚洲一区二区高清| 国产福利电影一区二区三区久久久久成人精品综合 | 国产成人精品亚洲一区| 91精品一区二区综合在线| 在线播放国产一区二区三区| 人妻天天爽夜夜爽一区二区| 人妻av无码一区二区三区| 亚洲成AV人片一区二区密柚 | 97se色综合一区二区二区| 3d动漫精品一区视频在线观看| 91一区二区视频| 免费无码一区二区三区蜜桃大 | 麻豆一区二区三区精品视频| 精品国产一区二区三区香蕉 | 日本丰满少妇一区二区三区| 色多多免费视频观看区一区| V一区无码内射国产| 中文字幕在线无码一区| 久久高清一区二区三区| 亚洲国产高清在线精品一区|