Using this Methode we can get the CSV file values to the JavaScript.
parseCSV: function parseCSV() {
// once we are deep in callbacks, we will need this variable to access this file's particular context
// essentially how we access the file API for the selected file
var fileInput = document.getElementById("csvinput");
//Possible file formats
var valid_CSV_Formats = [
"text/csv",
"text/plain",
"application/csv",
"text/comma-separated-values",
"application/excel",
"application/vnd.ms-excel",
"application/vnd.msexcel",
"text/anytext",
"application/octet-stream",
"application/txt",
];
// check that there is one (and only one) file and that its type is a CSV file format
if (
fileInput.files.length === 1 &&
valid_CSV_Formats.includes(fileInput.files[0].type)
) {
// call PapaParse
Papa.parse(fileInput.files[0], {
// when it's done parsing, do this
complete: function (parsedLines) {
var sku_array = [];
var container
//creating a new key 'internalid' in the obj
for (var i = 0; i < parsedLines.data.length; i++) {
parsedLines.data[i].internalid = parsedLines.data[i].productId;
}
//Fetching sku value from the main obj
for (var i = 0; i < parsedLines.data.length; i++) {
sku_array.push(parsedLines.data[i].internalid);
}
console.log('Test-Rohit this',this);
console.log("Test-Rohit sku_array", sku_array);
var itemApi= "https://www.scatest1409.publicvm.com/api/items?fieldset=details"
console.log('Test-Rohit itemApi',itemApi);
var cart = container.getComponent("Cart");
cart.addLines({
lines: [
{
quantity: 1,
item: {
internalid: 8050,
},
},
{
quantity: 1,
item: {
internalid: 8044,
},
},
],
});
//Request to server to get internlaid correponding to the sku value
this.model = new ImportItemSS2Model();
var self = this;
this.model
.fetch({
sku_array: JSON.stringify(sku_array),
})
.done(function (result) {
console.log("Test-Rohit result", result);
})
.catch(function (error) {
console.log("Test-Rohit error", error);
});
},
header: true,
});
// if they click submit without selecting a file
} else if (fileInput.files.length === 0) {
self.showError(_.translate("Please select a file to parse"));
}
// if they select a file that isn't a CSV
else if (fileInput.files[0].type !== "text/csv") {
self.showError(_.translate("Please select a valid CSV file"));
}
},