procedure TForm1.Button1Click(Sender: TObject);
var
  MutPartForm: TIdMultiPartFormDataStream;
  sFile : string;
  response: string;
  FHttp: Tidhttp;
begin
    sFile := 'c:\fdsa.rar';
    FHttp := Tidhttp.Create(nil);
    FHttp.HandleRedirects := true;
    FHttp.AllowCookies := true;    MutPartForm := TIdMultiPartFormDataStream.Create;
    MutPartForm.AddFormField('file1', 'filename="' + sFile + '"');
    MutPartForm.AddFile('file1', sFile, 'text/plain');
    try
        response := FHttp.Post(ServerUrl, MPF);
        Messagebox(0, PAnsiChar(response), 'ok', MB_OK);
    finally
        MPF.Free;
        FHttp.Free;
    end;
end;

解决方案 »

  1.   

    上面写错了! 应该是procedure TForm1.Button1Click(Sender: TObject);
    var
      MutPartForm: TIdMultiPartFormDataStream;
      sFile : string;
      response: string;
      FHttp: Tidhttp;
    begin
        sFile := 'c:\fdsa.rar';
        FHttp := Tidhttp.Create(nil);
        FHttp.HandleRedirects := true;
        FHttp.AllowCookies := true;    MutPartForm := TIdMultiPartFormDataStream.Create;
        MutPartForm.AddFormField('file1', 'filename="' + sFile + '"');
        MutPartForm.AddFile('file1', sFile, 'text/plain');
        try
            response := FHttp.Post('http://127.0.0.1/1.asp', MutPartForm);
            Messagebox(0, PAnsiChar(response), 'ok', MB_OK);
        finally
            MutPartForm.Free;
            FHttp.Free;
        end;
    end;
      

  2.   

    unit Unit2;interfaceuses
      Classes,IdMultipartFormData,IdHTTP,Dialogs;type
      TMyThread = class(TThread)
      private
        { Private declarations }
        sFile :string;
        sUrl  :string;
      protected
        procedure Execute; override;
      public
        constructor create(filename,myurl:string);     {线程构造函数}
      end;implementation{ TMyThread }constructor TMyThread.create(filename, myurl: string);
    begin
      inherited Create(false);
      sFile:=filename;
      sUrl:=myurl;
      FreeOnTerminate:=true;
    end;procedure TMyThread.Execute;
    var
      MutPartForm: TIdMultiPartFormDataStream;
      //sFile : string;
      response: string;
      FHttp: Tidhttp;
      { Place thread code here }
    begin
        //sFile := 'c:\fdsa.rar';
        FHttp := Tidhttp.Create(nil);
        FHttp.HandleRedirects := true;
        FHttp.AllowCookies := true;    MutPartForm := TIdMultiPartFormDataStream.Create;
        MutPartForm.AddFormField('file1', 'filename="' + sFile + '"');
        MutPartForm.AddFile('file1', sFile, 'text/plain');
        try
            //response := FHttp.Post('http://127.0.0.1/1.asp', MutPartForm);
            //Messagebox(0, PAnsiChar(response), 'ok', MB_OK);
            response := FHttp.Post(sUrl, MutPartForm);
        finally
            MutPartForm.Free;
            FHttp.Free;
        end;
    end;end.
    //调用:
    procedure TForm1.Button1Click(Sender: TObject);
    var
        Thread1:TMyThread;
    begin
       Thread1 :=TMyThread.create('c:\fdsa.rar','http://127.0.0.1/1.asp');
    end;