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

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

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 acHTTP passes to the OnUploadFieldRequest event handler following parameters:  
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 field of the "web form".  
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. Leave the FileName empty if it's not required for your CGI.  

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

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 manuals), or (CGI Resource Index).  

Here is an example of PHP script which we are using to upload picture from one our program:  
$picdir  = '/data/www/domains/';  
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  
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:  
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);  
  FieldNames: Array[0..3of String = ('user', 'index', 'pic', 'smallpic');  
  W, H: Integer;  
  PicIndexStr: String;  
  BigImage, SmallImage: TacProportionalImage;  
  FieldName := FieldNames[FileIndex];  
  with UploadStream, Client.MyProfile do  
   case FileIndex of  
     0: Write(Username[1], Length(Username));  
         PicIndexStr := IntToStr(FPictureIndex);  
         UploadStream.Write(PicIndexStr[1], Length(PicIndexStr));  
       FileName := Username;  
       if FileIndex = 2 then  
         FPicture.Graphic.SaveToStream(UploadStream) // normal picture  
        begin // thumbnailed picture  
         BigImage := TacProportionalImage.Create(Self);  
           BigImage.Width := FThumbnailWidth;  
           BigImage.Height := FThumbnailHeight;  
           SmallImage := TacProportionalImage.Create(Self);  
             with BigImage, DrawRect do  
               W := Right - Left;  
               H := Bottom - Top;  
               with SmallImage.Picture.Bitmap do  
                 Width  := W;  
                 Height := H;  
               SmallImage.Canvas.StretchDraw(Rect(00, W, H), Picture.Graphic);  
             with TJPEGImage.Create do  
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;  
OnUploadProgress and OnPasswordRequest events.