diff options
author | Jon Jenkins <jondjenkins@gmail.com> | 2014-03-30 11:09:54 -0600 |
---|---|---|
committer | Jon Jenkins <jondjenkins@gmail.com> | 2014-03-30 11:09:54 -0600 |
commit | 4790cc3fb9a7bbdd88305247e19f4089cf989a16 (patch) | |
tree | 6da777ac5890e8a0d1bdb3ab4c0f37816270ff13 /static/js/upload.js | |
download | express-upload-4790cc3fb9a7bbdd88305247e19f4089cf989a16.tar.gz express-upload-4790cc3fb9a7bbdd88305247e19f4089cf989a16.tar.bz2 express-upload-4790cc3fb9a7bbdd88305247e19f4089cf989a16.zip |
initial commit
Diffstat (limited to 'static/js/upload.js')
-rw-r--r-- | static/js/upload.js | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/static/js/upload.js b/static/js/upload.js new file mode 100644 index 0000000..26b2117 --- /dev/null +++ b/static/js/upload.js @@ -0,0 +1,49 @@ +$(function () { + status('choose a file'); + var timerId; + + function setTimer() { + timerId = setInterval(function () { + if ($('#userFileInput').val() !== '') { + clearInterval(timerId); + $('#uploadForm').submit(); + } + }, 500); + } + + function setProgress(percent) { + $('#percent').html(percent + '%'); + $('#bar').css('width', percent + '%'); + } + + setTimer(); + $('#uploadForm').submit(function () { + status('0%'); + var formData = new FormData(); + var file = document.getElementById('userFileInput').files[0]; + formData.append('userFile', file); + var xhr = new XMLHttpRequest(); + xhr.overrideMimeType('application/json'); + xhr.open('post', '/api/upload', true); + xhr.upload.onprogress = function (e) { + if (e.lengthComputable) + setProgress(Math.round((e.loaded / e.total) * 100)); + }; + xhr.onerror = function (e) { + status('error while trying to upload'); + }; + xhr.onload = function () { + $('#userFileInput').val(''); + setProgress(0); + var resJson = JSON.parse(xhr.responseText); + status(resJson.file + ' done, choose a file'); + setTimer(); + window.open('./uploads/' + resJson.savedAs, 'upload', 'status=1, height = 300, width = 300, resizable = 0'); + }; + xhr.send(formData); + return false; // no refresh + }); + function status(message) { + $('#status').text(message); + } +});
\ No newline at end of file |