我试图在Delphi7中创建此功能
Function CreateBase64String(FileName:String):Base64String;
尝试过INDY 6-7和9 - 但是INDY(6-7--9)只给我一些错误。不知道为什么。
经过很多麻烦我终于做到了:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics,
Controls, Forms, Dialogs, ExtDlgs, StdCtrls, Jpeg;
type
TForm1 = class(TForm)
Edit1: TEdit;
Edit2: TEdit;
select: TButton;
Label1: TLabel;
Label2: TLabel;
ToB64: TButton;
OpenPictureDialog1: TOpenPictureDialog;
DecBtn: TButton;
Button1: TButton;
procedure FormCreate(Sender: TObject);
procedure ToB64Click(Sender: TObject);
procedure selectClick(Sender: TObject);
procedure DecBtnClick(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1 : TForm1;
Curdir : string;
implementation
Uses
EncdDeCd;
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
Curdir := ExtractfileDir(Application.ExeName);
end;
procedure TForm1.ToB64Click(Sender: TObject);
var
instream, oustream : TMemoryStream;
begin
instream := TMemoryStream.Create;
instream.LoadFromFile(Edit1.text);
oustream := TMemoryStream.Create;
EncodeStream(instream,oustream);
instream.Free;
oustream.SaveToFile(Edit2.Text);
oustream.Free;
end;
procedure TForm1.selectClick(Sender: TObject);
begin
Edit1.Text := '';
if OpenPictureDialog1.Execute then
begin
Edit1.Text := OpenPictureDialog1.FileName;
Edit2.Text := ChangeFileExt(Edit1.Text,'.b64');
end;
end;
procedure TForm1.DecBtnClick(Sender: TObject);
var
instream, oustream : TMemoryStream;
s : string;
begin
instream := TMemoryStream.Create;
instream.LoadFromFile(Edit2.text); //b64 file
oustream := TMemoryStream.Create;
DecodeStream(instream,oustream);
instream.Free;
s := Edit1.Text;
insert('x',s,pos('.',s)); // sæt et 'X' på org filen
oustream.SaveToFile(s);
oustream.Free;
end;
procedure TForm1.Button1Click(Sender: TObject);
VAR
Ofile : TextFile;
ifile : TextFile;
Istr : string;
begin
Edit1.Text := Curdir + '\sunset.jpg';
Edit2.Text := Curdir + '\sunset.b64';
ToB64Click(NIL);
AssignFile(Ofile,curdir + '\TESTABCDEF.HTML');
Rewrite(ofile);
Writeln(ofile,'<HTML><BODY>');
Write(ofile,'<img src="DATA:IMAGE/JPG; base64,');
Assignfile(ifile,edit2.Text);
Reset(ifile);
While not Eof(Ifile) DO
BEGIN
Readln(Ifile,Istr);
Write(ofile,Istr);
END;
Closefile(ifile);
Writeln(ofile, '" height="100" width="100" />');
Writeln(Ofile,'</body></html>');
Closefile(ofile);
DecBtnClick(NIL);
end;
END.
但无论我做什么,OFILE(TESTABCDEF.HTML)都不会在我的网络浏览器(Firefox和IE8)中正确显示。
AssignFile-stuff仅用于测试目的。通常我使用Streams。
尝试过大JPEG&#39; s&gt;错误 试过小JPEG&#39; s - &gt;错误!
有谁能告诉我我做错了什么?
克里斯/挪威答案 0 :(得分:1)
Indy的base64编码器工作得很好,你可能只是没有正确使用它。
Uses
..., IdCoderMIME;
Function CreateBase64String(FileName: String): String;
var
Fs: TFileStream;
begin
Fs := TFileStream.Create(FileName, fmOpenRead);
try
Result := TIdEncoderMIME.EncodeStream(Fs);
finally
Fs.Free;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
Ofile : TextFile;
begin
AssignFile(Ofile,curdir + '\TESTABCDEF.HTML');
Rewrite(ofile);
Writeln(ofile,'<HTML><BODY>');
Write(ofile,'<img src="data:image/jpeg;base64,');
Write(ofile, CreateBase64String(Curdir + '\sunset.jpg'));
Writeln(ofile, '" height="100" width="100" />');
Writeln(Ofile,'</body></html>');
Closefile(ofile);
end;
我甚至会删除TextFile
并使用TFileStream
代替,以便Indy可以将其base64输出直接写入目标文件而不会浪费内存将其放入String
首先:
Uses
..., IdCoderMIME;
procedure WriteBase64String(ADest: TStream; FileName: String);
var
Fs: TFileStream;
begin
Fs := TFileStream.Create(FileName, fmOpenRead);
try
TIdEncoderMIME.EncodeStream(Fs, ADest);
finally
Fs.Free;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
Ofile : TFileStream;
begin
Ofile := TFileStream.Create(curdir + '\TESTABCDEF.HTML', fmCreate);
try
// WriteStringToStream() is an Indy function...
WriteStringToStream(Ofile, '<HTML><BODY>'+EOL);
WriteStringToString(Ofile, '<img src="data:image/jpeg;base64,');
WriteBase64String(Ofile, Curdir + '\sunset.jpg');
WriteStringToStream(Ofile, '" height="100" width="100" />'+EOL);
WriteStringToStream(Ofile, '</body></html>'+EOL);
finally
Ofile.Free;
end;
end;