0

I want to save the svg file provided by the server as a png file with JavaScript. I think the file format is wrong, how can I download it?

This is the code to download the svg file. I wonder how to fix it.

const accessCookie = getAccessTokenCookie();
            axios({
                url: `${baseUrl}/${this.Qrid}`,
                method: 'GET',
                // responseType: 'arraybuffer',
                headers: {
                    Authorization: `Bearer` + accessCookie,
                },
            }).then(response => {
                var file = new Blob([response.data], { type: 'image/svg' });
                const fileURL = URL.createObjectURL(file);
                const link = document.createElement('a');
                link.href = fileURL;
                link.download = 'qr' + this.$date().format('YYYY.MM.DD_h_m_s') + '.svg';
                link.click();
                console.log(response);
            });
김정수
  • 611
  • 4
  • 9
  • 22
  • you'll need some way of converting svg to png - have you found anything? what makes you think the code doesn't download the svg correctly? – Bravo Feb 28 '22 at 01:19
  • 2
    See https://stackoverflow.com/questions/3975499/convert-svg-to-image-jpeg-png-etc-in-the-browser – Robert Longson Feb 28 '22 at 01:32
  • You'd want to download the `svg` blob and pipe into an image conversion process. [Sharp](https://www.npmjs.com/package/sharp), a Node.js image processing library, looks promising for this. – Bumhan Yu Feb 28 '22 at 01:34

0 Answers0