Calculate file size
this fiddle calculates file sizes from bytes to a user friendly string!
by renfley
HTML
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="http://knockoutjs.com/downloads/knockout-2.3.0.js"></script>
Click option to add new calculation
Select WebService
Request Size?
Repsonse Size?
<div class="container">
<div class="col-md-3">
<form>
<label>Response Size? (Bytes) :</label>
<input type="text" id='bytes' data-bind="value:bytes,valueUpdate:'afterkeydown'" class="form-control" />
<p data-bind="visible : bytes()<=0">File size must be greater than 0 </p>
<br />
<div data-bind="visible : bytes()>0">
<label> Equals to :</label>
<span id="size" data-bind="text: size"></span>
</div>
<br>
<label>Total Repsonses Per Day</label>
<input type="text" id='TotalResponses' class="form-control" />
</form>
</div>
</div>
JavaScript
function bytesToSize(bytes) {
var sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
if (bytes == 0) return 'n/a';
var i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)));
if (i == 0) return bytes + ' ' + sizes[i];
return (bytes / Math.pow(1024, i)).toFixed(1) + ' ' + sizes[i];
};
viewModel = function() {
this.bytes = ko.observable(1024);
this.size = ko.computed(function() {
return bytesToSize(this.bytes());
}, this);
}
ko.applyBindings(new viewModel);