send/frontend/src/upload.js

45 lines
1.3 KiB
JavaScript
Raw Normal View History

2017-06-02 03:59:27 +00:00
const FileSender = require('./fileSender');
2017-06-01 22:12:30 +00:00
let onChange = event => {
2017-06-02 03:59:27 +00:00
const file = event.target.files[0];
2017-06-01 22:12:30 +00:00
let li = document.createElement('li');
let name = document.createElement('p');
2017-06-02 03:59:27 +00:00
name.innerText = file.name;
li.appendChild(name);
2017-06-01 22:12:30 +00:00
let link = document.createElement('a');
li.appendChild(link);
2017-06-01 22:12:30 +00:00
let progress = document.createElement('p');
li.appendChild(progress);
2017-06-01 22:12:30 +00:00
document.getElementById('uploaded_files').appendChild(li);
2017-06-02 03:59:27 +00:00
const fileSender = new FileSender(file);
fileSender.on('progress', percentComplete => {
progress.innerText = `Progress: ${percentComplete}%`;
});
fileSender.upload().then(info => {
const url = `${window.location
.origin}/download/${info.fileId}/#${info.secretKey}`;
2017-06-02 03:59:27 +00:00
localStorage.setItem(info.fileId, info.deleteToken);
link.innerText = url;
link.setAttribute('href', url);
let btn = document.createElement('button');
btn.innerText = 'Delete from server';
btn.addEventListener('click', () => {
FileSender.delete(
info.fileId,
localStorage.getItem(info.fileId)
).then(() => {
document.getElementById('uploaded_files').removeChild(li);
localStorage.removeItem(info.fileId);
});
});
li.appendChild(btn);
});
2017-06-01 22:12:30 +00:00
};
2017-06-02 03:59:27 +00:00
window.onChange = onChange;