-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfirebase-upload-image.html
93 lines (83 loc) · 2.29 KB
/
firebase-upload-image.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
<link rel="import" href="bower_components/polymer/polymer-element.html">
<dom-module id="firebase-upload-image">
<template>
<style media="screen">
:host {
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
margin: 0;
padding: 0;
min-height: 100px;
width: 100%;
}
progress {
width: 100%;
margin-bottom: 5px;
-webkit-appearance: none;
appearance: none;
}
</style>
<progress id="upProg" value="0" max="100">0%</progress>
<input id="fileButt" type="file" value="upload" accept="[[fileType]]" />
</template>
<script>
/**
* `firebase-upload-image`
* A simple element to upload images from firebase
*
* @customElement
* @polymer
* @demo demo/index.html
*/
class FirebaseUploadImage extends Polymer.Element {
static get is() {
return 'firebase-upload-image';
}
ready() {
super.ready();
this.$.fileButt.addEventListener('change', e => this._handleFile(e));
}
static get properties() {
return {
appName: {
type: String,
value: 'app'
},
directory: {
type: String,
value: 'img/'
},
storedName: {
type: String,
value: ''
},
fileType: {
type: String,
value: 'image/*'
}
};
}
_handleFile(event) {
let file = event.target.files[0];
let dest = '';
let prog = this.$.upProg;
dest = this.directory + (this.storedName != '' ? this.storedName : file.name);
let fbsImageRef = firebase.app(this.appName).storage().ref(dest);
let monitor = fbsImageRef.put(file);
monitor.on('state_changed',
function progress(snapshot) {
prog.value = 100 * (snapshot.bytesTransferred / snapshot.totalBytes);
},
function error(err){
console.log(`Error: ${err.code}`);
},
function complete(){
console.log(`Complete: ${dest}`);
});
}
}
window.customElements.define(FirebaseUploadImage.is, FirebaseUploadImage);
</script>
</dom-module>