Search code examples
delphiindyindy10delphi-xe6

Indy upload Memorystream to FTP


I generate a JPEG at runtime, from a Screenshot, and want to upload it to a FTP server. But when I have it is uploaded it is no longer a valid jpeg.

In this example I just load a bitmap from the HD:

First some code:

procedure TForm13.Button1Click(Sender: TObject);
var
  InBitmap: TBitmap;
  JpegImage: TJpegImage;
  MemoryStream: TMemoryStream;
  IdFTP: TIdFTP;
begin
  InBitmap := TBitmap.Create;
  MemoryStream := TMemoryStream.Create;
  JpegImage := TJpegImage.Create;
  IdFTP := TIdFTP.Create(self);

  try
    InBitmap.LoadFromFile('C:\aa\test.bmp');

    JpegImage.Assign(InBitmap);
    JpegImage.CompressionQuality := 65;
    JpegImage.SaveToStream(MemoryStream);

    with IdFTP do
      try
        Host := <HOST>;
        Username := <USER>;
        Password := <PASS>;
        Port := 21;
        Passive := True;
        Connect;
        MemoryStream.Position := 0;
        Put(MemoryStream, 'test.jpg');
      finally
        Disconnect;
      end;    
  finally
    IdFTP.Free;
    JpegImage.Free;
    InBitmap.Free;
    MemoryStream.Free;
  end;
end;

I have tried to save the JPEG to file before uploading it, and it is a valid JPG

I have tried to save the MemortStream to a file before uploading it, and it is a valid JPG

But when it is uploaded to the FTP server it is just a blank JPG file, still "valid" though.

It is not my FTP server, but one owned by those who host my domain. Here is a link to the just uploaded jpg: http://fluffykids.dk/test.jpg and here is the jpeg I saved to disc : http://fluffykids.dk/aa.jpg.

Question: Why am I loosing the "content" of my JPEG after uploading it?


Solution

  • You have to set the TIdFTP.TransferType to ftBinary. Default is ftASCII and that makes the difference.

    with IdFTP do
      try
        Host := <HOST>;
        Username := <USER>;
        Password := <PASS>;
        Port := 21;
        Passive := True;
    
        TransferType := ftBinary;
    
        Connect;
        MemoryStream.Position := 0;
        Put(MemoryStream, 'test.jpg');
      finally
        Disconnect;
      end;