rejetto forum

Software => HFS ~ HTTP File Server => Topic started by: Daniel Z on August 29, 2007, 12:40:11 PM

Title: About upload a file to HFS by a Delphi Client
Post by: Daniel Z on August 29, 2007, 12:40:11 PM
i want to develop a client software, upload a file to HFS,
someone can give me a sample code for that. Thanks a lot.
Title: Re: About upload a file to HFS by a Delphi Client
Post by: rejetto on August 29, 2007, 09:42:28 PM
find an HTTP lib for your programming language first.
Title: Re: About upload a file to HFS by a Delphi Client
Post by: DanielZ on August 30, 2007, 08: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);       

Title: Re: About upload a file to HFS by a Delphi Client
Post by: rejetto on August 30, 2007, 03:42:28 PM
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.
Title: Re: About upload a file to HFS by a Delphi Client
Post by: DanielZ on August 31, 2007, 12:06:56 AM
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!
Title: Re: About upload a file to HFS by a Delphi Client
Post by: rejetto on August 31, 2007, 01:27:30 AM
are you sure the test folder does exist?
Title: Re: About upload a file to HFS by a Delphi Client
Post by: DanielZ on August 31, 2007, 05:57:03 AM
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.
Title: Re: About upload a file to HFS by a Delphi Client
Post by: rejetto on August 31, 2007, 03:17:18 PM
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 (http://www.rejetto.com/forum/?topic=4606).

so, use the last beta for your tests.
Title: Re: About upload a file to HFS by a Delphi Client
Post by: DanielZ on September 01, 2007, 02:02:04 PM
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.
Title: Re: About upload a file to HFS by a Delphi Client
Post by: rejetto on September 01, 2007, 02:11:53 PM
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.
Title: Re: About upload a file to HFS by a Delphi Client
Post by: DanielZ on September 02, 2007, 10:36:43 AM
hi, there is the test.
Title: Re: About upload a file to HFS by a Delphi Client
Post by: rejetto on September 05, 2007, 05:35:07 AM
fixed in next build
Title: Re: About upload a file to HFS by a Delphi Client
Post by: DanielZ on September 07, 2007, 05:16:25 AM
thanks a lot!
waitting for next version.
and i tried the #130 it's work well. :)