Mercurial > hg > fapweb
changeset 842:8ec53995e64d
Preliminary javascript code for file uploads.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Tue, 25 Nov 2014 20:46:50 +0200 |
parents | f007e63f82d6 |
children | ab2697983ab0 |
files | ajax.js |
diffstat | 1 files changed, 60 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- a/ajax.js Tue Nov 25 20:43:36 2014 +0200 +++ b/ajax.js Tue Nov 25 20:46:50 2014 +0200 @@ -215,3 +215,63 @@ return true; } + + +function jsFormatSize(bytes) +{ + var suffixes = ["Bytes", "KiB", "MiB"]; + var i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024))); + return (bytes / Math.pow(1024, i)).toFixed(1) +' '+ suffixes[i]; +} + + +function jsFileUploadSelected(id, maxSize) +{ +} + + +function jsStartFileUpload(id, tgt) +{ + var elem = document.getElementById(id); + //var fd = document.getElementById(id).getFormData(); // for FF3 + var fd = new FormData(elem); + + var req = jsCreateXMLRequest(); + req.upload.addEventListener('progress', jsUploadProgress, false); + req.addEventListener('load', jsUploadFinished, false); + req.addEventListener('error', jsUploadError, false); + req.addEventListener('abort', jsUploadAbort, false); + req.open('POST', tgt); + req.send(fd); +} + + +function jsUploadProgress(e) +{ + if (e.lengthComputable) + { + var complete = Math.round(e.loaded * 100 / e.total); + if (complete < 100) + jsStatusMsg("Uploaded "+ complete.toString() +'%, '+ jsFormatSize(e.loaded)); + else + jsStatusMsg("Upload finished ..."); + } +} + + +function jsUploadFinished(e) +{ + jsMessageBox(e.target.responseText); +} + + +function jsUploadError(e) +{ + jsMessageBox("Error occured while uploading."); +} + + +function jsUploadAbort(e) +{ + jsMessageBox("File upload aborted."); +}