简体   繁体   English

encodeURI文件下载 - 崩溃的浏览器

[英]encodeURI file download - crashing browser

I created a web application to clean up CSV/TSV data. 我创建了一个Web应用程序来清理CSV / TSV数据。 The app allows me to upload a CSV file, read it, fix data, and then download a new CSV file with the correct data. 该应用程序允许我上传CSV文件,读取,修复数据,然后下载包含正确数据的新CSV文件。 One challenge I have run into is downloading files with more than ~ 2500 lines. 我遇到的一个挑战是下载超过2500行的文件。 The browser crashes with the following error message: 浏览器崩溃并显示以下错误消息:

"Aw, Snap! Something went wrong while displaying this webpage..."

To work around this I have changed the programming to download multiple CSV files not exceeding 2500 lines until all the data is downloaded. 为了解决这个问题,我已经改变了编程,下载了多个不超过2500行的CSV文件,直到下载完所有数据。 I would then put together the downloaded CSV files into a final file. 然后,我将下载的CSV文件放在最终文件中。 That's not the solution I am looking for. 这不是我要找的解决方案。 Working with files of well over 100,000 lines, I need to download all contents in 1 file, and not 40. I also need a front-end solution. 使用超过100,000行的文件,我需要将所有内容下载到1个文件中,而不是40个。我还需要一个前端解决方案。

Following is the code for downloading the CSV file. 以下是下载CSV文件的代码。 I am creating a hidden link, encoding the contents of data array (each element has 1000 lines) and creating the path for the hidden link. 我正在创建一个隐藏的链接,编码数据数组的内容(每个元素有1000行)并创建隐藏链接的路径。 I then trigger a click on the link to start the download. 然后我触发点击链接开始下载。

var startDownload = function (data){
    var hiddenElement = document.createElement('a');
    var path = 'data:attachment/tsv,';
    for (i=0;i<data.length;i++){
        path += encodeURI(data[i]);
    }
    hiddenElement.href = path;
    hiddenElement.target = '_blank';
    hiddenElement.download = 'result.tsv';
    hiddenElement.click();
}

In my case the above process works for ~ 2500 lines at a time. 在我的情况下,上述过程一次适用于~2500行。 If I attempt to download bigger files, the browser crashes. 如果我尝试下载更大的文件,浏览器会崩溃。 What am I doing wrong, and how can I download bigger files without crashing the browser? 我做错了什么,如何在不崩溃浏览器的情况下下载更大的文件? The file that is crashing the browser has (12,000 rows by 48 columns) 崩溃浏览器的文件有(12,000行乘48列)

ps I am doing all of this in Google Chrome, which allows for file upload. ps我在谷歌浏览器中做了所有这些,允许文件上传。 So the solution should work in Chrome. 因此,解决方案应该适用于Chrome。

I've experienced this problem before and the solution I found was to use Blob s to download the CSV. 我之前遇到过这个问题,我找到的解决方案是使用Blob来下载CSV。 Essentially, you turn the csv data into a Blob, then use the URL API to create a URL to use in the link, eg: 基本上,您将csv数据转换为Blob,然后使用URL API创建要在链接中使用的URL,例如:

var blob = new Blob([data], { type: 'text/csv' });
var hiddenElement = document.createElement('a');
hiddenElement.href = window.URL.createObjectURL(blob);

Blobs aren't supported in IE9 , but if you just need Chrome support you should be fine. IE9不支持 Blob,但如果您只需要Chrome支持,则应该没问题。

I also faced same problem. 我也面临同样的问题。 I used this code,it will works fine. 我使用这个代码,它会正常工作。 You can also try this. 你也可以试试这个。

if (window.navigator.msSaveBlob) {
    window.navigator.msSaveBlob(new Blob([base64toBlob($.base64.encode(excelFile), 'text/csv')]),'data.csv');
} else {
var link = document.createElement('a');
link.download = 'data.csv';
// If u use chrome u can use webkitURL in place of URL
link.href = window.URL.createObjectURL(new Blob([base64toBlob($.base64.encode(excelFile), 'text/csv')]));
link.click();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM