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.

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM