Delphi 文件转换Base64Base64转换文件
Posted fkdelphi
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Delphi 文件转换Base64Base64转换文件相关的知识,希望对你有一定的参考价值。
相关资料:
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 文件转换Base64Base64转换文件的主要内容,如果未能解决你的问题,请参考以下文章