【问题标题】:expoting big data to csv file - carshes将大量数据导出到 csv 文件 - 崩溃
【发布时间】:2017-01-30 23:55:49
【问题描述】:

我正在尝试将大数据导出到 csv 文件。 (超过 20000 行......可以轻松达到超过 100000 行)。在我尝试下载文件后,它崩溃了 - 由于网络故障,下载失败。 (设法下载了一个 18000 行的文件,重 1.7MB,代码运行良好。超过 20000 下载崩溃)

这是我的代码...谢谢!

edit-适用于 IE,不适用于 chrome

var data2 =     [[data12]];
var csvContent2 = "";
data2.forEach(function (infoArray, index) {

    dataString = Array.prototype.join.call(infoArray, "");
    csvContent2 += index < data2.length ? dataString + '\n' : dataString;

});

var download = function(content, fileName, mimeType) {
var a = document.createElement('a');
    mimeType = mimeType || 'application/octet-stream';

    if (navigator.msSaveBlob) { // IE10
        return navigator.msSaveBlob(new Blob([content], { type: mimeType }), fileName);
    } else if ('download' in a) { //html5 A[download]
        a.href = 'data:' + mimeType + ',' + encodeURIComponent(content);
        a.setAttribute('download', fileName);
        document.body.appendChild(a);
        setTimeout(function() {
        a.click();
        document.body.removeChild(a);
        }, 66);
    return true;
} else { //do iframe dataURL download (old ch+FF):
    var f = document.createElement('iframe');
    document.body.appendChild(f);
    f.src = 'data:' + mimeType + ',' + encodeURIComponent(content);

    setTimeout(function() {
        document.body.removeChild(f);
    }, 333);
    return true;
    }
}

    download(csvContent2, 'GroupB.csv', 'text/csv');

【问题讨论】:

  • 尝试不删除您为附加 Blob 而创建的 &lt;a&gt; 标签...也许标签和 blob 之间存在某种链接;)
  • 我认为添加您收到的错误消息是个好主意,因为“崩溃”本身并不能解释太多..
  • 我收到错误-由于网络故障导致下载失败
  • 我不知道...如何查看?无论如何,它在 IE 中工作......但在 chrome 中崩溃......
  • 复制源代码中的链接,然后粘贴到可以统计字符的软件中(比如word)。

标签: javascript csv webclient-download bigdata


【解决方案1】:

警告,URL 有一些限制,例如,包括 GET 在内的 URL 总长度,在 IE 上不得超过 2000+ 个字符,也许你会使用大的 CSV 文件达到最大大小?
你的 URI 有多长?见this link about IE URI limit

编辑: 也许你可以试试这个createObjectURL(blob),但这不是一个稳定的功能:x

【讨论】:

    【解决方案2】:

    没关系,只是使用了这个脚本,来自这个网络-http://jsfiddle.net/4zv6r/

               function cloneAndConvert(){
    
    var jsonData = [{firstName:"Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum", lastName:"Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum", age:46}];
    for(i=0;i<90000;i++)
    {     
       jsonData.push({firstName:"Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum"+i, lastName:"Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum"+i, age:46});
    }
    
    var filteredGridData = JSON.parse(JSON.stringify(jsonData))
    JSONToCSVConvertor(filteredGridData, "UserReport.csv", true);
    

    }

    function JSONToCSVConvertor(JSONData, ReportTitle, ShowLabel) {

    //If JSONData is not an object then JSON.parse will parse the JSON string in an Object
    var arrData = typeof JSONData != 'object' ? JSON.parse(JSONData) : JSONData;
    var CSV = '';    
    //This condition will generate the Label/Header
    if (ShowLabel) {
        var row = "";
    
        //This loop will extract the label from 1st index of on array
        for (var index in arrData[0]) {
            //Now convert each value to string and comma-seprated
            row += index + ',';
        }
        row = row.slice(0, -1);
        //append Label row with line break
        CSV += row + '\r\n';
    }
    
    //1st loop is to extract each row
    for (var i = 0; i < arrData.length; i++) {
        var row = "";
        //2nd loop will extract each column and convert it in string comma-seprated
        for (var index in arrData[i]) {
            row += '"' + arrData[i][index] + '",';
        }
        row.slice(0, row.length - 1);
        //add a line break after each row
        CSV += row + '\r\n';
    }
    
    if (CSV == '') {        
        alert("Invalid data");
        return;
    }   
    
    //this trick will generate a temp "a" tag
    var link = document.createElement("a");    
    link.id="lnkDwnldLnk";
    
    //this part will append the anchor tag and remove it after automatic click
    document.body.appendChild(link);
    
    var csv = CSV;  
    blob = new Blob([csv], { type: 'text/csv' }); 
    var csvUrl = window.webkitURL.createObjectURL(blob);
    var filename = 'UserExport.csv';
    $("#lnkDwnldLnk")
    .attr({
        'download': filename,
        'href': csvUrl
    }); 
    
    $('#lnkDwnldLnk')[0].click();    
    document.body.removeChild(link);
    

    }

    【讨论】:

      【解决方案3】:

      如果您正在处理大量数据,那么您可能想寻找一种方法来处理 使用StreamSaver处理内存更友好

      async function download(){
          const fileStream = streamSaver.createWriteStream('filename.csv')
          const writer = fileStream.getWriter()
          const encoder = new TextEncoder
      
          for(let row of rows){
              await writer.ready()
              let binary = encoder.encode(data + "\r\n")
              writer.write(uint8array)
          }
      
          writer.close()
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-06-11
        • 2014-09-20
        • 2014-09-13
        • 2015-07-07
        • 1970-01-01
        • 1970-01-01
        • 2021-07-11
        相关资源
        最近更新 更多