I have an input file type:
JavaScript
x
2
1
<input type="file" accept="image/*" (change)="openFile($event)" />
2
Method opens file:
JavaScript
1
6
1
openFile(event: Event) {
2
const input = event.target as HTMLInputElement;
3
4
this.read(input);
5
}
6
Method reads a data:
JavaScript
1
26
26
1
private read(input) {
2
const fileTypes = ['jpg', 'jpeg', 'png', 'gif', 'svg'];
3
4
if (input.files && input.files[0]) {
5
const extension = input.files[0].name.split('.').pop().toLowerCase(),
6
isSuccess = fileTypes.indexOf(extension) > -1;
7
8
if (isSuccess) {
9
const reader = new FileReader();
10
reader.onload = () => {
11
const data = reader.result;
12
13
// HOW create svg image here?
14
15
const = svg; // create svg here from data image
16
document.getElementBy('block').appendChild(svg);
17
18
};
19
20
reader.readAsDataURL(input.files[0]);
21
} else {
22
console.log('Wring image type');
23
}
24
}
25
}
26
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
1
10
10
1
<?xml version="1.0" encoding="utf-8"?>
2
<!-- Generator: Adobe Illustrator 16.0.4, SVG Export Plug-In . SVG Version: 6.00 Build 0) -->
3
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
4
<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
5
width="612px" height="502.174px" viewBox="0 65.326 612 502.174" enable-background="new 0 65.326 612 502.174"
6
xml:space="preserve">
7
<ellipse fill="#C6C6C6" cx="283.5" cy="487.5" rx="259" ry="80"/>
8
<path id="bird" d="M210.333,65.331C104.367,66.105-12.349,150.637,1.056,276.449c4.303,40.393,18.533,63.704,52.171,79.03
9
c36.307,16.544,57.022,54.556,50.406,112.954c-9.935,4.88-17.405,11.031-19.132,20.015c7.531-0.17,14.943-0.312,22.59,4.341
10
As you can see image file has svg tag.
For readAsText
I get this:
For readAsDataURL:
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 ordinaryonchange
listener, - take the event param for the event handler
openFile()
without a$
and - declare your functions as ordinary functions with
function
instead ofprivate
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
1
29
29
1
function openFile(event) {
2
this.read(event.target);
3
}
4
5
function read(input) {
6
const fileTypes = ['jpg', 'jpeg', 'png', 'gif', 'svg'];
7
8
if (input.files && input.files[0]) {
9
const extension = input.files[0].name.split('.').pop().toLowerCase(),
10
isSuccess = fileTypes.indexOf(extension) > -1;
11
12
if (isSuccess) {
13
const reader = new FileReader();
14
15
if (extension == "svg") {
16
reader.onload = () => {
17
document.getElementById('block').innerHTML = reader.result;
18
};
19
reader.readAsText(input.files[0]);
20
}
21
else {
22
// proceed the other image types
23
}
24
}
25
else {
26
console.log('Wrong image type');
27
}
28
}
29
}
JavaScript
1
3
1
<input type="file" id="file-input" accept="image/*" onchange="openFile(event)">
2
3
<div id="block"></div>