Commit 537bfb04 by Johannes Zellner

Implement file upload

1 parent ee9d1ada
Showing with 35 additions and 2 deletions
...@@ -63,7 +63,8 @@ ...@@ -63,7 +63,8 @@
<div class="row"> <div class="row">
<div class="col-lg-12"> <div class="col-lg-12">
<center> <center>
<button class="btn btn-primary">Upload</button> <input type='file' v-el:upload style="display: none"/>
<button class="btn btn-primary" v-on:click="upload()">Upload</button>
</center> </center>
<br/> <br/>
</div> </div>
......
...@@ -39,6 +39,14 @@ function sanitize(filePath) { ...@@ -39,6 +39,14 @@ function sanitize(filePath) {
return filePath.replace(/\/+/g, '/'); return filePath.replace(/\/+/g, '/');
} }
function encode(filePath) {
return filePath.split('/').map(encodeURIComponent).join('/');
}
function refresh() {
loadDirectory(app.path);
}
function loadDirectory(filePath) { function loadDirectory(filePath) {
app.busy = true; app.busy = true;
...@@ -70,6 +78,29 @@ function up() { ...@@ -70,6 +78,29 @@ function up() {
loadDirectory(app.path.split('/').slice(0, -1).filter(function (p) { return !!p; }).join('/')); loadDirectory(app.path.split('/').slice(0, -1).filter(function (p) { return !!p; }).join('/'));
} }
function upload() {
$(app.$els.upload).change(function () {
app.busy = true;
var file = app.$els.upload.files[0];
var path = encode(sanitize(app.path + '/' + file.name));
var formData = new FormData();
formData.append('file', file);
superagent.put('/api/files' + path).query({ username: app.session.username, password: app.session.password }).send(formData).end(function (error, result) {
app.busy = false;
if (error) return console.error(error);
if (result.statusCode !== 201) return console.error('Error uploading file: ', result.statusCode);
refresh();
});
});
app.$els.upload.click();
}
var app = new Vue({ var app = new Vue({
el: '#app', el: '#app',
data: { data: {
...@@ -87,7 +118,8 @@ var app = new Vue({ ...@@ -87,7 +118,8 @@ var app = new Vue({
logout: logout, logout: logout,
loadDirectory: loadDirectory, loadDirectory: loadDirectory,
open: open, open: open,
up: up up: up,
upload: upload
} }
}); });
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!