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

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

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 acHTTP 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.acHTTP1UploadFieldRequest(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;  

Remark
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  
 
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;  

See also
Upload method and OnUploadProgress event.  
Экскурсионный тур по Европе: туры в норвегию. Авиабилеты, Туры, Командировки. | Театр на Лебединое озеро. Билеты на лебединое озеро, весна 2012. | сывороточный протеин | продажа hyundai county