再谈Internet 传送文件 (HTTP)200 (200分)

  • 主题发起人 主题发起人 fc_long
  • 开始时间 开始时间
F

fc_long

Unregistered / Unconfirmed
GUEST, unregistred user!
服务器用php接收文件,如何用delphi编写客户端程序?
即客户端程序代替表单:
<html>
<head>
</head>
<body>
<form method="post" enctype="multipart/form-data" action="upload.php">
filename : <input type="file" name="filename"><br>
<input type=submit>
</form>
</body>
</html>

如何实现.
 
我看来很多帖子, 好像无人可答的样子.
 
Indy里不是有多控件呢吗,你用那里的TCP应该就可以了
 
谁能贴一段有用的代码, 使用idhttp控件post的, 不胜感谢.
 
我最近也在研究这个问题,idhttp好像不直接支持,估计要自己把文件按
multipart/form-data的编码格式编码。
 
procedure TForm1.SendFile(filename: string);
var
Source: TMemoryStream ;
Response: TStringStream;
workPostContentstream : TStringStream ;
filestream: TMemorystream;
tep: string;
const
crLf = #$D#$A;
Sectboundary = '-----------------------------7d374371d050e';
begin
if FileExists(filename) then
begin
Response := TStringStream.Create('');
Source := TMemoryStream.Create;
workPostContentstream := TStringStream.Create('');
source.LoadFromFile(filename);
IDHTTP1.ProtocolVersion := pv1_1 ;
try
workPostContentstream.WriteString(Sectboundary); //分隔符
workPostContentstream.WriteString(crLf); // 换行
workPostContentstream.WriteString(format('Content-Disposition: form-data; name="%s" ',['id'])); //输入upload file的form name 和 filename
workPostContentstream.WriteString(crLf);
workPostContentstream.WriteString(crLf);
workPostContentstream.WriteString(FUserID);
workPostContentstream.WriteString(crLf);
workPostContentstream.WriteString(Sectboundary); //分隔符
workPostContentstream.WriteString(crLf); // 换行
workPostContentstream.WriteString(format('Content-Disposition: form-data; name="%s" ; filename="%s"',['file', filename])); //输入upload file的form name 和 filename
workPostContentstream.WriteString(crLf);
workPostContentstream.WriteString('Content-Type: audio/wav');
workPostContentstream.WriteString(crLf); // 换行
workPostContentstream.WriteString(crLf); // 换行
workPostContentstream.CopyFrom(source, source.Size);
workPostContentstream.WriteString(crLf); // 换行
workPostContentstream.WriteString(Sectboundary); //分隔符
workPostContentstream.WriteString(crLf); // 换行

IDHTTP1.Post(FURL, workPostContentstream, Response);
tep := Response.DataString;
if pos('ok',tep) >0
then
begin
Application.MessageBox('您的像片已成功上传!','',MB_OK);
//CloseIE;
end
else
begin
if sendcount < 3 then
begin
sendcount := sendcount + 1;
SendWavFile(filename);
end
else
begin
StatusBar1.Panels.Items[0].Text := '像片上传失败';
end ;
end;
except
Application.MessageBox('网络连接错误,传送失败!','错误',MB_ICONERROR+MB_OK);
workPostContentstream.Free;
Response.Free;
source.Free;
exit;
end;
workPostContentstream.Free;
Response.Free;
source.Free;
end;
end;

[:D][:D]
 
需要服务器的支持


 
后退
顶部