Can I load a memo or rich edit from a text file on server?

后端 未结 3 391
一生所求
一生所求 2021-01-15 05:36

I designed a website and its uploaded to server and is working fine. in one of these pages i get some info from users like their addresses and ... and save them to a text fi

3条回答
  •  长情又很酷
    2021-01-15 06:01

    Yes, you can.

    function WebGetData(const UserAgent: string; const Server: string; const Resource: string): AnsiString;
    var
      hInet: HINTERNET;
      hURL: HINTERNET;
      Buffer: array[0..1023] of AnsiChar;
      i, BufferLen: cardinal;
    begin
      result := '';
      hInet := InternetOpen(PChar(UserAgent), INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0);
      try
        hURL := InternetOpenUrl(hInet, PChar('http://' + Server + Resource), nil, 0, 0, 0);
        try
          repeat
            InternetReadFile(hURL, @Buffer, SizeOf(Buffer), BufferLen);
            result := result + AnsiString(Buffer);
            if BufferLen < SizeOf(Buffer) then
              SetLength(result, length(result) + BufferLen - SizeOf(Buffer));
          until BufferLen = 0;
        finally
          InternetCloseHandle(hURL);
        end;
      finally
        InternetCloseHandle(hInet);
      end;
    end;
    
    procedure TForm1.FormClick(Sender: TObject);
    begin
      Memo1.Text := WebGetData('My Application', 'www.rejbrand.se', '');
    end;
    

    Notice that the above code does only work with ASCII text. To obtain a UTF-8 solution, replace AnsiString with string in the signature, and replace the second line in the repeat block with

        result := result + UTF8ToString(AnsiString(Buffer));
    

    and tweak the SetLength.

提交回复
热议问题