ank*_*r37 11 javascript html5 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"></a>
Run Code Online (Sandbox Code Playgroud)
Javascript代码
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();
});
};
Run Code Online (Sandbox Code Playgroud)
问题:当字符串长度很小时,我可以下载数据.但是当字符串长度变得更高(> 10 ^ 5)时,我的页面崩溃了.发生这种情况是因为"encodeUriComponet(text)"无法编码大数据.
我也尝试了window.open("data:attachment/txt," + encodeURIComponent(text));
但是我的页面再次崩溃,因为encodeURIComponet无法编码这么大的字符串的原因相同.
另一种方法:我还考虑使用HTML5文件编写API将数据写入文件,但它仅支持Chrome浏览器,但我需要将其用于至少firefox和chrome.
用例 我不想通过破坏数据来进行多次下载,因为我最终需要在单个文件中存储数据.
我的目标是支持大约10 ^ 6长度的字符串.任何人都可以帮我如何下载/写入这些数据到一个文件.
来自OP:
我如下解决了它。
Run Code Online (Sandbox Code Playgroud)var _generateFeed = function(text) { /*some code here*/ var url = URL.createObjectURL( new Blob( [text], {type:'text/plain'} ) ); $("#textLink").attr("href",url)[0].click(); };
笔记:
归档时间: |
|
查看次数: |
2590 次 |
最近记录: |