papa parse - unescape imported data

papa parse - unescape imported data

montoyammontoyam Posts: 568Questions: 136Answers: 5

I am using papa parse in my project to import a csv file. I am having an issue though because values are coming in escaped, so an ampersand is coming in as & I see that javascript has an 'unescape()' function,but not sure how to use it with the papa parse function:

            var uploadEditor = new $.fn.dataTable.Editor({
                fields: [{
                    label: 'CSV file:',
                    name: 'csv',
                    type: 'upload',
                    ajax: function (files) {
                        // Ajax override of the upload so we can handle the file locally. Here we use Papa
                        // to parse the CSV.
                        Papa.parse(files[0], {
                            header: true,
                            //change the 4th column header to 'Email'
                            beforeFirstChunk: function (chunk) {
                                var index = chunk.match(/\r\n|\r|\n/).index;
                                var headings = chunk.substr(0, index).split(',');
                                headings[3] = 'Email';
                                return headings.join() + chunk.substr(index);
                            },
                            skipEmptyLines: true,
                            delimiter: ",",
                            complete: function (results) {
                                if (results.errors.length) {
                                    console.log(results);
                                    uploadEditor.field('csv').error('CSV parsing error: ' + results.errors[0].message);
                                }
                                else {
                                    uploadEditor.close();
                                    selectColumns(editor, results.data, results.meta.fields);
                                }
                            }
                        });
                    }
                }]
            });

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 61,451Questions: 1Answers: 10,055 Site admin

    You could loop over the results.data array and decode each in turn there.

    PapaParse might have something that will help you do this itself, but I don't know I'm afraid, You'd need to ask in their support channels.

    Allan

  • montoyammontoyam Posts: 568Questions: 136Answers: 5
    edited June 2020

    actually, it seems that papa parse is not where the issue is? On dataTable submit I try this:

                selectEditor.on('submitComplete', function (e, json, data, action) {
                    // Use the host Editor instance to show a multi-row create form allowing the user to submit the data.
                    editor.create(csv.length, {
                        title: 'Confirm import',
                        buttons: 'Submit',
                        message: 'Click the <i>Submit</i> button to confirm the import of ' + csv.length + ' rows of data.'
                    });
    
    
                    for (var i = 0; i < fields.length; i++) {
                        var field = editor.field(fields[i]);
                        var mapped = data[field.name()];
                        console.log(mapped);
                        for (var j = 0; j < csv.length; j++) {
                            console.log(unescape(csv[j][mapped]));
                            field.multiSet(j, unescape(csv[j][mapped]));
                        }
                    }
                    
    
                });
    

    and console.log(unescape(csv[j][mapped])); is showing the data correctly. Is it something to do with multiset() or with the editor, or the way I have my controller (MVC project) written?

  • montoyammontoyam Posts: 568Questions: 136Answers: 5

    actually, even if I leave off the unescape() it appears correctly in the console log.

    console.log((csv[j][mapped]));
    
  • montoyammontoyam Posts: 568Questions: 136Answers: 5

    and just to confirm, I am looking at the raw csv file and the ampersands are showing correctly. It appears that something is going on when it sends it to SQL

  • allanallan Posts: 61,451Questions: 1Answers: 10,055 Site admin
    Answer ✓

    Are you using .NET on the server-side? (I feel I should remember from your previous posts, but I can't!). If so, use .Xss(false) on the Field elements which will disable the Microsoft anti-XSS library. It sounds like you might be running into an issue with it being overly aggressive in its encoding.

    Allan

  • montoyammontoyam Posts: 568Questions: 136Answers: 5

    yes!! that was it. thank you so much!

    .Field(new Field("Department").Xss(false))
    
This discussion has been closed.