-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.html
96 lines (73 loc) · 2.61 KB
/
example.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<p>Select images, they will be stored in your browser and will be displayed even after reload. Selecting big images (> 2MB) can crash your browser.</p>
<input type="file" id="files">
<p id="state">No images stored in your browser.</p>
<div id="list">
</div>
<a href="#" id="deleteImgs">Delete Images</a>
</body>
</html>
<script>
var imagesObject = [];
function handleFileSelect(evt) {
var files = evt.target.files; // FileList object
// Loop through the FileList and render image files as thumbnails.
for (var i = 0, f; f = files[i]; i++) {
// Only process image files.
if (!f.type.match('image.*')) {
continue;
}
var reader = new FileReader();
// Closure to capture the file information.
reader.onload = function(e) {
displayImgData(e.target.result)
addImage(e.target.result);
};
reader.readAsDataURL(f);
}
}
function loadFromLocalStorage(){
var images = JSON.parse(localStorage.getItem("images"))
if(images && images.length > 0){
imagesObject = images;
displayNumberOfImgs();
images.forEach(displayImgData);
}
}
function addImage(imgData){
imagesObject.push(imgData);
displayNumberOfImgs();
localStorage.setItem("images", JSON.stringify(imagesObject));
}
function displayImgData(imgData){
var span = document.createElement('span');
span.innerHTML = '<img class="thumb" src="' + imgData + '"/>';
document.getElementById('list').insertBefore(span, null);
}
function displayNumberOfImgs(){
if(imagesObject.length > 0){
document.getElementById("state").innerHTML = imagesObject.length + " image" + ((imagesObject.length > 1) ? "s" : "") + " stored in your browser";
document.getElementById("deleteImgs").style.display = "inline";
} else {
document.getElementById("state").innerHTML = "No images stored in your browser.";
document.getElementById("deleteImgs").style.display = "none";
}
}
function deleteImages(){
imagesObject = [];
localStorage.removeItem("images");
displayNumberOfImgs()
document.getElementById('list').innerHTML = "";
}
document.getElementById('files').addEventListener('change', handleFileSelect, false);
document.getElementById('deleteImgs').addEventListener("click", deleteImages);
loadFromLocalStorage();
</script>