Note that there are some explanatory texts on larger screens.

plurals
  1. PODecode file to stream, from base64 encoded string. How to do that?
    primarykey
    data
    text
    <p>I have a string, recived by my app from web-browser. String contains PNG image, and encoded to base64. </p> <p>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. </p> <p>So, how to decode this string to image? </p> <p>In Delphi I written my web-server, that recives data. </p> <p>In PHP this can be solved like <a href="https://stackoverflow.com/questions/15153776/convert-base64-string-to-an-image-file">that</a>:</p> <pre><code>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' ); </code></pre> <p>but in delphi, after decoding:</p> <pre><code> if (sImg &lt;&gt; 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; </code></pre> <p>I have a black square of Malevich.</p> <p>Also I tried functions from EncdDesd module of RTL,</p> <pre><code> if (sImg &lt;&gt; 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; </code></pre> <p>I got the same result. </p> <p>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:</p> <pre><code>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!'); }; </code></pre> <p>...</p> <pre><code>AddNewURL: function (aName, aDesc,sURL, aOwnerId,aImg) { var http = new XMLHttpRequest(); var params = 'Owner=' + aOwnerId + '&amp;Name=' + aName + '&amp;Desc=' + aDesc+ '&amp;URL=' + sURL; if (aImg) { params = params +'&amp;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); }, </code></pre> <p>My web-server recives this data like that:</p> <pre><code> 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 &lt;&gt; 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; </code></pre> <p>After all of that I have image, that contains only black color.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload