DATASNAP双缓存下载文件

原文链接: http://www.cnblogs.com/hnxxcxg/archive/2012/12/29/2839358.html

procedure TFrmMain.btnUpdateFilesClick(Sender: TObject);
begin
  try
    if Assigned(gEXEmm) then
      FreeAndNil(gEXEmm);
    if Assigned(gINImm) then
      FreeAndNil(gINImm);

    gEXEmm := TFileStream.Create(ExtractFilePath(Application.ExeName) +
      'download\jlpos.exe', fmOpenRead);
    gINImm := TFileStream.Create(ExtractFilePath(Application.ExeName) +
      'download\client.ini', fmOpenRead);

    gEXEmm2.Clear;
    gINImm2.Clear;
    gEXEmm2.CopyFrom(gEXEmm, 0);
    gINImm2.CopyFrom(gINImm, 0);

    FreeAndNil(gEXEmm);
    FreeAndNil(gINImm);
  except
    on E: Exception do
    begin
      gSysLog.WriteLog('btnUpdateFilesClick: ' + E.Message);
      Exit;
    end;
  end;
end;

function TServerMethods1.DownLoadFile(const FileName: string): TStream;
var
  f: string;
begin
  Result := nil;
  try
    f := ExtractFilePath(Application.ExeName) + 'download\' + FileName;
    if not FileExists(f) then
      Exit;
    Result := TMemoryStream.Create;
    if LowerCase(FileName) = 'jlpos.exe' then
      Result.CopyFrom(gEXEmm2, 0)
    else if LowerCase(FileName) = 'client.ini' then
      Result.CopyFrom(gINImm2, 0);
    Result.Position := 0;
  except
    on E: Exception do
    begin
      gSysLog.WriteLog('DownLoadFile: ' + E.Message);
      Exit;
    end;
  end;
end;

function TdmCommonFun.DownLoadFile(const FileName: string): Boolean;
var
  a: TServerMethods1Client;
  ini: TIniFile;
  Stream, ms: TStream;
  Buffer: TBytes;
  ReadCount: Integer;
const
  BufSize = $F000;
begin
  Result := False;
  if (not TryConnectAPPServer) or (FileName = '') then
    Exit;
  a := TServerMethods1Client.Create(SQLConnection1.DBXConnection);
  ms := TMemoryStream.Create;
  try
    Stream := a.DownLoadFile(FileName);
    if Stream.Size = -1 then
    begin
      SetLength(Buffer, BufSize);
      repeat
        ReadCount := Stream.Read(Buffer[0], BufSize);
        if ReadCount > 0 then
          ms.WriteBuffer(Buffer[0], ReadCount);
        if ReadCount < BufSize then
          break;
      until ReadCount < BufSize;
    end
    else
    begin
      ms.CopyFrom(Stream, 0);
    end;
    // delete bak files
    if FileExists(ExtractFilePath(Application.ExeName) + FileName + 'bak') then
      DeleteFile(PWideChar(ExtractFilePath(Application.ExeName) + FileName
        + 'bak'));
    // 现有文件改名
    if FileExists(ExtractFilePath(Application.ExeName) + FileName) then
    begin
      RenameFile(ExtractFilePath(Application.ExeName) + FileName,
        ExtractFilePath(Application.ExeName) + FileName + 'bak');
    end;
    // 下载最新文件
    TMemoryStream(ms).SaveToFile(ExtractFilePath(Application.ExeName) +
      FileName);
    // 更新本机版本号
    ini := TIniFile.Create(ExtractFilePath(Application.ExeName) + 'client.ini');
    try
      ini.WriteInteger(FileName, 'ver', GetVer(FileName));
    finally
      ini.Free;
    end;
  finally
    a.Free;
    ms.Free;
  end;
  Result := True;
end;

转载于:https://www.cnblogs.com/hnxxcxg/archive/2012/12/29/2839358.html

转载于:https://www.cnblogs.com/railgunman/p/11456331.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值