forked from CianCoders/feathers-example-fileupload
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
57 lines (53 loc) · 2.31 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
<!doctype html>
<html>
<head>
<title>Feathersjs File Upload</title>
<script src="https://code.jquery.com/jquery-2.2.3.min.js" integrity="sha256-a23g1Nt4dtEYOj7bR+vTu7+T8VP13humZFBJNIYoEJo=" crossorigin="anonymous"></script>
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/core-js/2.1.4/core.min.js"></script>
<script type="text/javascript" src="//unpkg.com/feathers-client@^1.0.0/dist/feathers.js"></script>
<script type="text/javascript">
// feathers client initialization
const rest = feathers.rest('http://localhost:3030');
const app = feathers()
.configure(feathers.hooks())
.configure(rest.jquery($));
// setup jQuery to watch the ajax progress
$.ajaxSetup({
xhr: function () {
var xhr = new window.XMLHttpRequest();
// upload progress
xhr.addEventListener("progress", function (evt) {
if (evt.lengthComputable) {
var percentComplete = evt.loaded / evt.total;
console.log('upload progress: ', Math.round(percentComplete * 100) + "%");
}
}, false);
return xhr;
}
});
const uploadService = app.service('uploads');
const reader = new FileReader();
// upload on file selection
$(document).ready(function(){
$('input#file').change(function(){
var file = this.files[0];
reader.readAsDataURL(file); // encode file
})
});
// when encoded, upload
reader.addEventListener("load", function () {
console.log('encoded file: ', reader.result);
var upload = uploadService
.create({uri: reader.result})
.then(function(response){
alert('UPLOADED!! ');
console.log('Server responded with: ', response);
});
}, false);
</script>
</head>
<body>
<h1>Let's upload some files!</h1>
<input type="file" id="file"/>
</body>
</html>