Delphi 文件转换Base64、Base64转换文件

 

相关资料:

https://www.cnblogs.com/h2285409/p/11714576.html

http://www.delphitop.com/html/jiami/4213.html

https://www.cnblogs.com/findumars/p/5789404.html

 

unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Memo1: TMemo;
    Memo2: TMemo;
    Button3: TButton;
    Edit1: TEdit;
    OpenDialog1: TOpenDialog;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation
uses
  EncdDecd;
{$R *.dfm}

function FileToBase64(FileName: string): string;
var
  MemoryStream: TMemoryStream;
begin
  Result := '';
  MemoryStream := TMemoryStream.Create;
  try
    MemoryStream.LoadFromFile(FileName);
    Result := EncodeBase64(MemoryStream.Memory, MemoryStream.Size);
    //base64默认有一个77字符后换行一次,用TCP发送时不方便,就去掉了回车换行。
    Result := StringReplace(Result, #13, '', [rfReplaceAll]);
    Result := StringReplace(Result, #10, '', [rfReplaceAll]);
  finally
    MemoryStream.Free;
  end;
end;

procedure Base64ToFile(AStr: string);
var
  MemoryStream: TMemoryStream;
begin
  MemoryStream := TMemoryStream.Create;
  try
    MemoryStream.Write(DecodeBase64(AStr), Length(DecodeBase64(AStr)));
    MemoryStream.SaveToFile('d:\11.txt');
  finally
    MemoryStream.Free;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  if OpenDialog1.Execute() then
  begin
    Edit1.Text :=  OpenDialog1.FileName;
  end;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  Memo1.Text := FileToBase64(Edit1.Text);
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
  Base64ToFile(Memo1.Text);
end;

end.

 

posted on 2020-02-10 13:02  疯狂delphi  阅读(2600)  评论(0编辑  收藏  举报

导航