Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

VertexBuffer's avatar

How to convert axios response to DataURI?

I feel like I've tried everything! I do have a semi-working piece of code, but it has a lot of issues and often runs into some maximum call-stack issue, as well as it just being generally gross code.

          const file = `data:${res.headers['content-type']};base64,${btoa(
            String.fromCharCode(...new Uint8Array(res.data))
          )}`;

I want to use the https://www.npmjs.com/package/datauri package, but everytime I try and use it I get some really obscure error that there is basically no information on.

          const res = await this.$http.get(val, {
            responseType: 'arraybuffer',
          });

          const file = await datauri.format('.jpeg', res.data);

Response:

DataURI {_events: {…}, _eventsCount: 0, _maxListeners: undefined, readable: true, base64: "PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0idXRmLTgiPz…uMDYzLS41ODIuMDYxLS44NzEuMDh2LjAwMXoiLz48L3N2Zz4=", …}
Uncaught (in promise) TypeError: File.name and File.type are readonly properties

So I'm not really sure what to do. Hopefully someone can be of assistance.

0 likes
1 reply

Please or to participate in this conversation.