码迷,mamicode.com
首页 > Windows程序 > 详细

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

时间:2020-02-10 13:29:10      阅读:78      评论:0      收藏:0      [点我收藏+]

标签:ant   ctr   div   有一个   eal   cut   create   ext   classes   

 

相关资料:

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.

 

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

标签:ant   ctr   div   有一个   eal   cut   create   ext   classes   

原文地址:https://www.cnblogs.com/FKdelphi/p/12290576.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!