首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > .NET >

idhttp上传文件的有关问题

2012-03-27 
idhttp上传文件的问题?varresponse: TStringStreamMMPFDS: TIdMultiPartFormDataStreamidhttp1: TIDhttp

idhttp上传文件的问题?
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" 导致文件打不开



[解决办法]
1.indy9 的某些版本TIdMultiPartFormDataStream有bug 
ntent-Transfer-Encoding: binary 是说内容传输乱码
to LZ

To 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;

热点排行