我必须在我的android项目上传一个文本文件,所以我使用了IdFTP
。这是代码:
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;
我遇到了问题,因为当我在三星(Android 2.3)上运行应用程序时,出现错误Cannot create file "/filehost.txt". Not a directory
。
我必须在我的Android设备中保存该Memo1的内容,然后使用IdFTP上传它。我怎么能避免这个错误?
答案 0 :(得分:3)
您无法写入根文件夹。使用TPath
查找可写文件夹,例如来自TPath.GetTempPath()
或TPath.GetDocumentsPath()
。
或者,根本不要使用文件。 TIdFTP.Put()
有一个重载版本,可以上传TStream
而不是文件,例如:
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;