var
response: TStringStream;
MMPFDS: TIdMultiPartFormDataStream;
idhttp1: TIDhttp;
begin
if OpenDialog1.Execute then
begin
MMPFDS := TIdMultiPartFormDataStream.Create;
response := TStringStream.Create('');
idhttp1 := TIDhttp.Create(nil);
MMPFDS.AddFile('file1', OpenDialog1.FileName, 'multipart/form-data'); //
idhttp1.Request.ContentType := MMPFDS.RequestContentType; MMPFDS.Position := 0;
try
idhttp1.Post('http://119.119.119.69/upload/upload.asp', MMPFDS, response);
finally
showMessage(response.DataString);
MMPFDS.Free;
response.Free;
idhttp1.Disconnect;
idhttp1.Free;
end;
end;
end;在上传文件的第一行总是加了"ntent-Transfer-Encoding: binary" 导致文件打不开
response: TStringStream;
MMPFDS: TIdMultiPartFormDataStream;
idhttp1: TIDhttp;
begin
if OpenDialog1.Execute then
begin
MMPFDS := TIdMultiPartFormDataStream.Create;
response := TStringStream.Create('');
idhttp1 := TIDhttp.Create(nil);
MMPFDS.AddFile('file1', OpenDialog1.FileName, 'multipart/form-data'); //
idhttp1.Request.ContentType := MMPFDS.RequestContentType; MMPFDS.Position := 0;
try
idhttp1.Post('http://119.119.119.69/upload/upload.asp', MMPFDS, response);
finally
showMessage(response.DataString);
MMPFDS.Free;
response.Free;
idhttp1.Disconnect;
idhttp1.Free;
end;
end;
end;在上传文件的第一行总是加了"ntent-Transfer-Encoding: binary" 导致文件打不开
并非名称非法,所有上传的文件都加了
ntent-Transfer-Encoding: binary 是说内容传输乱码
to LZTo myy上传文件的例子,还没测试//indy一定要用9.0.18版本,否则上传不成功。uses
IdHTTP,IdMultipartFormData, IdGlobal;//上传jpeg,返回值是服务器端保存的文件名称。
function UpLoagFile(FileName: String;http: TIdhttp): string;
var
obj : TIdMultiPartFormDataStream;
Url: String;
begin
obj := TIdMultiPartFormDataStream.Create;
try
obj.AddFile('Image',FileName, GetMIMETypeFromFile(FileName));
http.Request.ContentType := obj.RequestContentType;
obj.Position := 0;
Url := 'http://192.168.1.49/insertImage.aspx'; //这个页面负责接收文件
try
Result := http.Post(Url, obj);
except
on E: Exception do
begin
Application.MessageBox(PChar('上传文件失败,错误原因:' + E.Message), ('错误'), MB_OK + MB_ICONERROR);
Result := '';
end;
end;
finally
obj.Free;
end;
end;