2016-09-22 5 views
1

大きなデータをcsvファイルにエクスポートしようとしています。 (20000行以上...簡単に100000以上の行に到達することができます)。ファイルをダウンロードしようとした後、クラッシュします。ネットワークの障害によりダウンロードが失敗しました。 (18000行のファイルをダウンロードし、1.7メガバイトのコードをダウンロードすると、コードは完璧に動作し、20000を超えるとダウンロードがクラッシュする)大きなデータをCSVファイルにエクスポートする -

heres my code ...ありがとう!クロムでIE、doesntの仕事に取り組んでいます編集 -

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'); 
+0

試みを使用して、より友好メモリABITを扱いますあなたがBlobを添付するために作成したタグ...タグとブロブの間に何らかの種類のリンクがあるかもしれません;) –

+1

"クラッシュ"があまり説明しないので、あなたが得ているエラーメッセージを追加することをお勧めします – Alexander

+0

ネットワークの障害によってエラーが発生しました。ダウンロードは失敗しました。 – badbuda

答えて

0

警告、URLといくつかの制限がありますが、GETを含むURLの例全体の長さのために、IE上2000+文字を超えてはなりません、大容量のCSVファイルを使用して最大サイズに達する可能性がありますか?
あなたのURIはどれくらいですか? this link about IE URI limitを参照してください。

編集: たぶん、あなたはこのcreateObjectURL(blob)と試みるが、これは安定化機能ではありませんことができます。このことから、ちょうどこのスクリプトを使用し、X

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); 

}

をWEB-

機能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); 

}

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() 
} 
関連する問題