rest api上传和下载文件
function FileToString(AFileName: string): string; var LMemoryStream: TMemoryStream; LStringStream: TStringStream; LDstStream: TMemoryStream; begin Result := ''; if not FileExists(AFileName) then Exit; LMemoryStream := TMemoryStream.Create; LStringStream := TStringStream.Create; LDstStream := TMemoryStream.Create; try LMemoryStream.LoadFromFile(ExtractFilePath(ParamStr(0)) + AFileName); LMemoryStream.Position := 0; ZCompressStream(LMemoryStream, LDstStream); LDstStream.Position := 0; TNetEncoding.Base64.Encode(LDstStream, LStringStream); Result := LStringStream.DataString; finally LMemoryStream.Free; LStringStream.Free; LDstStream.Free; end; end;
procedure FileFromString(AFileName, AFileContent: string); var LStringStream: TStringStream; LMemoryStream: TMemoryStream; LOrgStream: TMemoryStream; begin if AFileName = '' then Exit; LStringStream := TStringStream.Create(AFileContent); LMemoryStream := TMemoryStream.Create; LOrgStream := TMemoryStream.Create; try LStringStream.Position := 0; TNetEncoding.Base64.Decode(LStringStream, LOrgStream); LOrgStream.Position := 0; ZDecompressStream(LOrgStream, LMemoryStream); LMemoryStream.Position := 0; LMemoryStream.SaveToFile(ExtractFileName(AFileName)); finally LStringStream.Free; LMemoryStream.Free; LOrgStream.Free; end; end;