2

I am trying to import a csv-list of email addresses to a form field with jQuery-csv... Everything works fine but for some strange reason, the array is always missing out the last item (in this case it is: 'adress-6@test.com').

My jQuery:

function process_csv() {

// The event listener for the file upload
document.getElementById('txtFileUpload').addEventListener('change', upload, false);

// Method that checks that the browser supports the HTML5 File API
function browserSupportFileUpload() {
    var isCompatible = false;
    if (window.File && window.FileReader && window.FileList && window.Blob) {
    isCompatible = true;
    }
    return isCompatible;
}

// Method that reads and processes the selected file
function upload(evt) {
if (!browserSupportFileUpload()) {
    alert('The File APIs are not fully supported in this browser!');
    } else {
        var data = null;
        var files = evt.target.files;
        var file = files[0];
        var reader = new FileReader();
        reader.readAsText(file);
        reader.onload = function(event) {
            var csv = event.target.result;
            console.log(csv);
            var data = jQuery.csv.toArrays(csv);
            console.log(data);
            if (data && data.length > 0) {
              alert('Imported -' + data.length + '- rows successfully!');

              var output = '';

              for(var row in data){
                for (var item in data[row]){
                    output += data[row][item] + ', ';
                }
              }
              console.log(output);

            } else {
                alert('No data to import!');
            }
            $("input#field_50euu").val( output );
        };
        reader.onerror = function() {
            alert('Unable to read ' + file.fileName);
        };
    }
}
};

I logged the values to the console, step-by-step. This is what I get:

[Log] adress-1@test.com
adress-2@test.com
adress-3@test.com
adress-4@test.com
adress-5@test.com
adress-6@test.com (mandanteninformation.js, line 28)

[Log] [["adress-1@test.com"], ["adress-2@test.com"], ["adress-3@test.com"], ["adress-4@test.com"], ["adress-5@test.com"]] (5) (mandanteninformation.js, line 30)

[Log] adress-1@test.com, adress-2@test.com, adress-3@test.com, adress-4@test.com, adress-5@test.com,  (mandanteninformation.js, line 41)

When I edit my csv-file and add an empty line at the end, I get all adresses.

Can anyone tell me, what is wrong with my function? I did not find anything on the internet about it.

Thank you!

David
  • 69
  • 8
  • Would you be able to turn this into a [jsfiddle](https://jsfiddle.net/)? – MJH Sep 21 '16 at 11:27
  • what does this line `alert('Imported -' + data.length + '- rows successfully!');` say? – Vinay Sep 21 '16 at 13:28
  • @MJH I tried. I get a bunch of errors there. It is not working. But just in case it helps: https://jsfiddle.net/s5u1vLg2/8/ – David Sep 21 '16 at 13:38
  • @Novice It says 'Imported -5- rows successfully!' when I upload a file. Unfortunately there are 6. You can see it when you look at the console log-file that I posted. At the first Log, there are all adresses. After turning it into an array with the line `var data = jQuery.csv.toArrays(csv);` there are only the first 5 adresses left... – David Sep 21 '16 at 13:42
  • I bet nothing is wrong with your own written code but it may have something to do with the jquery csv plugin which is doing most of the parsing i have also used it couple of times but never have seen such an issue may be you should update the plugin first.Also post a verbatim sample of csv line including any quotes or spaces it may contain. – Vinay Sep 21 '16 at 14:58

1 Answers1

0

I had no problem getting your code (with a few small modifications, including the external files) to output all lines of a small CSV file that has no blank line at the end.

As a test, I created this CSV file:

Col1,Col2,Col3
1,A,Red
2,B,Yellow
3,C,Blue

And then used this jsfiddle to upload it, resulting in an alert that displays all four lines of the CSV file.

Please test and respond with your results. Perhaps the problem lies with your CSV file, and not your code.

MJH
  • 2,301
  • 7
  • 18
  • 20