要求是这样的,我用idHTTP上传文件。
但服务器组件没有更名另存为的功能。
所以我想在提交前先改名。
即调用post前,将TStream里有关文件名那一段改变一下。procedure TIdCustomHTTP.Put(AURL: string; const ASource, AResponseContent: TStream);我的代码如下
-----------------------------------------
procedure TForm1.SendPostData(filename:string);
Const
CRLF = #13#10;
var
Source: TMemoryStream;
Source1: TMemoryStream;
Response: TStringStream;
S,s1: String;
begin
Screen.Cursor := crHourGlass;
try
memoHTML.Clear;
HTTP.Request.Username := 'Admintrator';
HTTP.Request.Password := '';
HTTP.ProxyParams.ProxyServer := '';
HTTP.ProxyParams.ProxyPort := 80;
HTTP.Request.ContentType := 'multipart/form-data';
HTTP.Intercept := LogDebug;
//HTTP.InterceptEnabled := true;
Response := TStringStream.Create('');
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
HTTP.Post(cbURL.Text, Source, Response);
finally
Source.Free;
end;
memoHTML.Lines.Text := Response.DataString;
finally
Response.Free;
end;
finally
Screen.Cursor := crDefault;
end;end;
但服务器组件没有更名另存为的功能。
所以我想在提交前先改名。
即调用post前,将TStream里有关文件名那一段改变一下。procedure TIdCustomHTTP.Put(AURL: string; const ASource, AResponseContent: TStream);我的代码如下
-----------------------------------------
procedure TForm1.SendPostData(filename:string);
Const
CRLF = #13#10;
var
Source: TMemoryStream;
Source1: TMemoryStream;
Response: TStringStream;
S,s1: String;
begin
Screen.Cursor := crHourGlass;
try
memoHTML.Clear;
HTTP.Request.Username := 'Admintrator';
HTTP.Request.Password := '';
HTTP.ProxyParams.ProxyServer := '';
HTTP.ProxyParams.ProxyPort := 80;
HTTP.Request.ContentType := 'multipart/form-data';
HTTP.Intercept := LogDebug;
//HTTP.InterceptEnabled := true;
Response := TStringStream.Create('');
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
HTTP.Post(cbURL.Text, Source, Response);
finally
Source.Free;
end;
memoHTML.Lines.Text := Response.DataString;
finally
Response.Free;
end;
finally
Screen.Cursor := crDefault;
end;end;
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货