rejetto forum

About upload a file to HFS by a Delphi Client

Guest · 13 · 10758

0 Members and 1 Guest are viewing this topic.

Daniel Z

  • Guest
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
  • Tireless poster
  • *****
    • Posts: 13521
    • View Profile

Offline DanielZ

  • Occasional poster
  • *
    • Posts: 6
    • View Profile
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
  • Tireless poster
  • *****
    • Posts: 13521
    • View Profile
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
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
  • Tireless poster
  • *****
    • Posts: 13521
    • View Profile

Offline DanielZ

  • Occasional poster
  • *
    • Posts: 6
    • View Profile
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
  • Tireless poster
  • *****
    • Posts: 13521
    • View Profile
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
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
  • Tireless poster
  • *****
    • Posts: 13521
    • View Profile
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


Offline DanielZ

  • Occasional poster
  • *
    • Posts: 6
    • View Profile
thanks a lot!
waitting for next version.
and i tried the #130 it's work well. :)