Author Topic: About upload a file to HFS by a Delphi Client  (Read 5365 times)

0 Members and 1 Guest are viewing this topic.

Daniel Z

  • Guest
About upload a file to HFS by a Delphi Client
« on: August 29, 2007, 06:40:11 AM »
i want to develop a client software, upload a file to HFS,
someone can give me a sample code for that. Thanks a lot.

Offline rejetto

  • Administrator
  • Insane programmer
  • *
  • Posts: 12844
    • View Profile
Re: About upload a file to HFS by a Delphi Client
« Reply #1 on: August 29, 2007, 03:42:28 PM »
find an HTTP lib for your programming language first.

Offline DanielZ

  • Occasional poster
  • *
  • Posts: 6
    • View Profile
Re: About upload a file to HFS by a Delphi Client
« Reply #2 on: August 30, 2007, 02:29:54 AM »
hi, rejetto, thanks for your reply. i used WinINet.
this is my code, but dosen't work, Plz help me again, thank you.

  ALocalFile:= 'D:\upload\111.txt';
  ARemoteServer:= '127.0.0.1';
  ARemoteFile:= '/101_111.txt';

  HttpNet := InternetOpen(PChar(ARemoteServer), INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0);
  HttpConnect := InternetConnect(HttpNet, PChar(ARemoteServer), 0, nil, nil, INTERNET_SERVICE_HTTP, 0, 0);
  HttpRequest := HttpOpenRequest(HttpConnect, 'PUT', PChar(ARemoteFile), nil, nil, nil, 0, 0);

  hFile := CreateFile(PChar(ALocalFile), GENERIC_READ, FILE_SHARE_READ, nil, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
  FillChar(BufferIn, SizeOf(BufferIn), 0);
  BufferIn.dwStructSize := SizeOf(BufferIn);
  BufferIn.dwBufferLength := cBuffsize;
  BufferIn.dwBufferTotal := GetFileSize(hFile, nil);
  HttpSendRequestEx(HttpRequest, @BufferIn, nil, HSR_INITIATE, 0);

  ReadInF := 0;
  repeat
    SetLength(pBuffer, cBuffsize);
    ReadFile(hFile, Pointer(pBuffer)^, cBuffsize, Count, nil);
    ReadInF := ReadInF + Count;
    InternetWriteFile(httpRequest, Pointer(pBuffer), Count, oBytesWritten);
  until ReadInF = BufferIn.dwBufferTotal;

  HttpEndRequest(HttpRequest, nil, 0, 0);
  InternetCloseHandle(HttpRequest);
  InternetCloseHandle(HttpConnect); 
  InternetCloseHandle(HttpNet);       


Offline rejetto

  • Administrator
  • Insane programmer
  • *
  • Posts: 12844
    • View Profile
Re: About upload a file to HFS by a Delphi Client
« Reply #3 on: August 30, 2007, 09:42:28 AM »
i don't know the lib you are using, but you don't have to use the PUT method.
use the POST method towards the ~upload page.

Offline DanielZ

  • Occasional poster
  • *
  • Posts: 6
    • View Profile
Re: About upload a file to HFS by a Delphi Client
« Reply #4 on: August 30, 2007, 06:06:56 PM »
thank you again!
and i follow you to used a IdHTTP send a file to server.

var
    res   :   String;
    mps   :   TIdMultiPartFormDataStream;
begin
    try
        mps   :=   TIdMultiPartFormDataStream.Create;
        mps.AddFile('File1','D:\222.txt','');
        res:=idhttp1.Post('http://192.168.1.100/test/~upload', mps);
    finally
        mps.Free;
    end;

the HFS is already reciving that file, but there is an error :
  Upload failed, Folder not found: 222.txt
how to fix that, thanks!

Offline rejetto

  • Administrator
  • Insane programmer
  • *
  • Posts: 12844
    • View Profile
Re: About upload a file to HFS by a Delphi Client
« Reply #5 on: August 30, 2007, 07:27:30 PM »
are you sure the test folder does exist?

Offline DanielZ

  • Occasional poster
  • *
  • Posts: 6
    • View Profile
Re: About upload a file to HFS by a Delphi Client
« Reply #6 on: August 30, 2007, 11:57:03 PM »
yes, I am sure
and the test folder is a real folder.
and everyone can upload file to the test folder, i already test used web, it's ok.

Offline rejetto

  • Administrator
  • Insane programmer
  • *
  • Posts: 12844
    • View Profile
Re: About upload a file to HFS by a Delphi Client
« Reply #7 on: August 31, 2007, 09:17:18 AM »
sorry, i was wrong.
you have to post to the folder, not the ~upload page.

anyway, while testing your procedure to find what was wrong, i found some bugs in the upload handling.
if we are lucky this may fix an old terrific bug.

so, use the last beta for your tests.

Offline DanielZ

  • Occasional poster
  • *
  • Posts: 6
    • View Profile
Re: About upload a file to HFS by a Delphi Client
« Reply #8 on: September 01, 2007, 08:02:04 AM »
i tried it used the latest beta version 128#.
but there is a new problem.
the connection cannot be auto disconnected when the transfer finished.
It will be disconnected until the time out.
The file can be uploaded to server successfully but the size is wrong. Destination file size more than source file about 2-3bit.

Offline rejetto

  • Administrator
  • Insane programmer
  • *
  • Posts: 12844
    • View Profile
Re: About upload a file to HFS by a Delphi Client
« Reply #9 on: September 01, 2007, 08:11:53 AM »
there's no need to disconnect,
but you can break the connection if you really want to.

make a test so that i can see the file size problem on my computer.

Offline DanielZ

  • Occasional poster
  • *
  • Posts: 6
    • View Profile
Re: About upload a file to HFS by a Delphi Client
« Reply #10 on: September 02, 2007, 04:36:43 AM »
hi, there is the test.

Offline rejetto

  • Administrator
  • Insane programmer
  • *
  • Posts: 12844
    • View Profile
Re: About upload a file to HFS by a Delphi Client
« Reply #11 on: September 04, 2007, 11:35:07 PM »
fixed in next build

Offline DanielZ

  • Occasional poster
  • *
  • Posts: 6
    • View Profile
Re: About upload a file to HFS by a Delphi Client
« Reply #12 on: September 06, 2007, 11:16:25 PM »
thanks a lot!
waitting for next version.
and i tried the #130 it's work well. :)