Saving a Base64 string to disk as a binary using Delphi 2007

北城以北 提交于 2019-12-01 01:32:56

OmniXMLUtils.pas from the OmniXML project contains following functions:

function  Base64Decode(encoded, decoded: TStream): boolean; overload;
function  Base64Decode(const encoded: string; decoded: TStream): boolean; overload;
function  Base64Decode(const encoded: string; var decoded: string): boolean; overload;
function  Base64Decode(const encoded: string): string; overload;
procedure Base64Encode(decoded, encoded: TStream); overload;
procedure Base64Encode(decoded: TStream; var encoded: string); overload;
function  Base64Encode(decoded: TStream): string; overload;
function  Base64Encode(const decoded: string): string; overload;
procedure Base64Encode(const decoded: string; var encoded: string); overload;

The Base64Decode(string, TStream) should do the trick. For the TStream parameter you can pass it TFileStream like this:

procedure SaveBase64ToFile(const encoded, fileName: string);
var
  fs: TFileStream;
begin
  fs := TFileStream.Create(fileName, fmCreate);
  try
    if not Base64Decode(encoded, fs) then
      raise Exception.Create('Invalid data!');
  finally FreeAndNil(fs); end;
end;

The Internet Direct (Indy) library contains classes in IdCoderMIME.pas which support Base64 encoding and are easy to use: TIdEncoderMIME and TIdDecoderMIME.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!