Web forum is in read-only mode. Login as active registered customer for write access
  Forum Search   New Posts New Posts

Base64 and DB

 Post Reply Post Reply
Author
Dino Gomezel View Drop Down
Member
Member
Avatar

Joined: 27 Aug 2018
Posts: 89
Post Options Post Options   Quote Dino Gomezel Quote  Post ReplyReply Direct Link To This Post Topic: Base64 and DB
    Posted: 25 Oct 2019 at 8:36am
Hi!

working on a project that takes an image and save it to the DB I noticed that it is saved in a strange format. It's working, I'm just curious.

Using this code:
procedure TfrmNew.UpdatePicture;
var
  xhr: TJSXmlHttpRequest;
//  slika: string;

  function Base64ToArrayBuffer(str: string): TJSArrayBuffer;
  var
    BufView: TJSUInt8Array;
    BinaryString: string;
    I: Integer;
  begin
    BinaryString := window.atob(str);
    Result := TJSArrayBuffer.New(Length(BinaryString));
    BufView := TJSUInt8Array.New(Result);
    for I := 0 to Length(BinaryString) - 1 do
      BufView := TJSString(BinaryString).CharCodeAt(I);
  end;

begin
  xhr := TJSXMLHttpRequest.new;
  xhr.open('PUT', connServer.URL+'/'+string('pobuda('+wdsPobuda.FieldByName('Id').AsString+')/Slika'));
  xhr.send(Base64ToArrayBuffer(StringReplace(camMain.SnapShotAsBase64, 'data:image/png;base64,', '', [])));
end;

The input string is like this:
iVBORw0KGgoAAAANSUhEUgAAAqIAAAGoCAYAAACdcocIAAAgAElEQVR4
but in the DB is saved like this:
PNG  IHDR5 IDATx^̽YnYrv)3U=f7AX4m

Is this correct?
Back to Top
Dino Gomezel View Drop Down
Member
Member
Avatar

Joined: 27 Aug 2018
Posts: 89
Post Options Post Options   Quote Dino Gomezel Quote  Post ReplyReply Direct Link To This Post Posted: 25 Oct 2019 at 8:37am
Note: The strings are of course truncated to first 40 chars for demo purposes
Back to Top
Bruno Fierens View Drop Down
TMS Support
TMS Support
Avatar

Joined: 11 May 2010
Posts: 8402
Post Options Post Options   Quote Bruno Fierens Quote  Post ReplyReply Direct Link To This Post Posted: 25 Oct 2019 at 12:18pm
What data type is your server expecting?
If you expect this to be stored in your DB as base64 string, why trying the conversion to an Int8Array?
Can't you send the base64 string as-is?
Back to Top
Dino Gomezel View Drop Down
Member
Member
Avatar

Joined: 27 Aug 2018
Posts: 89
Post Options Post Options   Quote Dino Gomezel Quote  Post ReplyReply Direct Link To This Post Posted: 08 Nov 2019 at 1:49pm
Back to Top
Bruno Fierens View Drop Down
TMS Support
TMS Support
Avatar

Joined: 11 May 2010
Posts: 8402
Post Options Post Options   Quote Bruno Fierens Quote  Post ReplyReply Direct Link To This Post Posted: 09 Nov 2019 at 10:06am
The code converts the base64 encoded image data to a binary array, so it is normal that the data you look at as base64 string is different from the binary data.
The binary data looks, at least from what I can see as PNG file header correct, a PNG image file starts with header PNG So, I would assume the binary data to be correct? What do you experience as incorrect?
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down