Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/89.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法使用javascript下载大数据_Javascript_Html_Download_Encodeuricomponent - Fatal编程技术网

无法使用javascript下载大数据

无法使用javascript下载大数据,javascript,html,download,encodeuricomponent,Javascript,Html,Download,Encodeuricomponent,我在javascript中有一个JSON对象形式的大数据。我已经使用JSON.stringify()将其转换为字符串。现在,我的用例是在文本文件中向用户提供这个大字符串。为此,我编写了以下代码 HTML代码 <button id='text_feed' type="submit">Generate ION Feed</button> <a href="data:attachment/txt" id="textLink" download="feed.txt

我在javascript中有一个JSON对象形式的大数据。我已经使用JSON.stringify()将其转换为字符串。现在,我的用例是在文本文件中向用户提供这个大字符串。为此,我编写了以下代码

HTML代码

  <button id='text_feed' type="submit">Generate ION Feed</button>

  <a href="data:attachment/txt" id="textLink" download="feed.txt"></a>
 var text = //huge string  

 $("#text_feed").click(function() {
        _generateFeed(text);
 });

 var _generateFeed = function(text) {
    //some code here
    $("#textLink").attr("href",
                          "data:attachment/txt," + encodeURIComponent(text))  [0].click();
    });
 }; 
问题:当字符串长度很小时,我可以下载数据。 但是当字符串长度变长(>10^5)时,我的页面崩溃。 发生这种情况的原因是“encodeUriComponet(text)”无法对大型文件进行编码 数据

我还尝试了
window.open(“数据:attachment/txt,”+encodeURIComponent(text))
但我的页面再次崩溃,原因与encodeURIComponet无法对如此大的字符串进行编码相同

另一种方法:我也在考虑使用HTML5 file write API将数据写入文件,但它仅在Chrome web浏览器中提供支持,但我需要至少在firefox和Chrome两种浏览器中都能做到这一点

用例 我不想通过破坏数据来进行多次下载,因为我最终需要将数据保存在一个文件中

我的目标是支持长度约为10^6的字符串。有谁能帮我把这么多的数据下载/写入一个文件吗

我解决了它如下

var _generateFeed = function(text) {
    /*some code here*/
    var url = URL.createObjectURL( new Blob( [text], {type:'text/plain'} ) );
    $("#textLink").attr("href",url)[0].click();
}; 
注:

  • 与现代浏览器和IE10+兼容,但这是一项不稳定的实验性技术
  • 使用URL.createObjectURL()创建的对象必须在您不再需要它们时通过调用来释放。“-

URL.createObjectURL(新Blob(text,{type:'text/plain'})怎么样-这样行吗?@RGraham同意,试试这个!。Blob编译一兆字节的文本,就像它什么都不是一样。@RGraham和Xufox谢谢。它对我有效:)我解决了它如下
var_generateFeed=function(text){/*此处的一些代码*/var url=url.createObjectURL(新Blob([text],{type:'text/plain'}));$(“#textLink”).attr(“href”,url)[0]。单击()