diff options
author | Johannes Zellner <johannes@nebulon.de> | 2016-03-01 15:54:16 +0100 |
---|---|---|
committer | Johannes Zellner <johannes@nebulon.de> | 2016-03-01 15:54:16 +0100 |
commit | 537bfb042b9f97e3a0ab8d6391b3519111073067 (patch) | |
tree | ea1ded91feacb1ef2d5662713599b3ebe6ccff66 /app | |
parent | ee9d1ada964d9e9ce7d9c020ab105aa7ded8147e (diff) | |
download | Surfer-537bfb042b9f97e3a0ab8d6391b3519111073067.tar.gz Surfer-537bfb042b9f97e3a0ab8d6391b3519111073067.tar.zst Surfer-537bfb042b9f97e3a0ab8d6391b3519111073067.zip |
Implement file upload
Diffstat (limited to 'app')
-rw-r--r-- | app/index.html | 3 | ||||
-rw-r--r-- | app/js/app.js | 34 |
2 files changed, 35 insertions, 2 deletions
diff --git a/app/index.html b/app/index.html index 78f5a94..721ec25 100644 --- a/app/index.html +++ b/app/index.html | |||
@@ -63,7 +63,8 @@ | |||
63 | <div class="row"> | 63 | <div class="row"> |
64 | <div class="col-lg-12"> | 64 | <div class="col-lg-12"> |
65 | <center> | 65 | <center> |
66 | <button class="btn btn-primary">Upload</button> | 66 | <input type='file' v-el:upload style="display: none"/> |
67 | <button class="btn btn-primary" v-on:click="upload()">Upload</button> | ||
67 | </center> | 68 | </center> |
68 | <br/> | 69 | <br/> |
69 | </div> | 70 | </div> |
diff --git a/app/js/app.js b/app/js/app.js index b71a7ed..e2fe621 100644 --- a/app/js/app.js +++ b/app/js/app.js | |||
@@ -39,6 +39,14 @@ function sanitize(filePath) { | |||
39 | return filePath.replace(/\/+/g, '/'); | 39 | return filePath.replace(/\/+/g, '/'); |
40 | } | 40 | } |
41 | 41 | ||
42 | function encode(filePath) { | ||
43 | return filePath.split('/').map(encodeURIComponent).join('/'); | ||
44 | } | ||
45 | |||
46 | function refresh() { | ||
47 | loadDirectory(app.path); | ||
48 | } | ||
49 | |||
42 | function loadDirectory(filePath) { | 50 | function loadDirectory(filePath) { |
43 | app.busy = true; | 51 | app.busy = true; |
44 | 52 | ||
@@ -70,6 +78,29 @@ function up() { | |||
70 | loadDirectory(app.path.split('/').slice(0, -1).filter(function (p) { return !!p; }).join('/')); | 78 | loadDirectory(app.path.split('/').slice(0, -1).filter(function (p) { return !!p; }).join('/')); |
71 | } | 79 | } |
72 | 80 | ||
81 | function upload() { | ||
82 | $(app.$els.upload).change(function () { | ||
83 | app.busy = true; | ||
84 | |||
85 | var file = app.$els.upload.files[0]; | ||
86 | var path = encode(sanitize(app.path + '/' + file.name)); | ||
87 | |||
88 | var formData = new FormData(); | ||
89 | formData.append('file', file); | ||
90 | |||
91 | superagent.put('/api/files' + path).query({ username: app.session.username, password: app.session.password }).send(formData).end(function (error, result) { | ||
92 | app.busy = false; | ||
93 | |||
94 | if (error) return console.error(error); | ||
95 | if (result.statusCode !== 201) return console.error('Error uploading file: ', result.statusCode); | ||
96 | |||
97 | refresh(); | ||
98 | }); | ||
99 | }); | ||
100 | |||
101 | app.$els.upload.click(); | ||
102 | } | ||
103 | |||
73 | var app = new Vue({ | 104 | var app = new Vue({ |
74 | el: '#app', | 105 | el: '#app', |
75 | data: { | 106 | data: { |
@@ -87,7 +118,8 @@ var app = new Vue({ | |||
87 | logout: logout, | 118 | logout: logout, |
88 | loadDirectory: loadDirectory, | 119 | loadDirectory: loadDirectory, |
89 | open: open, | 120 | open: open, |
90 | up: up | 121 | up: up, |
122 | upload: upload | ||
91 | } | 123 | } |
92 | }); | 124 | }); |
93 | 125 | ||