source: src/main/resources/static/js/fileUploadPreview.js@ 5577566

Last change on this file since 5577566 was 5577566, checked in by NikolaCenevski <cenevskinikola@…>, 3 years ago

prototip part 2

  • Property mode set to 100644
File size: 1.9 KB
Line 
1var imagePreviewRegion = document.getElementById("image-preview");
2var titleInput = document.getElementById("titleImage");
3var imagesInput = document.getElementById("images");
4var moderatorInput = document.getElementById("moderatorImages");
5
6titleInput.addEventListener("change", function () {
7 imagePreviewRegion.innerHTML = "";
8 handleFiles(titleInput);
9 handleFiles(imagesInput);
10 handleFiles(moderatorInput);
11})
12
13imagesInput.addEventListener("change", function () {
14 imagePreviewRegion.innerHTML = "";
15 handleFiles(titleInput);
16 handleFiles(imagesInput);
17 handleFiles(moderatorInput);
18})
19
20moderatorInput.addEventListener("change", function () {
21 imagePreviewRegion.innerHTML = "";
22 handleFiles(titleInput);
23 handleFiles(imagesInput);
24 handleFiles(moderatorInput);
25})
26
27function preventDefault(e) {
28 e.preventDefault();
29 e.stopPropagation();
30}
31
32function 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
42function 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
58function 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}
Note: See TracBrowser for help on using the repository browser.