filefunc.pas

/// <author>cxg 2020-9-29</author>

unit filefunc;

interface

uses
  Classes, ulog, serial, uGlobal, SysUtils;

function downfile(const params: RawByteString): RawByteString;

function upfile(const params: RawByteString): RawByteString;

implementation

function downfile(const params: RawByteString): RawByteString;
var
  requ: TSerial;
  filename: string;
  fs: TFileStream;
begin
  requ := TSerial.Create;
  fs := TFileStream.Create(DownPath + filename, fmOpenRead or fmShareExclusive);
  try
    try
      requ.loadFromRawByteString(params);
      filename := requ.force('filename').AsString;
      requ.clear;
      requ.force('return').AsBoolean := true;
      requ.force('file').AsStream := fs;
      Result := requ.saveToRawByteString;
    except
      on e: Exception do
      begin
        requ.clear;
        requ.force('return').AsBoolean := False;
        requ.force('err').AsString := e.Message;
        Result := requ.saveToRawByteString;
        Log.WriteLog('filefunc.downfile ' + e.Message);
      end;
    end;
  finally
    requ.Free;
    fs.Free;
  end;
end;

function upfile(const params: RawByteString): RawByteString;
var
  requ: TSerial;
  filename: string;
  ms: TStream;
begin
  requ := TSerial.Create;
  ms := TMemoryStream.Create;
  try
    try
      requ.loadFromRawByteString(params);
      filename := requ.force('filename').AsString;
      ForceDirectories(UpPath);
      ms := requ.force('file').AsStream;
      TMemoryStream(ms).SaveToFile(UpPath + filename);
      requ.clear;
      requ.force('return').AsBoolean := true;
      Result := requ.saveToRawByteString;
    except
      on e: Exception do
      begin
        requ.clear;
        requ.force('return').AsBoolean := False;
        Result := requ.saveToRawByteString;
        Log.WriteLog('filefunc.upfile ' + e.Message);
      end;
    end;  
  finally
    requ.Free;
    ms.Free;
  end;   
end;

end.

  

上一篇:误清空表的ibd文件时,该如何恢复?


下一篇:PHP实现无限极分类的两种方式,递归和引用