我試圖使用庫THTTPSendEx上載文件。如何使用Synapse將文件上載到服務器
我的代碼:
procedure TForm1.Button1Click(Sender: TObject);
var
HTTP:THTTPSendEx;
Data:TMultipartFormDataStream;
sHTML:string; //Recived HTML code from web
begin
if OpenDialog1.Execute then
begin
HTTP:=THTTPSEndEx.Create;
Data:=TMultipartFormDataStream.Create;
try
Data.AddFile('myFile', OpenDialog1.FileName);
Data.DataEnd;
if HTTP.Post('http://kaon.rghost.ru/files',Data,sHTML) then
begin
//Connection established
//Check HTTP response
if HTTP.IsSuccessfull then //HTTP returns "200 OK" code.
begin
ShowMessage('File successfully posted to the server.');
end;
end else
begin
ShowMessage('Can not establish a connection to the server...'+#13+'Network is not avaliable or server socket does not exist.');
end;
finally
FreeAndNil(HTTP);
FreeAndNil(Data);
end;
end;
end;
但是不打印。提示什麼問題?
我的翻譯不好對不起,我用谷歌翻譯器
UPDATE:爲PHP
工作例如:
$ url = 'http://kaon.rghost.ru/files';
$ FILENAME = 'add.png';
$ files = array ('file' => '@'. $ FILENAME);
$ useragent = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv: 1.9.0.3) Gecko/2008092417 Firefox/3.0.3';
$ ch = curl_init ($ url);
curl_setopt ($ ch, CURLOPT_HTTPHEADER, array ("Content-type: multipart/form-data"));
curl_setopt ($ ch, CURLOPT_HEADER, 1);
curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ ch, CURLOPT_USERAGENT, $ useragent);
curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ files);
$ response = curl_exec ($ ch);
echo $ response;
你有沒有嘗試單步調試器中的代碼?當你做什麼時會發生什麼? –
不,我沒有試過。代碼取自: http://stackoverflow.com/questions/11471513/how-to-send-a-file-and-other-post-data-with-synapse – Muhammad
如果你不能被打擾爲了努力解決你自己的問題,我們爲什麼要這樣做?此外,您發佈的PHP代碼完全是多餘的,與Delphi問題無關。 –