![]() |
![]() ![]() ![]() |
auHTTP component.
|
type
|
TauHTTPUploadFieldRequest = procedure(Sender: TObject;
|
FileIndex: Word; UploadStream: TStream;
|
var FieldName, FileName: String) of object;
|
|
procedure OnUploadFieldRequest: TauHTTPUploadFieldRequest;
|
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:
|
Parameter | Meaning
|
FileIndex | parameter 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.)
|
|
UploadStream | parameter 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).
|
|
FieldName | should be specified in this event handler. This is the name of form field.
|
|
FileName | is 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.
|
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;
|
![]() |
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
|
|
![]() |
move_uploaded_file($_FILES['field_name']['tmp_name'], $_SERVER['DOCUMENT_ROOT'].'/permanent_location/file.dat') or die('Cannot copy file!');
|
|
![]() |
|
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..3] of 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));
|
1: begin
|
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(0, 0, 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;
|
|
![]() |
Upload method and OnUploadProgress event.
|