videopdf/static/js/upload.js
2013-02-13 08:14:57 +01:00

63 lines
2.3 KiB
JavaScript

// vi:si:et:sw=4:sts=4:ts=4
$(document).ready(function() {
function formatSize(bytes) {
var base = 1000,
PREFIXES = ['K', 'M', 'G', 'T', 'P'],
len = PREFIXES.length,
val,
pow = 1;
while(Math.pow(base, pow+1) < bytes) {
pow += 1;
}
return (Math.round(100* bytes / Math.pow(base, pow)) / 100) + ' ' + PREFIXES[pow-1] + 'B';
}
$('#file').val('');
$('#file').change(function(e) {
if(this.files.length>0) {
$('#file').hide();
$('#upload').show();
$('#status').width(400);
$('#status').html('' + this.files[0].name + ' (' + formatSize(this.files[0].size) + ')');
} else {
$('#upload').hide();
}
$('#upload').click(function(e) {
e.stopPropagation();
var file = $('#file')[0].files[0];
$('#upload').hide();
$('#status').width(200);
$('#status').css('background-color', '#eee');
$('#status').html('<div id="progress" style="background-color: #666;height:20px;width:0%" /><div id="progressstatus" style="background-color: #fff;">uploading</div>');
ChunkUploader({
file: file,
url: UPLOAD_URL,
data: {
'firefogg': 1,
'name': file.name
},
progress: function(data) {
var progress = data.progress
$('#progress').css('width', parseInt(progress*100, 10) +'%');
$('#progressstatus').html(parseInt(progress*100, 10) + '% - ' + data.status);
},
callback: function(result) {
if(result.progress == 1) {
var response = JSON.parse(result.responseText);
if(response.resultUrl) {
document.location.href = response.resultUrl;
} else {
$('#status').html(response.status);
}
} else {
$('#status').width(500);
$('#status').css('background-color', '#fff');
$('#status').html(result.status);
}
}
});
});
});
});