2

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 file. Can i make an application and load a rich edit or memo from that file? that file has itself address like www.mysite.com/my_text_File.txt thank you for your help.

none
  • 4,669
  • 14
  • 62
  • 102
Armin Taghavizad
  • 1,625
  • 7
  • 35
  • 57
  • Make sure your text files are not stored in any Unicode format (should not be a problem, as you have control over them, but since Delphi 7 does not support Unicode and the web becomes more and more Unicode, you should be aware before using this code outside your own website). – Jeroen Wiert Pluimers Jan 04 '11 at 15:39

3 Answers3

3

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.

Andreas Rejbrand
  • 105,602
  • 8
  • 282
  • 384
3

Drop a TMemo or TRichedit on the form of your application. Then drop a TidHTTP component from the Indy components.

add a onclick button event event and do the following:

procedure TForm1.Button1Click(Sender: TObject);
begin
memo1.lines.Text:= idHttp1.Get('http://www.delphiprojectcode.com/test.txt');
end;

OR

procedure TForm1.Button1Click(Sender: TObject);
begin
richedit1.Text:= idHttp1.Get('http://www.delphiprojectcode.com/test.txt');
end;

IElite
  • 1,818
  • 9
  • 39
  • 64
0

Both TRichEdit and TMemo load the data from string you pass to them. So what you need to do in your client-side application is download the text file (probably using HTTP client, Indy's one is one of the options) and pass it's contents to TRichEdit or TMemo (via Text property in TMemo, and corresponding mechanisms in TRichEdit).

Eugene Mayevski 'Callback
  • 45,135
  • 8
  • 71
  • 121