欢迎光临
我们一直在努力

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

建站超值云服务器,限时71元/月

文件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>

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 以前收集的一些资料—不用组件上载文件代码(二)
分享到: 更多 (0)