从base64编码的字符串解码文件到流。怎么做? [英] Decode file to stream, from base64 encoded string. How to do that?

查看:1069
本文介绍了从base64编码的字符串解码文件到流。怎么做?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串,由我的应用程序从网络浏览器中获取。字符串包含PNG图像,并编码为base64。

I have a string, recived by my app from web-browser. String contains PNG image, and encoded to base64.

这不是我的愚蠢想法,将图像转换为base64字符串))这是做web-broser。
HTML5画布对象,当需要将图像发送到某处时。

It isn't my stupid idea, convert image to base64 string )) That doing web-broser. HTML5 canvas object doing that, when need to send image to somewhere.

那么,如何将此字符串解码为图像?

So, how to decode this string to image?

在Delphi中,我编写了我的Web服务器,它可以传输数据。

In Delphi I written my web-server, that recives data.

在PHP中,这可以像那个

In PHP this can be solved like that:

function base64_to_jpeg( $base64_string, $output_file ) {
    $ifp = fopen( $output_file, "wb" ); 
    fwrite( $ifp, base64_decode( $base64_string) ); 
    fclose( $ifp ); 
    return( $output_file ); 
}

$image = base64_to_jpeg( $my_base64_string, 'tmp.jpg' );

但是在delphi中,解码后:

but in delp after decoding:

  if (sImg <> EmptyStr) then
  begin
    Coder := TIdDecoderMIME.Create(nil);
    MS := TMemoryStream.Create;
    try
      Coder.DecodeStream(sImg, MS);
      MS.SaveToFile(myDir + '1.png');
    finally
      FreeAndNil(MS);
      FreeAndNil(Coder);
    end;
  end;

我有一个黑色方格的Malevich。

I have a black square of Malevich.

我还尝试了RTL的EncdDesd模块中的函数,

Also I tried functions from EncdDesd module of RTL,

  if (sImg <> EmptyStr) then
  begin
    MS := TMemoryStream.Create;
    SS := TStringStream.Create(sImg);
    try
      DecodeStream(SS, MS);
      MS.SaveToFile(myDir + '1.png');
    finally
      FreeAndNil(MS);
      FreeAndNil(SS);
    end;
  end;

我得到了相同的结果。

I got the same result.

我写了一个web扩展来从用户web浏览器和使用TidHTTPServer的web服务器获取一些信息。
来自JS我捕获网页截图,并从exe中删除一些数据,之后,我将此字符串发送到服务器:

I wrote web-extension for get some info from user web-browser, and web-server using TidHTTPServer. From JS I capture the web-screenshot, and remove some data from exe, after that, I send this string to the server:

var Img = capture().toDataURL();
var Img = Img.replace('data:image/png;base64,','');

     if (FLA.AddNewURL(sN,sD,sU,sI,Img)) {
      window.close();
     } else {
      alert('can not establish connection!');
     };

...

AddNewURL: function (aName, aDesc,sURL, aOwnerId,aImg) {
    var http = new XMLHttpRequest();
    var params = 'Owner=' + aOwnerId + '&Name=' + aName + '&Desc=' + aDesc+ '&URL=' + sURL;
    if (aImg) {
      params = params +'&Img='+aImg;
    };
        http.open("POST", "http://" + this.host + ":" + this.port + "/addurl", false);
        http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
        http.send(params);
    return (http.status === 200);
},

我的网络服务器像这样回收这些数据:

My web-server recives this data like that:

  if (ARequestInfo.Document = '/addurl') then
  begin
    Id := ARequestInfo.Params.Values['Owner'];
    sName := ARequestInfo.Params.Values['Name'];
    sDesc := ARequestInfo.Params.Values['Desc'];
    sURL := ARequestInfo.Params.Values['URL'];
    sImg := ARequestInfo.Params.Values['Img'];
    RootLI := nil;
    LinksManager.FindByID(Id, RootLI);
    if Assigned(RootLI) then
    begin
      LI := TLinkItem.Create(nil);
      LI.name := sName;
      LI.Description := sDesc;
      LI.URL := sURL;
      if (sImg <> EmptyStr) then
      begin
        MS := TMemoryStream.Create;
        SS := TStringStream.Create(sImg);
        try
          DecodeStream(SS, MS);
          MS.SaveToFile(myDir + '1.png');
        finally
          FreeAndNil(MS);
          FreeAndNil(SS);
        end;
      end;
      RootLI.Add(LI, True);
      AResponseInfo.ResponseText := 'OK';
    end
    else
      AResponseInfo.ResponseText := 'FAIL';
  end;

完成所有这些后,我只有黑色。

After all of that I have image, that contains only black color.

推荐答案

我通过其他方式解决了我的问题。
在JS中,我通过头文件(Ajax.setRequestHeader)发送数据,以及我在POST流中发送的文件。
在DecodeStream之后,从EncdDecd工作正常,我从字符串中获取了图像。

I solved my problem by other way. In JS I send data via headers(Ajax.setRequestHeader), and the file I send in the POST stream. After that DecodeStream, from EncdDecd is working correctly, I got the image from string.

        Id := ARequestInfo.RawHeaders.Values['OwnerId'];
        sName := ARequestInfo.RawHeaders.Values['Name'];
        sDesc := ARequestInfo.RawHeaders.Values['Desc'];
        sURL := ARequestInfo.RawHeaders.Values['URL'];
...
          if (ARequestInfo.PostStream <> nil) then //We have the image!
          begin
            MS := TMemoryStream.Create;
            MS2 := TMemoryStream.Create;
            try
              MS2.LoadFromStream(ARequestInfo.PostStream);
              DecodeStream(MS2, MS);
              MS.SaveToFile(myDir + '1.png');
            finally
              FreeAndNil(MS);
              FreeAndNil(MS2);
            end;
          end;

您也可以尝试混合使用其他多部分/表格数据编码。但Indy没有实现以这种格式解析回收数据的类。

Also you can try mixed of alternative multipart/form-data encoding. But Indy doesn't have implemented classes for parsing recived data in this format.

这篇关于从base64编码的字符串解码文件到流。怎么做?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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