Skip to content
Advertisement

How to display svg image after load?

I have an input file type:

JavaScript

Method opens file:

JavaScript

Method reads a data:

JavaScript

So when I load a SVG image I get content of it. How to place image in block id="block" based data?

SVG image looks like:

JavaScript

As you can see image file has svg tag.

For readAsText I get this:

�PNG

For readAsDataURL:

enter image description here

Advertisement

Answer

Maybe i think it too simple, but you can read the file as text (reader.readAsText()) and simply use innerHTML for inserting the svg. To get this to work you have to:

  • change the (change) listener in your input to an ordinary onchange listener,
  • take the event param for the event handler openFile() without a $ and
  • declare your functions as ordinary functions with function instead of private

Working example: I saved your example svg on my PC, closed the path tag and added a closing svg tag) and uploaded it using the input.

I don’t know why you have that extra function openFile(), but i let it like in your example. But you could just use read(event) and reader.readAsText(event.target.files[0]);.

JavaScript
JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement