-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathindex.js
130 lines (112 loc) · 3.02 KB
/
index.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
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
if (!window.FileReader) {
console.error('Your browser does not support FileReader API!');
}
exports.default = {
name: 'vue-base64-file-upload',
props: {
imageClass: {
type: String,
default: ''
},
inputClass: {
type: String,
default: ''
},
accept: {
type: String,
default: 'image/png,image/gif,image/jpeg'
},
maxSize: {
type: Number,
default: 10 // megabytes
},
disablePreview: {
type: Boolean,
default: false
},
fileName: {
type: String,
default: ''
},
placeholder: {
type: String,
default: 'Click here to upload image'
},
defaultPreview: {
type: String,
default: ''
}
},
data: function data() {
return {
file: null,
preview: null,
visiblePreview: false
};
},
computed: {
wrapperStyles: function wrapperStyles() {
return {
'position': 'relative',
'width': '100%'
};
},
fileInputStyles: function fileInputStyles() {
return {
'width': '100%',
'position': 'absolute',
'top': 0,
'left': 0,
'right': 0,
'bottom': 0,
'opacity': 0,
'overflow': 'hidden',
'outline': 'none',
'cursor': 'pointer'
};
},
textInputStyles: function textInputStyles() {
return {
'width': '100%',
'cursor': 'pointer'
};
},
previewImage: function previewImage() {
return this.preview || this.defaultPreview;
}
},
methods: {
onChange: function onChange(e) {
var _this = this;
var files = e.target.files || e.dataTransfer.files;
if (!files.length) {
return;
}
var file = files[0];
var size = file.size && file.size / Math.pow(1000, 2);
// check file max size
if (size > this.maxSize) {
this.$emit('size-exceeded', size);
return;
}
// update file
this.file = file;
this.$emit('file', file);
var reader = new FileReader();
reader.onload = function (e) {
var dataURI = e.target.result;
if (dataURI) {
_this.$emit('load', dataURI);
_this.preview = dataURI;
}
};
// read blob url from file data
reader.readAsDataURL(file);
}
},
template: '\n <div class="vue-base64-file-upload">\n <img\n v-show="previewImage && !disablePreview"\n :src="previewImage"\n :class="imageClass" />\n <div class="vue-base64-file-upload-wrapper" :style="wrapperStyles">\n <input\n type="file"\n @change="onChange"\n :style="fileInputStyles"\n :accept=accept />\n <input\n type="text"\n :class="inputClass"\n :style="textInputStyles"\n :value="fileName || file && file.name"\n :placeholder="placeholder"\n disabled />\n </div>\n </div>\n '
};