1 | var imagePreviewRegion = document.getElementById("image-preview");
|
---|
2 | var titleInput = document.getElementById("titleImage");
|
---|
3 | var imagesInput = document.getElementById("images");
|
---|
4 | var moderatorInput = document.getElementById("moderatorImages");
|
---|
5 |
|
---|
6 | titleInput.addEventListener("change", function () {
|
---|
7 | imagePreviewRegion.innerHTML = "";
|
---|
8 | handleFiles(titleInput);
|
---|
9 | handleFiles(imagesInput);
|
---|
10 | handleFiles(moderatorInput);
|
---|
11 | })
|
---|
12 |
|
---|
13 | imagesInput.addEventListener("change", function () {
|
---|
14 | imagePreviewRegion.innerHTML = "";
|
---|
15 | handleFiles(titleInput);
|
---|
16 | handleFiles(imagesInput);
|
---|
17 | handleFiles(moderatorInput);
|
---|
18 | })
|
---|
19 |
|
---|
20 | moderatorInput.addEventListener("change", function () {
|
---|
21 | imagePreviewRegion.innerHTML = "";
|
---|
22 | handleFiles(titleInput);
|
---|
23 | handleFiles(imagesInput);
|
---|
24 | handleFiles(moderatorInput);
|
---|
25 | })
|
---|
26 |
|
---|
27 | function preventDefault(e) {
|
---|
28 | e.preventDefault();
|
---|
29 | e.stopPropagation();
|
---|
30 | }
|
---|
31 |
|
---|
32 | function handleFiles(input) {
|
---|
33 | var files = input.files;
|
---|
34 | for (var i = 0, len = files.length; i < len; i++) {
|
---|
35 | if (validateImage(files[i]))
|
---|
36 | previewAnduploadImage(files[i]);
|
---|
37 | else
|
---|
38 | input.value="";
|
---|
39 | }
|
---|
40 | }
|
---|
41 |
|
---|
42 | function validateImage(image) {
|
---|
43 | var validTypes = ['image/jpeg', 'image/png', 'image/gif'];
|
---|
44 | if (validTypes.indexOf(image.type) === -1) {
|
---|
45 | alert("Invalid File Type");
|
---|
46 | return false;
|
---|
47 | }
|
---|
48 |
|
---|
49 | var maxSizeInBytes = 10e6; // 10MB
|
---|
50 | if (image.size > maxSizeInBytes) {
|
---|
51 | alert("File too large");
|
---|
52 | return false;
|
---|
53 | }
|
---|
54 |
|
---|
55 | return true;
|
---|
56 | }
|
---|
57 |
|
---|
58 | function previewAnduploadImage(image) {
|
---|
59 |
|
---|
60 | var imgView = document.createElement("div");
|
---|
61 | imgView.className = "image-view";
|
---|
62 | imagePreviewRegion.appendChild(imgView);
|
---|
63 |
|
---|
64 | var img = document.createElement("img");
|
---|
65 | imgView.appendChild(img);
|
---|
66 |
|
---|
67 | var reader = new FileReader();
|
---|
68 | reader.onload = function (e) {
|
---|
69 | img.src = e.target.result;
|
---|
70 | }
|
---|
71 | reader.readAsDataURL(image);
|
---|
72 | } |
---|