Upload - Monitor and handle upload state/result

          Article, Upload - Monitor and handle upload state/result 
          Member of  ScriptUtils.ASPForm 

Upload - Monitor and handle upload state/result
 IIS server and Upload/POST data   
        The POST request comunication between client and server is realized in blocks of data. 
  1. Client sends http header with basic info about POST data - content-type and content-length headers.
  2. IIS will then response 100 Continue http header and will try to read source data from client. 
  3. Client sends form data
  4. IIS reads UploadReadAheadSize bytes first, and the data are passed to ASP BinaryRead. ASP can read reast of data using BinaryRead.

      This IIS concept is a problem to handle errors and limit upload size with large uploads. The problem is mainly with UploadReadAheadSize and 100 Continue IIS message. You have to change UploadReadAheadSize value to zero if you want to control and properly handle fsSizeLimit event. UploadReadAheadSize can be set for whole IIS, web site, virtual dir, folder or file. The basic code to set this value is:
  Dim WebServer, UploadReadAheadSize
'  Set WebServer = GetObject("IIS://localhost/W3SVC[/sitenum[/folder[/uploadscript.asp]]]")
  Set WebServer = GetObject("IIS://localhost/W3SVC/1")
  
  'Get UploadReadAheadSize property
  UploadReadAheadSize = WebServer.UploadReadAheadSize
  
  'Set another size
  WebServer.UploadReadAheadSize = 0
  WebServer.SetInfo 

    You can also use adsutil.vbs administration script to set the value, or directly edit IIS metabase.
 
cscript adsutil.vbs SET W3SVC/1/Root/Upload/UploadReadAheadSize 0
 
    You can set any other response code instead of first 100 Continue with UploadReadAheadSize set to zero. This property has to be set to control form size limit. If a client exceeds form size limit, you can set response.status to "413 Request Entity Too Large" and response.write some explanation message about the problem. Please see more in Cannot find server or DNS Error, page cannot be displayed when uploading files? and UploadReadAheadSize articles.
 Monitor State property   
        Huge-ASP upload lets you monitor several upload states. Main property to monitor upload results and current state is ScriptUtils.ASPForm.State with ScriptUtils.FormStates values. 
       Upload can finish as completed (State = fsCompletted) or with some error (State>fsError). There are three usual errors: Form data exceeds size limit, upload time exceeds timeout limit and client was stopped upload.
       There may be also two different states of cancel of upload - With/Without progress bar. Huge-ASP upload progress bar has a special querystring parameter to cancel upload - request.QueryString("Action") = "Cancel", when user clicks "Cancel" button on progress indicator.
       You can see typical code to handle possible upload states bellow:
<%
  Dim Form: Set Form = Server.CreateObject("ScriptUtils.ASPForm")
  'Do not upload data greater than 20MB.
  Form.SizeLimit = 20*&H100000
  'Set maximum upload time to 10 munutes.
  Server.ScriptTimeout = 10*60
  
  Const fsCompletted  = 0
  Const fsSizeLimit   = &HD
  Const fsTimeOut     = &HE
  Const fsError       = &HA
  Dim hResult
  If Form.State > fsError Then 'Some error state. 
    If Form.State = fsSizeLimit Then 'Data size exceeds limit. 
      hResult = "Form data exceeds limit (" & Form.SizeLimit/1024 & "kB)."
    ElseIf Form.State = fsTimeOut Then 'Request timeout 
      hResult = "Upload time exceeds limit (" & Form.ReadTimeout & "s)."
    Else
      hResult = "Another upload problem."
    End If
    hResult = "<Font Color=Red>" & hResult & "</Font>"
    Response.Status = "400 Bad request"
  ElseIf Form.State = fsCompletted Then 'Completted
    hResult = "n of form Items:" & Form.Items.Count
    '..... Process form fields
  ElseIf Request.QueryString("Action") = "Cancel" Then
  hResult = "Upload was cancelled."
End If %>

      Notes.
      1. Remember that fsZeroLength is a regular state for application/x-www-form-urlencoded forms (application/x-www-form-urlencoded without fields, there is not usual state, of course)
      2. You do not need a special handler for fsNoConnected state - client will not see output message if disconnected.
      3. fsInProgress and fsNone states are designed for HTML progress bar indicator. ASPForm object is true free-threaded class, so you can access object state and form fields from another ASP task. The second task will receive fsNone state before first upload data was received and fsInProgress during upload (see ScriptUtils.ASPForm.Upload - Progress bar indicator)
  Other links for Upload - Monitor and handle upload state/result
      Easy to use, hi-performance ASP file upload component with progress bar indicator. Let's you upload multiple files with size up to 2GB to a disk or database along with another form fields. Works with large posts, any character set (including unicode utf-8). Contains one-click multiple files/folders download with on-the-fly compression (Using BinaryWrite/BinaryRead).
      Hi-performance text file logging for ASP/VBScript/VBA applications. Lets you create daily/weekly/monthly log files with variable number of logged values and extra timing and performance info.
       Lets you work with safearray binary data in VBS/JS. It also enables conversion between binary and String data using several code pages. ZLib compress and uncompress functions. Lets you transfer files using compressed stream from a client to server using IE.
      This library also enables calling of some Kernel and Advapi functions (performance, timing, sleep, configuration ...) and enables native work with INI files.


© 1996 – 2005 Motobit Software, help{at}pstruh.cz, help v. 2.16.14