memorystream base64
function BaseImage(fn: string): string;
var m1: TMemoryStream; m2: TStringStream; str: string;begin m1 := TMemoryStream.Create; m2 := TStringStream.Create; m1.LoadFromFile(fn); EncdDecd.EncodeStream(m1, m2); // 将m1的内容Base64到m2中 str := m2.DataString; str := StringReplace(str, #13, '', [rfReplaceAll]); // 这里m2中数据会自动添加回车换行,所以需要将回车换行替换成空字符 str := StringReplace(str, #10, '', [rfReplaceAll]); result := str; // 返回值为Base64的Stream m1.Free; m2.Free;end;filestream base64
需要uses IdCoderMIME;
function TForm1.Base64E(Path: string): string;
var
filepath: string;
filestream: TFileStream;
EncoderMIME: TIdEncoderMIME;
encode_result: string;
begin
EncoderMIME := TIdEncoderMIME.Create(self);
filestream := TFileStream.Create(Path, fmOpenRead);
result := EncoderMIME.Encode(filestream);
filestream.Free;
end;