-
Notifications
You must be signed in to change notification settings - Fork 3
/
script.js
37 lines (33 loc) · 1.14 KB
/
script.js
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
// Grabbing Elements and Storing in Variables
const defaultFile = document.getElementById("default-file");
const customBtn = document.getElementById("custom-btn");
const customSpace = document.getElementById("custom-space");
customBtn.addEventListener("click", function () {
defaultFile.click();
});
// File Upload
defaultFile.addEventListener("change", function () {
// Format Selected File Text
if (defaultFile.value) {
customSpace.innerHTML =
defaultFile.value.match(/[\/\\]([\w\d\s\.\-\(\)]+)$/)[1] + "🔥";
} else {
customSpace.innerHTML = "No File, Selected!😭";
}
// Image Preview
const files = defaultFile.files[0]; //files[0] - For getting first file
// console.log(files);
if (files) {
// Showing Image and Hiding "Image Preview" Text
preview_img.style.display = "block";
preview_text.style.display = "none";
//Read File
const fileReader = new FileReader();
fileReader.addEventListener("load", function () {
// convert image to base64 encoded string
preview_img.setAttribute("src", this.result);
console.log(this.result);
});
fileReader.readAsDataURL(files);
}
});