Upload File dengan Indy


Seorang teman bertanya mengenai upload file dengan Delphi component Indy. Setelah googling dan nanya-nanya sama Mas Totok, akhirnya ketemu juga caranya yang cukup mudah.

Untuk di server, cukup membutuhkan komponen TIDHttpServer dan di client TIDHttp.

unit unitClient;
interface
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient,
  IdHTTP;
type
  TformMain = class(TForm)
    IdHTTP1: TIdHTTP;
    btnSend: TButton;
    OpenDialog1: TOpenDialog;
    procedure btnSendClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
var
  formMain: TformMain;
implementation
uses
  IdMultipartFormData;
{$R *.dfm}
{ TForm8 }
procedure TformMain.btnSendClick(Sender: TObject);
var
  Stream: TStringStream;
  Params: TIdMultipartFormDataStream;
begin
  try
    Stream := TStringStream.Create('');
    try
      Params := TIdMultipartFormDataStream.Create;
      try
        if OpenDialog1.Execute then
        begin
          Params.AddFile('File1', OpenDialog1.FileName,
            'application/octet-stream');
          try
            IdHTTP1.Post('http://localhost:8080/upload', Params, Stream);
          except
            on E: Exception do
              ShowMessage('Error encountered during Post: ' + E.Message);
          end;
        end;
      finally
        Params.Free;
      end;
    finally
      Stream.Free;
    end;
  except
  end;
end;
end.
 

Sedangkan untuk di sisi servernya:

 
unit TestHTTPServer;
interface
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, IdBaseComponent, IdComponent, IdCustomTCPServer, IdCustomHTTPServer,
  IdHTTPServer, IdContext, StdCtrls;
type
  TformMain = class(TForm)
    IdHTTPServer1: TIdHTTPServer;
    Memo1: TMemo;
    procedure IdHTTPServer1CommandGet(AContext: TIdContext;
      ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
var
  formMain: TformMain;
implementation
{$R *.dfm}
procedure TformMain.FormCreate(Sender: TObject);
begin
  IdHTTPServer1.Active := True;
end;
procedure TformMain.IdHTTPServer1CommandGet(AContext: TIdContext;
  ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);
var
  memStream: TMemoryStream;
begin
  memStream := TMemoryStream.Create;
  memStream.CopyFrom(ARequestInfo.PostStream, 0);
  try
    Memo1.Lines.Add('Saving to c:\test.zip...');
    memStream.SaveToFile('c:\test.zip');
    Memo1.Lines.Add('Saving done!');
  finally
    memStream.Free;
  end;
end;
end.

Diterbitkan oleh wisnuwidiarta

Hi, my name is Wisnu Widiarta. I am a movie lover and love traveling especially camping and doing outdoor activities. Coding and problem solving in general are things I love as well.

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout /  Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout /  Ubah )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d blogger menyukai ini: