2016-09-20 2 views
1

Ich habe ein Array, das ein anderes Array enthält. Ich versuche, dieses Array in einer CSV-Datei über dieses Skript zu speichern, und es funktioniert nicht. Es Download nicht meine Datei ...Speichern von Array-Inhalt als CSV-Datei js

var data =(JSON.stringify(allFileGenesDetails1)); 
    var csvContent = ''; 
    data.forEach(function (infoArray, index) { 
     dataString = infoArray.join(';'); 
     csvContent += index < data.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(csvContent, 'csv file.csv', 'text/csv'); 
+0

Es ist äußerst wichtig zu wissen, welchen Browser Sie verwenden. Safari unterstützt beispielsweise keine JS-erzwungenen Downloads und unterstützt auch nicht das HTML-Attribut "Download". – NoobishPro

Antwort

0

Hier Geige ist https://jsfiddle.net/51fbr4x9/1/ mit Beispielcode I für txt/csv ohne Blob mit Speichern verwenden. Dies ist der Beispielcode:

$(function() { 
    var fileName = "test.csv"; 
    var table = [ 
     "colA1,colB1,colC1", 
     "colA2,colB2,colC2" 
    ]; 

    if ("download" in document.createElement("a")) { 
     var link = $("<a target='_blank' href='data:text/csv;charset=utf-8,%EF%BB%BF" 
     + encodeURI(table.join("\n")) + "' download='" + fileName + "'></a>"); 
     link.appendTo("body"); 
     link[0].click(); 
     setTimeout(function() { 
      link.remove(); 
     }, 50); 
     return; 
    } 

    var txt = $("<textarea cols='65536'></textarea>").get(0); 
    txt.innerHTML = table.join("\n"); 
    var frame = $("<iframe src='text/csv;charset=utf-8' style='display:none'></iframe>").appendTo("body").get(0); 
    frame.contentWindow.document.open("text/csv;charset=utf-8", "replace"); 
    frame.contentWindow.document.write(txt.value); 
    frame.contentWindow.document.close(); 
    frame.contentWindow.document.execCommand("SaveAs", true, fileName); 
    setTimeout(function() { 
     $(frame).remove(); 
     $(txt).remove(); 
    }, 50); 
}); 
+0

Bitte bringen Sie diesen Code hier und nicht ** nur ** auf eine Geige. Fiedeln können verloren gehen, was diese Antwort viel weniger nützlich machen würde. – zero298

+0

Ich habe den Code dorthin gebracht. – xxxmatko