11<template >
2+ <div
3+ class ="
4+ d-none d-md-flex
5+ flex-column
6+ dragndrop
7+ container
8+ p-4
9+ shadow
10+ rounded-3
11+ justify-content-center
12+ align-items-center
13+ "
14+ style =" width : 50% "
15+ >
16+ <div class =" text-center fs-4 fw-bold my-2" >Upload your image</div >
17+ <div class =" text-center fw-normal fs-6 my-2" >
18+ File should be Jpeg, Png,...
19+ </div >
20+ <div v-if =" imgSrc" >
21+ <img :src =" imgSrc" alt =" " style =" width : 90% " class =" rounded-3 my-4" />
22+ </div >
223 <div
3- class ="
4- d-none d-md-flex
5- flex-column
6- dragndrop
7- container
8- p-4
9- shadow
10- rounded-3
11- justify-content-center
12- align-items-center
13- "
14- style =" width : 50% "
24+ class ="
25+ drag-area
26+ border-primary border-2
27+ rounded-3
28+ p-5
29+ bg-light bg-gradient
30+ my-4
31+ "
32+ :style =" { border: borderState }"
33+ style =" width : 90% "
34+ @dragover.prevent =" changeBorderToSolid"
35+ @dragleave =" changeBorderToDashed"
36+ @drop.prevent =" imageDrop"
37+ v-else
1538 >
16- <div class =" text-center fs-4 fw-bold my-2" >Upload your image</div >
17- <div class =" text-center fw-normal fs-6 my-2" >
18- File should be Jpeg, Png,...
19- </div >
20- <div v-if =" imgSrc" >
21- <img :src =" imgSrc" alt =" " style =" width : 90% " class =" rounded-3 my-4" />
22- </div >
23- <div
24- class ="
25- drag-area
26- border-primary border-2
27- rounded-3
28- p-5
29- bg-light bg-gradient
30- my-4
31- "
32- :style =" { border: borderState }"
33- style =" width : 90% "
34- @dragover.prevent =" changeBorderToSolid"
35- @dragleave =" changeBorderToDashed"
36- @drop.prevent =" imageDrop"
37- v-else
38- >
39- <img
40- src =" ../assets/image.svg"
41- alt =" "
42- class =" image-fluid mb-4"
43- />
44- <div class =" text-center" >Drag & Drop your image here</div >
45- <div class =" text-center fs-4 fw-normal" >or</div >
46- <input type =" file" name =" file" id =" file" @change =" imageChosen" ref =" fileInput" style =" display :none ;" />
47- <div class =" btn btn-primary mt-4 shadow-sm" @click =" $refs.fileInput.click()" >Pick File</div >
48- </div >
39+ <img src =" ../assets/image.svg" alt =" " class =" image-fluid mb-4" />
40+ <div class =" text-center" >Drag & Drop your image here</div >
41+ <div class =" text-center fs-4 fw-normal" >or</div >
42+ <input
43+ type =" file"
44+ name =" file"
45+ id =" file"
46+ @change =" imageChosen"
47+ ref =" fileInput"
48+ style =" display : none "
49+ />
50+ <div
51+ class =" btn btn-primary mt-4 shadow-sm"
52+ @click =" $refs.fileInput.click()"
53+ >
54+ Pick File
55+ </div >
4956 </div >
57+ </div >
5058</template >
5159
5260<script >
5361export default {
54- name: " dragndrop" ,
55- data () {
56- return {
57- borderState: " dashed" ,
58- getImage: null ,
59- };
62+ name: " dragndrop" ,
63+ data () {
64+ return {
65+ borderState: " dashed" ,
66+ getImage: null ,
67+ };
68+ },
69+ computed: {
70+ imgSrc () {
71+ return this .$store .getters .imageSrc ;
72+ },
73+ },
74+ methods: {
75+ changeBorderToSolid (e ) {
76+ e .preventDefault ();
77+ this .borderState = " solid" ;
6078 },
61- computed: {
62- imgSrc () {
63- return this .$store .getters .imageSrc ;
64- },
79+ changeBorderToDashed () {
80+ this .borderState = " dashed" ;
6581 },
66- methods: {
67- changeBorderToSolid (e ) {
68- e .preventDefault ();
69- this .borderState = " solid" ;
70- },
71- changeBorderToDashed () {
72- this .borderState = " dashed" ;
73- },
74- imageChosen (e ){
75- this .setImage (e .target .files [0 ])
76- },
77- dropImage (e ){
78- this .setImage (e .dataTransfer .files [0 ])
79- },
80- setImage (file ) {
81- let validExtension = [" image/jpg" , " image/jpeg" , " image/png" ];
82- // let file = e.dataTransfer.files[0];
82+ imageChosen (e ) {
83+ this .setImage (e .target .files [0 ]);
84+ },
85+ dropImage (e ) {
86+ this .setImage (e .dataTransfer .files [0 ]);
87+ },
88+ setImage (file ) {
89+ let validExtension = [" image/jpg" , " image/jpeg" , " image/png" ];
90+ // let file = e.dataTransfer.files[0];
8391
84- if (validExtension .includes (file .type )) {
85- let fileReader = new FileReader ();
86- fileReader .onload = () => {
87- let fileURL = fileReader .result ;
88- // console.log(fileURL)
89- this .$store .dispatch (" setImgSrc" , fileURL);
90- };
91- fileReader .readAsDataURL (file);
92- this .$store .dispatch (" setImgFile" , file);
93- } else {
94- alert (" Please enter an image file" );
95- }
96- },
92+ if (validExtension .includes (file .type )) {
93+ let fileReader = new FileReader ();
94+ fileReader .onload = () => {
95+ let fileURL = fileReader .result ;
96+ // console.log(fileURL)
97+ this .$store .dispatch (" setImgSrc" , fileURL);
98+ };
99+ fileReader .readAsDataURL (file);
100+ this .$store .dispatch (" setImgFile" , file);
101+ } else {
102+ alert (" Please enter an image file" );
103+ }
97104 },
105+ },
98106};
99107 </script >
100108
101- <style lang="scss" scoped>
102- </style >
109+ <style lang="scss" scoped></style >
0 commit comments