熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

asp文件上傳:文件上傳 源代碼

2013-11-13 10:38:39  來源: .NET編程 

  <!#include FILE="uploadinc">
<%
dim uploadfileformNameformPathiCountfilenamefileExt
set upload=new upload_xSoft 建立上傳對象
formPath="uploadimages/"
 在目錄後加(/)
if right(formPath)<>"/" then formPath=formPath&"/"
iCount=
for each formName in uploadfile 列出所有上傳了的文件
 set file=uploadfile(formName)  生成一個文件對象
 if filefilesize< then
  responsewrite "<font size=><br>請先選擇你要上傳的圖片 [ <a href=# onclick=historygo()>重新上傳</a> ]</font>"
 responseend
 end if
 
 if filefilesize> then
  responsewrite "<font size=><br>圖片大小超過了限制 [ <a href=# onclick=historygo()>重新上傳</a> ]</font>"
 responseend
 end if

  fileExt=lcase(right(filefilename))

  if fileEXT<>"jpg" and fileEXT<>"gif" then
  responsewrite "<font size=><br>文件格式只能為jpg和gif格式 [ <a href=# onclick=historygo()>重新上傳</a> ]</font>"
 responseend
 end if
 randomize
 ranNum=int(*rnd)+
 filename=formPath&year(now)&month(now)&day(now)&hour(now)&minute(now)&second(now)&fileExt
 filename=year(now)&month(now)&day(now)&hour(now)&minute(now)&second(now)&fileExt
 
 if fileFileSize> then         如果 FileSize > 說明有文件數據
 fileSaveAs Servermappath(filename)   保存文件
responsewrite "<script>parentformimgvalue="&FileName&"</script>"

  iCount=iCount+
 end if
 set file=nothing
next
set upload=nothing  刪除此對象

  ResponseWrite "<img src="&"/UpLoad/UpLoadImages/"&FileName&" onload =DrawImage(this)>"
 responseend
%>

  下面是uploadinc的代碼

  <SCRIPT RUNAT=SERVER LANGUAGE=VBSCRIPT>
