Split A large JSON into Smaller parts in Javascript/jQuery -
i have file upload functionality in application can not upload json files more 10mb in size. if user uploads file >= 10 mb , app should split smaller json files each less 10mb. also, proper json objects needs maintained in new low-sized files.
is there way in javascript or jquery?
i propose solution without specific library. use bit of modern techniques maybe useful you:
var openfile = function(event, callback) { // target input var input = event.target; // create instance of filereader var reader = new filereader(); // define handler results reader.onload = function(e){ var contents = e.target.result; // use promise maybe make neater callback(contents); }; // make sure tell read text // maybe add validation on input // correct types reader.readastext(input.files[0]); }; var getchunks = function(str){ var chunks = []; // not best @ these things should // around 1mb max var chunksize = 1000000; // while chunk less size indicated goes // same item of array while (str) { if (str.length < chunksize) { chunks.push(str); break; } else { chunks.push(str.substr(0, chunksize)); str = str.substr(chunksize); } } return chunks; } var fileinput = document.queryselector('#jsonupload'); fileinput.addeventlistener('change', function(event){ openfile(event, function(str){ console.log(getchunks(str)); }); }); then read json file from:
<input type='file' accept='*' id="jsonupload">
Comments
Post a Comment