使用Delphi 2007将Base64字符串作为二进制文件保存到磁盘 [英] Saving a Base64 string to disk as a binary using Delphi 2007

查看:881
本文介绍了使用Delphi 2007将Base64字符串作为二进制文件保存到磁盘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Base64二进制字符串,它是由第三方供应商发送给我们的XML文档的一部分,我希望能够将其保存回原始文件格式(jpg)。



使用这个问题接受的答案将base64字符串作为使用php的二进制文件保存到磁盘我可以很少的努力将字符串保存到jpg,所以我知道字符串是一个好的形式,是一个JPG文件。



但是如何在Delphi 2007中执行此操作?



在网上看到我发现了一个关于如何将Base64转换为TByteDynArray的教程,并保存,但它不正常。我也曾与Indy的IdDecoderMIME打交道,但没有成功。



有没有人知道如何做,或者我应该在看什么?

解决方案

OmniXMLUtils.pas从 OmniXML 项目包含以下功能:

 功能Base64Decode(编码,解码:TStream):boolean;超载; 
函数Base64Decode(const encoded:string; decoding:TStream):boolean;超载;
函数Base64Decode(const encoded:string; var decoding:string):boolean;超载;
函数Base64Decode(const encoded:string):string;超载;
程序Base64Encode(解码,编码:TStream);超载;
程序Base64Encode(解码:TStream; var encoded:string);超载;
功能Base64Encode(解码:TStream):string;超载;
函数Base64Encode(const decode:string):string;超载;
procedure Base64Encode(const decoding:string; var encoded:string);超载;

Base64Decode(string,TStream)应该做的伎俩。对于TStream参数,您可以像这样传递它的TFileStream:

  procedure SaveBase64ToFile(const encoded,fileName:string); 
var
fs:TFileStream;
begin
fs:= TFileStream.Create(fileName,fmCreate);
尝试
如果不是Base64Decode(编码,fs)然后
raise Exception.Create('Invalid data!');
finally FreeAndNil(fs);结束;
结束


I have a Base64 binary string that is part of an XML document that is sent to us by a 3rd party supplier, I would like to be able to save it back to its original file format (jpg).

Using the accepted answer from this question "saving a base64 string to disk as a binary using php" I can save the string to a jpg with little effort, so I know the string is in good form and is a JPG file.

But how do I do this in Delphi 2007?

Looking on the net I found a tutorial on how to convert the Base64 into a TByteDynArray, and save that, but it doesn't work right. I have also played about with Indy's IdDecoderMIME, but with with no success.

Does any one know how to do this, or where I should be looking?

解决方案

OmniXMLUtils.pas from the OmniXML project contains following functions:

function  Base64Decode(encoded, decoded: TStream): boolean; overload;
function  Base64Decode(const encoded: string; decoded: TStream): boolean; overload;
function  Base64Decode(const encoded: string; var decoded: string): boolean; overload;
function  Base64Decode(const encoded: string): string; overload;
procedure Base64Encode(decoded, encoded: TStream); overload;
procedure Base64Encode(decoded: TStream; var encoded: string); overload;
function  Base64Encode(decoded: TStream): string; overload;
function  Base64Encode(const decoded: string): string; overload;
procedure Base64Encode(const decoded: string; var encoded: string); overload;

The Base64Decode(string, TStream) should do the trick. For the TStream parameter you can pass it TFileStream like this:

procedure SaveBase64ToFile(const encoded, fileName: string);
var
  fs: TFileStream;
begin
  fs := TFileStream.Create(fileName, fmCreate);
  try
    if not Base64Decode(encoded, fs) then
      raise Exception.Create('Invalid data!');
  finally FreeAndNil(fs); end;
end;

这篇关于使用Delphi 2007将Base64字符串作为二进制文件保存到磁盘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