3

i've been at this for days now and im stuck on this final part. as the title suggests my creation receives a base64 encoded image. it then loads it into a buffer and attempts to send it to to a channel like so:

sfbuffer = new Buffer.from(base64_img, "base64");
const finalattach = new Discord.MessageAttachment(sfbuffer);
message.channel.send(finalattach);

it does send the buffer however it always results in this

example of base64 data that gets loaded

data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAQAAAAEACAIAAADTED8xAAEA and so on...

i've tried sending it with file options (didn't even send), and as an embed using both .setimage and .attachfiles and both produce the same results. please im banging my head over this. all the google links are purple and i don't know what else to do D:

Lexy
  • 61
  • 1
  • 5
  • If you download that image does it actually display properly? – Algo7 Jan 11 '21 at 23:43
  • this confuses me more. took an example base64 image and put it into a generic converter and it didn't work. looked over it and theres a speech mark at the end even though i removed them with `.replace('"', '')` – Lexy Jan 12 '21 at 00:00
  • Have you tried to decode it using online tools that are made for it? https://base64.guru/converter/decode/image – Algo7 Jan 12 '21 at 00:03
  • sorry for not being more clear. i took the output that the bot gets and put it into a tool to decode base64 pngs (they're always pngs) and it didn't work. i looked over it and found the extra speech mark when they should of got removed with the above method. removal of the speech mark lets it decode properly – Lexy Jan 12 '21 at 00:05

2 Answers2

3

image buffers should NOT contain the data:image/png;base64, part. only the non-human readable data

split the base64 string at the comma, load it into a attachment as a buffer and send attachment e.g.

const sfbuff = new Buffer.from(base64_img.split(",")[1], "base64");
const sfattach = new Discord.MessageAttachment(sfbuff, "output.png");
message.channel.send(sfattach)

Even though i figured it out, i couldn't of done it without Aviv lo Thank you all :)

Lexy
  • 61
  • 1
  • 5
1

Try something like this:

replyMessage.Attachments.Add(new Attachment()
{
     ContentUrl = $"data:image/jpeg;base64,{Convert.ToBase64String(bdata)}"
});

This post is answering similar questions.

Algo7
  • 2,122
  • 1
  • 8
  • 19
  • i receive the base64 as a string. it cointains the data:image/png;base64 stuff – Lexy Jan 12 '21 at 00:37
  • Maybe this? https://stackoverflow.com/questions/64834944/discordjs-using-base64-image-in-webhook-embed – Algo7 Jan 12 '21 at 00:38