Upload and download files idhttp

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient,
  IdHTTP, StdCtrls;

type
  TForm1 = class(TForm)
    btn1: TButton;
    IdHTTP1: TIdHTTP;
    procedure btn1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

where
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.btn1Click (Sender: TObject);
was
  with: integer;
  sFN,sPath:string;
  MS,MS2:TMemoryStream;
begin
  MS:=TMemoryStream.Create;
  try
    sPath:=ExtractFilePath(application.ExeName)+'a.bmp';
    sFN:=ExtractFileName(sPath);
    with: = length (SFN);

    MS.WriteBuffer(iLen,sizeof(iLen));
    MS.WriteBuffer(sFN[1],iLen);

    MS2:=TMemoryStream.Create;
    MS2.LoadFromFile(sPath);

    with: = ms2.siz A;

    MS.WriteBuffer(iLen,sizeof(iLen));
    
    MS.CopyFrom(MS2,iLen);
    MS2.Free;
    IDHTTP1.post('http://127.0.0.1:2111',MS,nil);
  finally
    MS.Free;
  over ;
over ;

procedure TForm1.FormCreate(Sender: TObject);
begin
//  IdHTTP1.Host:= '127.0.0.1';
//  IdHTTP1.Port:= 2111;
end;

end.




unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, IdBaseComponent, IdComponent, IdTCPServer, IdCustomHTTPServer,
  IdHTTPServer;

type
  TForm1 = class(TForm)
    IdHTTPServer1: TIdHTTPServer;
    procedure IdHTTPServer1CommandGet(AThread: TIdPeerThread;
      ARequestInfo: TIdHTTPRequestInfo;
      AResponseInfo: TIdHTTPResponseInfo);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

where
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.IdHTTPServer1CommandGet(AThread: TIdPeerThread;
  ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);
where
  with: Integer;
  sData,sFN:string;
  MS:TMemoryStream;
  pData:PChar;
begin
  sData:=ARequestInfo.UnparsedParams;
  if sData<>'' then
  begin
  {
   Their definition of a simple message format, a data length, data 1, data length of 2, data 2
   
   | Filename length | filename | File Content-Length | file content | 
   |5|a.bmp|17788|0x10101010101001010100101...|

  }

    The MS: . = TMemoryStream the Create ;
     the try 
      // . 1 to read the file name 
      MS.WriteBuffer (sData [ . 1 ], length (sData));
      MS.Position:=0;
      MS.ReadBuffer (iLen, sizeof (iLen)); // read file name length 
      SetLength (sFN, iLen);
      MS.ReadBuffer (SFN [ . 1 ], iLen); // read file name 
      // 2 reads the contents of the file 
      MS.ReadBuffer (iLen, the sizeof (iLen)); // read the contents of the file length 
      GetMem (pData, iLen);
      MS.ReadBuffer (pData ^, iLen); // read the contents of the file saved pData which is the picture data

      // 3 saved as a file 
      MS.Clear;
      MS.WriteBuffer(pData^,iLen);
      MS.SaveToFile(ExtractFilePath(Application.ExeName)  +sFN);
    finally
      MS.Free;
    over ;
  over ;
over ;
 

procedure TForm1.FormCreate(Sender: TObject);
begin
  IdHTTPServer1.DefaultPort:= 2111;
  IdHTTPServer1.Active:= true;
  Self.Caption: = ' Start ' ;
 End ;

end.

Guess you like

Origin www.cnblogs.com/tobetterlife/p/12169477.html