-
Notifications
You must be signed in to change notification settings - Fork 0
/
toBase64.html
39 lines (38 loc) · 1.07 KB
/
toBase64.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<html>
<input id="inputFileToLoad" type="file" onchange="encodeImageFileAsURL()"/>
<div class="converterwrapper">
<div id="imgTest" style="width: 300px; height: auto"></div>
<div id="base64"></div>
</div>
<style>
.converterwrapper{
display: flex;
flex-direction: row;
margin: 0 auto;
}
img{
max-width: 300px;
height: auto;
}
#base64 {
max-width: 300px;
}
</style>
<script type="text/javascript">
function encodeImageFileAsURL() {
var fileSelected = document.getElementById("inputFileToLoad").files;
if(fileSelected.length > 0){
var fileToLoad = fileSelected[0];
var fileReader = new FileReader();
fileReader.onload = function(fileLoadedEvent){
var srcData = fileLoadedEvent.target.result;
var newImage = document.createElement('img');
newImage.src = srcData;
document.getElementById("imgTest").innerHTML = newImage.outerHTML;
document.getElementById("base64").innerHTML = newImage.src;
}
fileReader.readAsDataURL(fileToLoad);
}
}
</script>
</html>