U
U
Username2017-06-14 18:32:53
JavaScript
Username, 2017-06-14 18:32:53

How to download an image in base64?

In react I generate a table that can be downloaded. Those. I render the layout, then using the html2canvas.js plugin I convert the layout into a canvas element, and download the canvas element using canvas2image.js

html2canvas(ReactDOM.findDOMNode(this.refs.scheduleTable), {
    onrendered: function (canvas) {
        //Получаю изображение в base64
        var dataURL = canvas.toDataURL('image/png');


        // Convert and download as image 
        Canvas2Image.saveAsPNG(canvas);

    }
});

  1. base64 passed to window.open(dataURL), otherwise the picture opens in a new tab, but you need to download it when you click on the download button
  2. Tried to write base64 to state inside onrendered, error
    TypeError: this.setState is not a function
    outside writes undefined
  3. Canvas2Image.saveAsPNG(canvas);saves the picture without extension, although this is the easiest way to save, it would be possible to specify the format.

How can I save the image when clicking on the download image button ?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Peter, 2017-06-14
@petermzg

Browser loading always happens if header is set in response
Content-disposition: attachment; filename=fileName

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question