使用idhttp上传文件的方法
Const
CRLF = #13#10;
var
s,s1,filename:String;
response:TStringStream;
source,source1:TMemoryStream;
Url:string;
i,cnt:integer;
begin
idhttp.Request.ContentType:='multipart/form-data';
Response := TStringStream.Create('');
url:='http://'+host+dir;
cnt:=files.Count;
if (not dead) then
begin
for i:=0 to cnt-1 do
begin
filename:=files[i];
if fileexists(filename) then
begin
try
S := '-----------------------------7cf1d6c47c' + CRLF +
'Content-Disposition: form-data; name="file1"; filename="'+filename+'"'+CRLF +
'Content-Type: application/octet-stream' + CRLF + CRLF;
//上传文件内容
s1:='file one content. Contant-Type can be application/octet-stream or if'+
'you want you can ask your OS fot the exact type.' + CRLF +
'-----------------------------7cf1d6c47c' + CRLF + //分界符,用于分隔表单(Form)中的各个域
'Content-Disposition: form-data; name="text1"' + CRLF + CRLF +
'hello2' + CRLF +
'-----------------------------7cf1d6c47c--';
//提交的下一个表单内容域的内容
s1:=CRLF +'-----------------------------7cf1d6c47c' + CRLF +
'Content-Disposition: form-data; name="text1"' + CRLF + CRLF +
'hello2' + CRLF +
'-----------------------------7cf1d6c47c--';
Source := TMemoryStream.Create;
Source1 := TMemoryStream.Create;
Source1.LoadFromFile(filename);
Response:=TStringStream.Create('') ;
Response.CopyFrom(source1,source1.Size);
s:=s+Response.DataString;//因为只能传字符串
Source.Position :=0;
Source.Write(s[1],length(s));
Source.Position :=source.Size ;
Source.Write(s1[1],length(s1));
Response.Position :=0;
try
idHTTP.Post(url, Source, Response);
finally
if not uploadsuccess(Response.DataString) then
begin
dead:=true;
self.idhttp.Disconnect;
end;
Source.Free;
Response.Free;
end;