Вопрос

I have to upload a text file on my android project and so I used the IdFTP. This is the code:

Button2.Enabled:=False;
Label5.Text:='Uploading...';

Memo1.Lines.Add(Edit1.Text+':'+ComboBox1.Items.Text);
Memo1.SaveToFile('filehost.txt');

try
 IdFTP1.Connect;
  // I set the host, password and username
 IdFTP1.Put('filehost.txt');
finally
 IdFTP1.Disconnect;

I am having a problem because when I run the app on my Samsung (Android 2.3) I have an error that says Cannot create file "/filehost.txt". Not a directory.

I must save the content of that Memo1 in my Android device and then upload it using the IdFTP. How can I do it avoiding that error?

Это было полезно?

Решение

You can't write to the root folder. Use TPath to find a writable folder, such as from TPath.GetTempPath() or TPath.GetDocumentsPath().

Alternatively, do not use a file at all. TIdFTP.Put() has an overloaded version that uploads a TStream instead of a file, eg:

var
  MS: TMemoryStream;
begin
  MS := TMemoryStream.Create;
  try
    Memo1.Lines.SaveToStream(MS);
    MS.Position := 0;
    ...
    IdFTP1.Put(MS, 'filehost.txt');
    ...
  finally
    MS.Free;
  end;
end;
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top