下载大尺寸json

时间:2016-10-10 13:28:53

标签: javascript html json

我正在尝试下载大型json数据。 但它会导致Uncaught RangeError: Invalid string length

enter image description here

请帮忙解决这个问题,提前谢谢。

这是Jsfiddle:http://jsfiddle.net/sLq3F/456/

4 个答案:

答案 0 :(得分:2)

您可以使用fetch()Response.body.getReader()返回ReadableStreamTextDecoder()Blob()URL.createObjectURL()

请注意,在RAM Save Save File对话4:20对话后,Save File对话框1:30在{{1}之前经过了4分20秒.crdownload在文件管理器GUI中从文件中删除4:20扩展名之前,对话框已关闭,然后再加1分30秒Save File。在文件下载到文件系统的第一个Save File期间,.crdownload对话框可见,鼠标指针可以移动,但UI暂时无法响应点击或尝试更改标签。当189.8 MB (189,778,220 bytes)对话框关闭且文件仍在下载到文件系统时,如果扩展名为<!DOCTYPE html> <html> <head> <style> code { color:navy; background-color:#eee; padding:2px; } </style> </head> <body> <button>Request File</button><br> <progress min="0" max="189778220" value="0"></progress> <output></output> <br><br> <label></label> <script> var url = "https://raw.githubusercontent.com/zemirco/sf-city-lots-json/master/citylots.json"; var button = document.querySelector("button"); var progress = document.querySelector("progress"); var label = document.querySelector("label"); var output = document.querySelector("output"); var request = (url) => { label.innerHTML = `Requesting <code>${url}</code> at ${new Date()}.<br><br>`; return fetch(url) .then(response => response.body.getReader()) .then(reader => { var decoder = new TextDecoder(); var json = ""; label.innerHTML += `Request successful.<br><br>Reading request body at ${new Date()}.<br><br>`; return reader.read().then(function processData(result) { if (result.done) { // do stuff when `reader` is `closed` return reader.closed.then(function() { // return `json` string return json; }); }; json += decoder.decode(result.value); output.innerHTML = ` ${json.length} of ${progress.max} bytes read`; progress.value = json.length; return reader.read().then(processData) }) .then(function(data) { var message = `Reading of <code>${url}</code> complete at ${new Date()}. <br><br>` + `${data.length} total bytes read. ` + `Please allow up to 4 minutes for file to download ` + `to filesystem after clicking <code>Save</code>.<br><br>`; label.innerHTML += message; var blob = new Blob([data], { type: "application/json" }); var file = URL.createObjectURL(blob); var a = document.createElement("a"); a.download = "citylots.json"; a.href = file; document.body.appendChild(a); a.click(); var closeBlob = (e) => { window.removeEventListener("focus", closeBlob); blob.close(); URL.revokeObjectURL(file); }; window.addEventListener("focus", closeBlob); return message.replace(/<[^>]*>/g, ""); }) .catch(function(err) { console.log("err", err) }) }); } var handleRequest = (e) => { button.setAttribute("disabled", "disabled"); request(url).then(function(message) { console.log(message); button.removeAttribute("disabled"); }) }; button.addEventListener("click", handleRequest); </script> </body> </html>,则UI将恢复正常功能。

在上述过程结束时,文件已成功下载到总大小为// Create a pool $pool = new Pool($this->threads, ParserWorkers::class); // Create a thread class $thread = new class extends Threaded { public function run() { // The class will receive data from a provider // that will be shared between threads through ParserWorkers. // It will work with the API and store the data in the database. // The need to work with the threads, // because the data for processing an incredible amount. echo '+'; } }; // Start a threads for ($i = 0; $i < $this->threads; $i++) { $pool->submit($thread); } $pool->shutdown(); 的本地文件系统。

[13-Oct-2016 11:27:35 Europe/Moscow] PHP Fatal error:  Uncaught Exception: Serialization of 'Closure' is not allowed in [no active file]:0
Stack trace:
#0 {main}
  thrown in [no active file] on line 0

plnkr https://plnkr.co/edit/gewixzHZSKRXquZ2OVF2?p=preview

答案 1 :(得分:0)

我想你需要循环访问JSON文件中的某些内容并将其拆分为更易于管理的字符串。

汤姆

您是否有JSON文件的示例代码段?

答案 2 :(得分:0)

您是否在PHP上运行服务器?

如果你这样做,我相信你必须绝对检查你的PHP.ini文件或运行phpinfo页面,因为php设置确实限制了文件传输的大小,即使它不是外部请求。它可以用其他语言做到这一点但我从来没有遇到过这个问题,除了PHP。

P.S。我没有看到文件的大小

答案 3 :(得分:-1)

我认为你的json文件是“TOO BIG FOR JSON”场景之一。你知道,如果json文件有这么多记录(确切地说,基于一个测试,100000条记录使浏览器挂起,然后在许多浏览器中加载失败),不建议使用它。

您可以阅读本文以获取更多信息:HOW BIG IS TOO BIG FOR JSON?