以前收集的一些资料---不用组件上载文件代码(二)

发表于:2007-06-30来源:作者:点击数: 标签:
文件futils.inc SCRIPT RUNAT=SERVER LANGUAGE= VB SCRIPT ‘’True PureASP upload - enables save of uploaded text fields to the disk. ‘’c1997-1999 Antonin Foller, PSTRUH Software, http://www.pstruh.cz ‘’The file is part of ScriptUtilities
文件futils.inc
<SCRIPT RUNAT=SERVER LANGUAGE=VBSCRIPT>
‘’True PureASP upload - enables save of uploaded text fields to the disk.
‘’c1997-1999 Antonin Foller, PSTRUH Software, http://www.pstruh.cz
‘’The file is part of ScriptUtilities library
‘’The file enables http upload to ASP without any components.
‘’But there is a small problem - ASP does not allow save binary data to the disk.
‘’ So you can use the upload for :
‘’ 1. Upload small text (or HTML) files to server-side disk (Save the data by filesystem object)
‘’ 2. Upload binary/text files of any size to server-side database (RS("BinField") = Upload("FormField").Value

‘’All uploaded files and log file will be saved to the next folder :
Dim LogFolder
LogFolder = Server.MapPath(".")

‘’********************************** SaveUpload **********************************
‘’This function creates folder and saves contents of the source fields to the disk.
‘’The fields are saved as files with names of form-field names.
‘’Also writes one line to the log file with basic informations about upload.
Function SaveUpload(Fields, DestinationFolder, LogFolder)
  if DestinationFolder = "" then DestinationFolder = Server.MapPath(".")

  Dim UploadNumber, OutFileName, FS, OutFolder, TimeName, Field
  Dim LogLine, pLogLine, OutLine

  ‘’Create unique upload folder
  Application.Lock
    if Application("UploadNumber") = "" then
      Application("UploadNumber") = 1
    else
      Application("UploadNumber") = Application("UploadNumber") + 1
    end if
    UploadNumber = Application("UploadNumber")
  Application.UnLock

  TimeName = Right("0" & Year(Now), 2) & 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) & "-" & UploadNumber
  Set FS = CreateObject("Scripting.FileSystemObject")
  Set OutFolder = FS.CreateFolder(DestinationFolder + "\" + TimeName)

  Dim TextStream
  ‘’Save the uploaded fields and create log line
  For Each Field In Fields.Items
    ‘’Write content of the field to the disk
    ‘’!!!! This function uses FileSystemObject to save the file. !!!!!
    ‘’So you can only use text files to upload. Save binary files by the function takes undefined results.
    ‘’To upload binary files see ScriptUtilities, http://www.pstruh.cz

    ‘’You can save files with original file names :
    ‘’Set TextStream = FS.CreateTextFile(OutFolder & "\" & Field.FileName )
    
    ‘’Or with names of the fields
    Set TextStream = FS.CreateTextFile(OutFolder & "\" & Field.Name & ".")

        ‘’And this is the problem why only short text files - BinaryToString uses char-to-char conversion. It takes a lot of computer time.
    TextStream.Write BinaryToString(Field.Value) ‘’ BinaryToString is in upload.inc.
    TextStream.Close
    

    ‘’Create log line with info about the field
    LogLine = LogLine & """" & LogF(Field.name) & LogSeparator & LogF(Field.Length) & LogSeparator & LogF(Field.ContentDisposition) & LogSeparator & LogF(Field.FileName) & LogSeparator & LogF(Field.ContentType) & """" & LogSeparator
  Next
  
  ‘’Creates line with global request info
  pLogLine = pLogLine & Request.ServerVariables("REMOTE_ADDR") & LogSeparator
  pLogLine = pLogLine & LogF(Request.ServerVariables("LOGON_USER")) & LogSeparator
  pLogLine = pLogLine & Request.ServerVariables("HTTP_Content_Length") & LogSeparator
  pLogLine = pLogLine & OutFolder & LogSeparator
  pLogLine = pLogLine & LogLine
  pLogLine = pLogLine & LogF(Request.ServerVariables("HTTP_USER_AGENT")) & LogSeparator
  pLogLine = pLogLine & LogF(Request.ServerVariables("HTTP_COOKIE"))

  ‘’Create output line for the client
  OutLine = OutLine & "Fields was saved to the <b>" & OutFolder & "</b> folder.<br>"
  
  DoLog pLogLine, "UP"
  
  OutFolder = Empty ‘’Clear variables.
  SaveUpload = OutLine
End Function

‘’Writes one log line to the log file
Function DoLog(LogLine, LogPrefix)
  if LogFolder = "" then LogFolder = Server.MapPath(".")
  Const LogSeparator = ", "
  Dim OutStream, FileName
  FileName = LogPrefix & Right("0" & Year(Now), 2) & Right("0" & Month(Now), 2) & Right("0" & Day(Now), 2) & ".LOG"

  Set OutStream = Server.CreateObject("Scripting.FileSystemObject").OpenTextFile(LogFolder & "\" & FileName, 8, True)
  OutStream.WriteLine Now() & LogSeparator & LogLine
  OutStream = Empty
End Function

‘’Returns field or "-" if field is empty
Function LogF(ByVal F)
  If "" & F = "" Then LogF = "-" Else LogF = "" & F
End Function

‘’Returns field or "-" if field is empty
Function LogFn(ByVal F)
  If "" & F = "" Then LogFn = "-" Else LogFn = formatnumber(F,0)
End Function

Dim Kernel, TickCount, KernelTime, UserTime
Sub BeginTimer()
on error resume next
  Set Kernel = CreateObject("ScriptUtils.Kernel") ‘’Creates the Kernel object
  ‘’Get start times
  TickCount = Kernel.TickCount
  KernelTime = Kernel.CurrentThread.KernelTime
  UserTime = Kernel.CurrentThread.UserTime
on error goto 0
End Sub

Sub EndTimer()
  ‘’Write times
on error resume next
  Response.Write "<br>Script time : " & (Kernel.TickCount - TickCount) & " ms"
  Response.Write "<br>Kernel time : " & CLng((Kernel.CurrentThread.KernelTime - KernelTime) * 86400000) & " ms"
  Response.Write "<br>User time : " & CLng((Kernel.CurrentThread.UserTime - UserTime) * 86400000) & " ms"
on error goto 0
  Kernel = Empty
End Sub
</SCRIPT>

原文转自:http://www.ltesting.net