Here is a sample that work.

Get the 'multipart/form-data' encode tstream class code from this unit:
http://read.pudn.com/downloads116/sourcecode/internet/495621/Gz235_200671623235689/cn700_del549790557/IndyMultipartFormData/MsMultiPartFormData.pas__.htm

Start a new Delphi application, and in the main form place:

1 THttpCli,
1 TButton,
1 TMemo
1 TOpenDialog

   function GetMimeType(fileName: string): string;
   var reg: tregistry;
      ext: string;
   begin
      result := 'application/unknown';
      ext := lowercase(extractFileExt(filename));
      reg := tregistry.Create;
      try
        reg.RootKey := HKEY_CLASSES_ROOT;
        if reg.OpenKey(ext, false) and reg.ValueExists('Content Type') then
          result := reg.ReadString('Content Type')
      finally
        reg.Free;
      end;
   end;

   procedure TForm1.UploadFile(url, Filename: string);
   var
      MultiPartFormDataStream: TMsMultiPartFormDataStream;
      fStream: TFileStream;
   begin
      fStream := TFileStream.Create(Filename, fmOpenRead);
      MultiPartFormDataStream := TMsMultiPartFormDataStream.create;
      MultiPartFormDataStream.AddFile('fileupload1',
   ExtractFileName(filename), GetMimeType(Filename), fstream);
      fStream.free;
      //there is a bug in the HFS code so we need to add a dummy field here
      MultiPartFormDataStream.AddFormField('fileupload2', '');
      MultiPartFormDataStream.PrepareStreamForDispatch;
      MultiPartFormDataStream.Position := 0;
      Memo1.Lines.LoadFromStream(MultiPartFormDataStream);
      MultiPartFormDataStream.Position := 0;
      HttpCli1.ContentTypePost := 'multipart/form-data;
   boundary='+MultiPartFormDataStream.Boundary;
      HttpCli1.SendStream := MultiPartFormDataStream;
      HttpCli1.URL := url;
      HttpCli1.Post;
      HttpCli1.SendStream.free;
   end;

   procedure TForm1.Button1Click(Sender: TObject);
   begin
      if OpenDialog1.Execute then
        UploadFile('http://81.184.21.22:100/hfs/', OpenDialog1.FileName);
   end;

I have just now upload a file with this code to your HFS server.

--
To unsubscribe or change your settings for TWSocket mailing list
please goto http://lists.elists.org/cgi-bin/mailman/listinfo/twsocket
Visit our website at http://www.overbyte.be

Reply via email to