将大数据用于csv文件 - carshes

时间:2016-09-22 11:59:45

标签: javascript csv webclient-download bigdata

我试图将大数据导出到csv文件。 (超过20000行...可以轻松获得超过100000行)。在我尝试下载文件后,崩溃 - 由于网络故障导致下载失败。 (设法下载一个18000行文件,重量为1.7MB,代码运行完美。下载崩溃20000以上)

继承我的代码......谢谢!

编辑 - 在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');

3 个答案:

答案 0 :(得分:0)

警告,URL存在一些限制,例如包含GET的URL的总长度,在IE上不得超过2000+个字符,也许您会使用大型CSV文件达到最大尺寸?
你的URI有多长?请参阅this link about IE URI limit

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

答案 1 :(得分:0)

没关系,只是使用了这个脚本,来自这个网站 - 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);

}

答案 2 :(得分:0)

如果您正在处理大量数据,那么您可能想要寻找一种方法 使用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()
}