somanyerrorswhy
somanyerrorswhy

Reputation: 173

Conversion of base64 to image results in a "Failed to load resource: net::ERR_INVALID_URL" error when array is used.

Basically when I use:

imageObj.src = "data:image/webp;base64,data[2]";

It will give me an error.

However, when I console.log(data[2]) and then copy and paste whatever data[2] is into the code like this:

imageObj.src = "data:image/webp;base64,sampleexampleasfnakdjfbaksjbrwkaerhdhcoaunsf";

Everything works out. Please help me understand?

Upvotes: 1

Views: 8977

Answers (1)

user3357118
user3357118

Reputation:

Javascript does not interpolate variables inside strings like some languages. If data[2] is a string, use string concatenation to add data[2] to the URI:

imageObj.src = "data:image/webp;base64,"+data[2];

Upvotes: 2

Related Questions