http OnUploadFieldRequest event
Return to Introduction  Previous page  Next page
Applies to
auHTTP component.  

Declaration
type  
  TauHTTPUploadFieldRequest = procedure(Sender: TObject;  
    FileIndex: Word; UploadStream: TStream;  
    var FieldName, FileName: Stringof object;  
 
procedure OnUploadFieldRequest: TauHTTPUploadFieldRequest;  

Description
The OnUploadFieldRequest should be used to put the FieldName, FileName (if required), and data to the stream (UploadStream parameter) for further uploading to the CGI application.  
 
The auHTTP passes to the OnUploadFieldRequest event handler following parameters:  
ParameterMeaning  
FileIndexparameter specifies the index of data-field/file which should be uploaded. (Note: Total number of fields/files which should be uploaded must be specified on call of Upload method. This parameter is the index of file in queue.)  
 
UploadStreamparameter is the empty stream which should be used to write data for uploading. (Use Stream.Write() method to put data to stream, however, since this is TMemoryStream you can use other methods of TMemoryStreams).  
 
FieldNameshould be specified in this event handler. This is the name of form field.  
 
FileNameis the optional parameter used to specify local path and filename of uploaded file. It does not transmitted to CGI if empty. Use it only if your CGI application should know the real filename.  

Example
procedure TForm1.auHTTP1UploadFieldRequest(Sender: TObject;  
  FileIndex: Word; UploadStream: TStream; var FieldName, FileName: String);  
begin  
  if FileIndex = 0 then // first file  
   begin  
    FieldName := 'img1';  
    FileName := 'c:\1.jpg';  
   end  
  else // second file, if FileIndex = 1  
   begin  
    FieldName := 'img2';     
    FileName := 'c:\2.jpg';  
   end;  
 
  // put file data to stream  
  (UploadStream as TMemoryStream).LoadFromFile(FileName);  
end;  

Remarks
iiwarning Unfortunately the web server itself can NOT receive files by HTTP protocol. For this purpose you should use some intermediate CGI program, in example, written in C, Perl or PHP (or even in Delphi, if you're running Windows server). If you would like to get examples on how to create scripts which can receive files by HTTP protocol, please check out PHP.net (PHP manuals), or www.cgi-resources.com (CGI Resource Index).  

Here is an example of PHP script which we are using to upload picture from one our program:  
<?php  
 
$picdir  = '/data/www/domains/images.utilmind.com/images/';  
 
if (($index == '') || ($user == '')) die('0'); // not enough parameters POST'ed  
 
$big   = $picdir.$user.$index.'.big'// big picture  
$small = $picdir.$user.$index.'.small'//thumbnailed image  
 
if ((isset($pic)) && (isset($smallpic))) { // upload (else -- delete it)  
  copy($pic,      $big)   or die('2'); // store big picture  
  copy($smallpic, $small) or die('3'); // store thumbnailed image  
else {  
  if (file_exists($big))   unlink($big); // delete big picture  
  if (file_exists($small)) unlink($small); //delete thumbnailed image  
}  
 
?>  
1  
 
tip If script above does not work for some reason — use $_FILES predefined variable ($_FILES['fieldname']['name'] returns the name of uploaded file, $_FILES['fieldname']['tmp_name'] returns the location where uploaded data temporary stored. Following PHP function moves temporary file to permanent location:  
move_uploaded_file($_FILES['field_name']['tmp_name'], $_SERVER['DOCUMENT_ROOT'].'/permanent_location/file.dat'or die('Cannot copy file!');  
 
iiinfo Here is the link to nice file uploader class in PHP: http://dave.imarc.net/downloads/fileupload.zip  
 
And on Client side we are using following code to POST required data:  
procedure TMEMPicUploader.UploadPictureHTTPFieldRequest(Sender: TObject;  
  FileIndex: Word; UploadStream: TStream; var FieldName,  
  FileName: String);  
const  
  FieldNames: Array[0..3of String = ('user', 'index', 'pic', 'smallpic');  
var  
  W, H: Integer;  
  PicIndexStr: String;  
  BigImage, SmallImage: TacProportionalImage;  
begin  
  FieldName := FieldNames[FileIndex];  
 
  with UploadStream, Client.MyProfile do  
   case FileIndex of  
     0: Write(Username[1], Length(Username));  
     1begin  
         PicIndexStr := IntToStr(FPictureIndex);  
         UploadStream.Write(PicIndexStr[1], Length(PicIndexStr));  
        end;  
     else  
       FileName := Username;  
 
       if FileIndex = 2 then  
         FPicture.Graphic.SaveToStream(UploadStream) // normal picture  
       else  
        begin // thumbnailed picture  
         BigImage := TacProportionalImage.Create(Self);  
         try  
           BigImage.Picture.Assign(FPicture);  
           BigImage.Width := FThumbnailWidth;  
           BigImage.Height := FThumbnailHeight;  
           SmallImage := TacProportionalImage.Create(Self);  
           try  
             // CREATE THUMBNAILED IMAGE  
             with BigImage, DrawRect do  
              begin  
               W := Right - Left;  
               H := Bottom - Top;  
               with SmallImage.Picture.Bitmap do  
                begin  
                 Width  := W;  
                 Height := H;  
                end;  
               SmallImage.Canvas.StretchDraw(Rect(00, W, H), Picture.Graphic);  
              end;  
 
             with TJPEGImage.Create do  
              try  
                Assign(SmallImage.Picture.Bitmap);  
                SaveToStream(UploadStream);  
              finally  
                Free;  
              end;  
           finally  
             SmallImage.Free;  
           end;  
         finally  
           BigImage.Free;  
         end;  
        end;  
    end;  
end;  
 
iiwarning If you want to implement uploading of some bulky data to password protected directories, you must know that all uploading data is sent in the headers of HTTP request, before the HTTP server returns the notice that that login information is required. So you must specify Username and Password before uploading, otherwise, if you specifying login information in the OnPasswordRequest event handler, all request headers (all uploading files) will be sent more than once, every time when you set TryAgain parameter of OnPasswordRequest event handler to True.  

See also
Upload method and OnUploadProgress event.  
File not found.