gdhami

Delphi Client-side POST / Upload

Feb 29th, 2012
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 1.27 KB | None | 0 0
  1.      with IdHTTP do
  2.      begin
  3.           HTTPOptions     := [hoForceEncodeParams];
  4.           AllowCookies    := True;
  5.           HandleRedirects := True;
  6.           ProtocolVersion := pv1_1;
  7.      end;    // with
  8.  
  9.      Stream          := TIdMultipartFormDataStream.Create;
  10.  
  11.      EncodedFileName := TIdEncoderMIME.EncodeString(ExtractFileName(Trim(FileName)), IndyUTF8Encoding);
  12.      EncodedFileName := StringReplace(EncodedFileName, '+', '-', [rfReplaceAll, rfIgnoreCase]);
  13.      EncodedFileName := StringReplace(EncodedFileName, '/', '_', [rfReplaceAll, rfIgnoreCase]);
  14.      EncodedFileName := StringReplace(EncodedFileName, '=', ',', [rfReplaceAll, rfIgnoreCase]);
  15.  
  16.      with Stream.AddFormField('file_name', EncodedFileName, 'utf-8') do
  17.      begin
  18.           HeaderCharset  := 'utf-8';
  19.           HeaderEncoding := '8';
  20.      end;    // with
  21.  
  22.      IdHttp.Head('http://domain.com/data/my-file.zip');
  23.      ByteRange       := Http.Response.ContentLength;
  24.  
  25.      // TODO: Get the rest of the file that corresponds to [ByteRange-...]
  26.  
  27.      with Stream.AddFile('upload_file', FileName, 'application/octet-stream') do
  28.      begin
  29.           HeaderCharset  := 'utf-8';
  30.           HeaderEncoding := '8';
  31.      end;    // with
  32.  
  33.      PostStatus      := Trim(IdHTTP.Post(POST_URL, Stream));
Advertisement
Add Comment
Please, Sign In to add comment