-
Notifications
You must be signed in to change notification settings - Fork 1
/
file-uploader.html
162 lines (153 loc) · 4.52 KB
/
file-uploader.html
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<!doctype html>
<link rel="import" href="../polymer/polymer-element.html">
<link rel="import" href="../paper-styles/color.html">
<link rel="import" href="../paper-button/paper-button.html">
<dom-module id="file-uploader">
<template>
<style>
:host {
display: block;
font-family: 'Roboto', 'Noto', sans-serif;
font-weight: normal;
font-size: 14px;
-webkit-font-smoothing: antialiased;
}
paper-button {
font-family: 'Roboto', 'Noto', sans-serif;
font-weight: normal;
font-size: 14px;
-webkit-font-smoothing: antialiased;
}
paper-button.indigo {
background-color: var(--paper-indigo-500);
color: white;
--paper-button-raised-keyboard-focus: {
background-color: var(--paper-pink-a200) !important;
color: white !important;
};
}
</style>
<template is="dom-if" if="[[!native_element]]">
<paper-button on-click="buttonClick" class="indigo" raised>[[innerText]]</paper-button>
</template>
<template is="dom-if" if="[[native_element]]">
[[innerText]]
</template>
<input
accept="[[accept]]"
id="fileInput"
type="file"
on-change="filesLoaded"
multiple
></input>
</template>
<script>
/**
* # `file-uploader`
*
* An element that uploads one or more files to the browser
*
* @customElement
* @polymer
* @demo demo/index.html
*/
class FileUploader extends Polymer.Element {
static get is() { return 'file-uploader'; }
static get properties() {
return {
/**
* The label of the button.
**/
innerText: {
type: String
},
/**
* The array containing all the files uploaded from the user
**/
fileList: {
type: Array,
value: function(){ return []; },
readOnly: true
},
/** Accepted types of files, as written for a normal HTML
* `<input type="file">` tag.
*
* example: `image/*` or `.txt`
**/
accept: {
type: String,
value: '*',
},
/** How the files should be readed as after being loaded.
*
* Available options are:
* - Text (default)
* - ArrayBuffer
* - BinaryString
* - DataUrl
*
* Case here **is** important.
**/
parse_as: {
type: String,
value: 'Text'
},
/** If this attribute is true, the element will use a navite HTML tag
* instead of a paper-button.
**/
native_element: {
type: Boolean,
value: false
}
};
}
connectedCallback() {
super.connectedCallback();
this.innerText = this.textContent;
this.$.fileInput.style.display = this.native_element ? 'block':'none';
}
/**
* Triggers a click on the button.
**/
buttonClick(){
this.$.fileInput.click();
}
/**
* Reads all the files selected by the user with the
* [FileReader](https://developer.mozilla.org/en-US/docs/Web/API/FileReader)
* method `readAs$TYPE`, with `$TYPE` specified in the `parse_as`
* attribute.
*
* After a file is readed, its content is pushed in the `fileList` array
* and an `uploader-newfile` is fired.
*/
filesLoaded() {
var files = this.$.fileInput.files;
for ( var i = 0; i < files.length; i++){
var reader = new FileReader();
reader.addEventListener("loadend",
this._appendFile.bind(this),
false
);
reader['readAs'+this.parse_as](files[i]);
};
}
/**
* Appends a newly-readed file in the `fileList` array and fires
* a `uploader-newfile` event.
*/
_appendFile(f) {
this.push('fileList', f.target.result)
/**
* Fired when one file is loaded; it will bubble.
*
* @event uploader-newfiles
*/
this.dispatchEvent(
new CustomEvent('uploader-newfile', {bubbles: true, composed: true})
);
}
}
window.customElements.define(FileUploader.is, FileUploader);
</script>
</dom-module>