dim upfile_xSoft_Stream
Class upload_xSoft
dim FormFileVersion
Private Sub Class_Initialize
dim iStartiFileNameStartiFileNameEndiEndvbEnteriFormStartiFormEndtheFile
dim strDivmFormNamemFormValuemFileNamemFileSizemFilePathiDivLenmStr
if RequestTotalBytes< then Exit Sub
set Form=CreateObject("ScriptingDictionary")
set File=CreateObject("ScriptingDictionary")
set upfile_xSoft_Stream=CreateObject("AdodbStream")
upfile_xSoft_Streammode=
upfile_xSoft_Streamtype=
upfile_xSoft_Streamopen
upfile_xSoft_Streamwrite RequestBinaryRead(RequestTotalBytes)
vbEnter=Chr()&Chr()
iDivLen=inString(vbEnter)+
strDiv=subString(iDivLen)
iFormStart=iDivLen
iFormEnd=inString(iformStartstrDiv)
while iFormStart < iFormEnd
  iStart=inString(iFormStart"name=""")
  iEnd=inString(iStart+"""")
  mFormName=subString(iStart+iEndiStart)
  iFileNameStart=inString(iEnd+"filename=""")
  if iFileNameStart> and iFileNameStart<iFormEnd then
   iFileNameEnd=inString(iFileNameStart+"""")
   mFileName=subString(iFileNameStart+iFileNameEndiFileNameStart)
   iStart=inString(iFileNameEnd+vbEnter&vbEnter)
   iEnd=inString(iStart+vbEnter&strDiv)
   if iEnd>iStart then
    mFileSize=iEndiStart
   else
    mFileSize=
   end if
   set theFile=new FileInfo
   theFileFileName=getFileName(mFileName)
   theFileFilePath=getFilePath(mFileName)
   theFileFileSize=mFileSize
   theFileFileStart=iStart+
   theFileFormName=FormName
   fileadd mFormNametheFile
  else
   iStart=inString(iEnd+vbEnter&vbEnter)
   iEnd=inString(iStart+vbEnter&strDiv)

  if iEnd>iStart then
    mFormValue=subString(iStart+iEndiStart)
   else
    mFormValue=""
   end if
   formAdd mFormNamemFormValue
  end if

  iFormStart=iformEnd+iDivLen
  iFormEnd=inString(iformStartstrDiv)
wend
End Sub

  Private Function subString(theStarttheLen)
 dim icstemp
 upfile_xSoft_StreamPosition=theStart
 stemp=""
 for i= to theLen
   if upfile_xSoft_StreamEOS then Exit for
   c=ascB(upfile_xSoft_StreamRead())
   If c > Then
    if upfile_xSoft_StreamEOS then Exit for
    stemp=stemp&Chr(AscW(ChrB(AscB(upfile_xSoft_StreamRead()))&ChrB(c)))
    i=i+
   else
    stemp=stemp&Chr(c)
   End If
 Next
 subString=stemp
End function

  Private Function inString(theStartvarStr)
 dim ijbttheLenstr
 InString=
 Str=toByte(varStr)
 theLen=LenB(Str)
 for i=theStart to upfile_xSoft_StreamSizetheLen
   if i>upfile_xSoft_Streamsize then exit Function
   upfile_xSoft_StreamPosition=i
   if AscB(upfile_xSoft_StreamRead())=AscB(midB(Str)) then
    InString=i
    for j= to theLen
      if upfile_xSoft_StreamEOS then
        inString=
        Exit for
      end if
      if AscB(upfile_xSoft_StreamRead())<>AscB(MidB(Strj)) then
        InString=
        Exit For
      end if
    next
    if InString<> then Exit Function
   end if
 next
End Function

  Private Sub Class_Terminate 
  formRemoveAll
  fileRemoveAll
  set form=nothing
  set file=nothing
  upfile_xSoft_Streamclose
  set upfile_xSoft_Stream=nothing
End Sub
  
 
 Private function GetFilePath(FullPath)
  If FullPath <> "" Then
   GetFilePath = left(FullPathInStrRev(FullPath ""))
  Else
   GetFilePath = ""
  End If
 End  function
 
 Private function GetFileName(FullPath)
  If FullPath <> "" Then
   GetFileName = mid(FullPathInStrRev(FullPath "")+)
  Else
   GetFileName = ""
  End If
 End  function

  Private function toByte(Str)
   dim iiCodeciLowiHigh
   toByte=""
   For i= To Len(Str)
   c=mid(Stri)
   iCode =Asc(c)
   If iCode< Then iCode = iCode +
   If iCode> Then
     iLow = Left(Hex(Asc(c)))
     iHigh =Right(Hex(Asc(c)))
     toByte = toByte & chrB("&H"&iLow) & chrB("&H"&iHigh)
   Else
     toByte = toByte & chrB(AscB(c))
   End If
   Next
 End function
End Class

  
Class FileInfo
  dim FormNameFileNameFilePathFileSizeFileStart
  Private Sub Class_Initialize
    FileName = ""
    FilePath = ""
    FileSize =
    FileStart=
    FormName = ""
  End Sub
 
 Public function SaveAs(FullPath)
    dim drErrorChari
    SaveAs=
    if trim(fullpath)="" or FileSize= or FileStart= or FileName="" then exit function
    if FileStart= or right(fullpath)="/" then exit function
    set dr=CreateObject("AdodbStream")
    drMode=
    drType=
    drOpen
    upfile_xSoft_Streamposition=FileStart
    upfile_xSoft_Streamcopyto drFileSize
    drSaveToFile FullPath
    drClose
    set dr=nothing
    SaveAs=
  end function
End Class
</SCRIPT>


From:http://tw.wingwit.com/Article/program/net/201311/14198.html
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.