其他分享
首页 > 其他分享> > filefunc.pas

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.

  

标签:pas,end,filefunc,requ,filename,RawByteString,params,force
来源: https://www.cnblogs.com/hnxxcxg/p/13751283.html