[DUG] HTTP File Upload
Conor Boyd
Conor.Boyd at trimble.co.nz
Tue Sep 16 08:05:32 NZST 2008
I've used the Indy library that comes with Delphi to do HTTP uploads
with progress notification. Works quite well.
E.g.
FHTTPClient := TIdHTTP.Create(nil);
with FHTTPClient do begin
ProxyParams.ProxyServer:= ProxyHost;
ProxyParams.ProxyPort := ProxyPort;
ProxyParams.ProxyUsername := ProxyUsername;
ProxyParams.ProxyPassword := ProxyPassword;
OnWork := HTTPWorkEvent;
OnWorkBegin := HTTPBeginWorkEvent;
OnWorkEnd := HTTPEndWorkEvent;
end;
I use it to upload a JPEG to a webserver running Gallery
(http://gallery.menalto.com/) as follows:
function TGalleryHelper.AddImage(const AlbumID: Integer; const Image:
TStream; const ImageFilename, Caption, Description, Summary: String):
TGalleryStatus;
var
Command: String;
Response: String;
ImageStream: TIdMultipartFormDataStream;
begin
...
ImageStream := TIdMultiPartFormDataStream.Create;
try
ImageStream.AddFormField('g2_form[cmd]', 'add-item');
ImageStream.AddFormField('g2_form[protocol_version]', Format('%f',
[PROTOCOL_VERSION]));
ImageStream.AddFormField('g2_form[set_albumId]', Format('%d',
[AlbumID]));
ImageStream.AddFormField('g2_form[caption]', Caption);
ImageStream.AddFormField('g2_form[force_filename]',
ImageFilename);
ImageStream.AddFormField('g2_form[extrafield.Summary]', Summary);
ImageStream.AddFormField('g2_form[extrafield.Description]',
Description);
ImageStream.AddObject('g2_userfile', 'image/jpeg', Image,
ImageFilename);
Response := FHTTPClient.Post(Command, ImageStream);
finally
ImageStream.Free;
end;
...
The events that the TIdHTTPClient class raises can be handled as
follows:
procedure TGalleryHelper.HTTPBeginWorkEvent(Sender: TObject; AWorkMode:
TWorkMode; const AWorkCountMax: Integer);
begin
// Do something with AWorkCountMax, e.g. set the max value on a
progress bar.
end;
procedure TGalleryHelper.HTTPEndWorkEvent(Sender: TObject; AWorkMode:
TWorkMode);
begin
// You're all done.
end;
procedure TGalleryHelper.HTTPWorkEvent(Sender: TObject; AWorkMode:
TWorkMode; const AWorkCount: Integer);
begin
// Do something with AWorkCount, e.g. set the progress value of a
progress bar.
end;
I have all this running in a thread to keep my GUI nice and responsive
while the upload is happening.
HTH, if you've more questions, let me know.
Conor
________________________________
From: delphi-bounces at delphi.org.nz [mailto:delphi-bounces at delphi.org.nz]
On Behalf Of Jeremy Coulter
I am wondering if anyone has seen or has any Delphi code that will do an
http file upload, BUT that shows you the upload progress.
I have looked at the Synapse code for doing this, but it doesn't have
the ability to show the progress.
Why use HTTP File upload and not FTP? simple answer, not every site has
FTP enabled...as we have found out, and email has been a bit unreliable.
I have taken a look at the odd Java plugin that does this which would be
good because I could just have my app. look at a webpage instead of
build it into the exe, but then, no guarantees that java is enabled
either....sigh...the joys :-(
OR someone might even have a brighter idea ;-)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://listserver.123.net.nz/pipermail/delphi/attachments/20080916/34c9bc3e/attachment-0001.html
More information about the Delphi
mailing list