forked from joshualloyd/firebase-file-upload-example
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
105 lines (82 loc) · 3.03 KB
/
index.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
<!DOCTYPE html>
<html>
<head>
<title>Testing file upload</title>
</head>
<body>
<progress value="0" max="100" id="uploader">0%</progress>
<input type="file" value="upload" accept=".jpg" id="fileButton"> // This example uses the CDN, in a production setting you'd likely want to use the npm package
<script src="https://www.gstatic.com/firebasejs/4.2.0/firebase.js"></script>
<script>
//BE SURE TO PROTECT EVERYTHING IN THE CONFIG
//DON'T COMMIT IT!!!
// Initialize Firebase
var config = {
apiKey: "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
authDomain: "YOURAPPNAME.firebaseapp.com",
databaseURL: "https://YOURAPPNAME.firebaseio.com",
projectId: "YOURAPPNAME",
storageBucket: "YOURAPPNAME.appspot.com",
messagingSenderId: "000000000000"
};
firebase.initializeApp(config);
</script>
<script type="text/javascript">
// firebase bucket name
// REPLACE WITH THE ONE YOU CREATE
// ALSO CHECK STORAGE RULES IN FIREBASE CONSOLE
var fbBucketName = 'images';
// get elements
var uploader = document.getElementById('uploader');
var fileButton = document.getElementById('fileButton');
// listen for file selection
fileButton.addEventListener('change', function (e) {
// what happened
console.log('file upload event', e);
// get file
var file = e.target.files[0];
// create a storage ref
var storageRef = firebase.storage().ref(`${fbBucketName}/${file.name}`);
// upload file
var uploadTask = storageRef.put(file);
// The part below is largely copy-pasted from the 'Full Example' section from
// https://firebase.google.com/docs/storage/web/upload-files
// update progress bar
uploadTask.on(firebase.storage.TaskEvent.STATE_CHANGED, // or 'state_changed'
function (snapshot) {
// Get task progress, including the number of bytes uploaded and the total number of bytes to be uploaded
var progress = (snapshot.bytesTransferred / snapshot.totalBytes) * 100;
uploader.value = progress;
console.log('Upload is ' + progress + '% done');
switch (snapshot.state) {
case firebase.storage.TaskState.PAUSED: // or 'paused'
console.log('Upload is paused');
break;
case firebase.storage.TaskState.RUNNING: // or 'running'
console.log('Upload is running');
break;
}
}, function (error) {
// A full list of error codes is available at
// https://firebase.google.com/docs/storage/web/handle-errors
switch (error.code) {
case 'storage/unauthorized':
// User doesn't have permission to access the object
break;
case 'storage/canceled':
// User canceled the upload
break;
case 'storage/unknown':
// Unknown error occurred, inspect error.serverResponse
break;
}
}, function () {
// Upload completed successfully, now we can get the download URL
// save this link somewhere, e.g. put it in an input field
var downloadURL = uploadTask.snapshot.downloadURL;
console.log('downloadURL', downloadURL);
});
});
</script>
</body>
</html>