从其它平台迁移而来


/test.txt

  1. 打开上节的工程

  2. 添加组件RtcDataProvider3并设置Server属性为RtcHttpServer1,设置CheckOrder属性为900,使得RtcDataProvider3所处理的请求在其它请求之后(CheckOrder越小越先处理)

  3. 在当前exe所在路径下创建一个data文件夹,并在该文件夹内新建一个有内容的test.txt,然后编写一个GetFullFileName函数,用于从请求中提取文件名并转化为本地文件名

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
function GetFullFileName(fname: string): string;
var
  DocRoot: string;
begin
  DocRoot := ExtractFilePath(AppFileName);
  if Copy(DocRoot, length(DocRoot), 1) = '\' then
    Delete(DocRoot, length(DocRoot), 1);
  DocRoot := DocRoot + '\data';
  fname := StringReplace(fname, '/', '\', [rfreplaceall]);
  Result := ExpandFileName(DocRoot + fname);
  if UpperCase(Copy(Result, 1, length(DocRoot))) <> UpperCase(DocRoot) then
    Result := '';
end;
  1. RtcDataProvider3OnCheckRequest事件中写上代码:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
var
  fname: string;
begin
  with TRtcDataServer(Sender) do
  begin
    fname := GetFullFileName(Request.FileName);
    if (fname <> '') and (File_Exists(fname)) then
    begin
      Accept;
      Request.Info['fname'] := fname;
    end;
  end;
end;
  1. RtcDataProvider3OnDataReceived事件中写上代码:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
procedure TForm1.RtcDataProvider3DataReceived(Sender: TRtcConnection);
var
  fname: string;
begin
  with TRtcDataServer(Sender) do
    if Request.Complete then
    begin
      fname := Request.Info['fname'];
      if File_Exists(fname) then
        Write(Read_File(fname))
      else
        Write;
    end;
end;
  1. 编译并运行

  2. 打开浏览器,访问网址http://localhost/test.txt