[SalesForce] Upload CSV file in lightning component

I need to upload CSV file(standard template with data to create new opportunity records) in lightning component and read this csv file in apex and insert opportunity records. Please find my code.

JSHelper:

    uploadHelper :function(component, event) {
    var fileInput = component.find("fileId").get("v.files");
    var file = fileInput[0];
    var self = this;
    var objFileReader = new FileReader();
    objFileReader.onload = $A.getCallback(function() {
    var fileContents = objFileReader.result;
    var base64 = 'base64,';
    var dataStart = fileContents.indexOf(base64) + base64.length;
    fileContents = fileContents.substring(dataStart);
    self.processHelper(component,event,file,fileContents);
    });
    objFileReader.readAsDataURL(file);
    },

    processHelper : function(component,event,file,fileContents) {
    var action = component.get("c.onProcessBatch");
    action.setParams({
    "fileContents":encodeURIComponent(fileContents)
    });
    action.setCallback(this, function(response) {
    var state = response.getState();
    if (state === "SUCCESS") {
    }
    else if (state === "INCOMPLETE") {

            }
                else if (state === "ERROR") {
                    var errors = response.getError();
                    if (errors) {
                        if (errors[0] && errors[0].message) {
                            console.log("Error message: " + 
                                        errors[0].message);
                        }
                    } else {
                        console.log("Unknown error");
                    }
                }
        });
        $A.enqueueAction(action);
},

}

Apex class:

     @AuraEnabled
     public static void onProcessBatch(String fileContents)
     {
      String stringFile = EncodingUtil.urlDecode(fileContents, 'UTF-8');
      system.debug('stringFile'+stringFile);
      }

The above code gives the following value in system debug.
TmFtZSxDdXJyZW5jeUlzb0NvZGUsU3RhcnREYXRlX19jLEVuZERhdGVfX2MNCk9wcG9ydHVuaXR5IE5hbWUsQ3VycmVuY3lJc29Db2RlLCIgClN0YXJ0RGF0ZSIsIiAKRW5kRGF0ZSINCk9wcG9ydHVuaXR5IDEsVVNELDMxLTEyLTIwMTgsMzEtMTItMjAxOA0K

When decoded the above value in https://www.base64decode.org/ , I am getting the values from csv.
But in apex class, I am getting only the above values. I tried converting them to blob and again converting them to String but nothing worked out.

How to get the entered values from the above format. Could anyone please help me?

Best Answer

Okay, so this was a little bit of a weird one to try and solve,

// The base64 you provided
String base64 ='TmFtZSxDdXJyZW5jeUlzb0NvZGUsU3RhcnREYXRlX19jLEVuZERhdGVfX2MNCk9wcG9ydHVuaXR5IE5hbWUsQ3VycmVuY3lJc29Db2RlLCIgClN0YXJ0RGF0ZSIsIiAKRW5kRGF0ZSINCk9wcG9ydHVuaXR5IDEsVVNELDMxLTEyLTIwMTgsMzEtMTItMjAxOA0K'

// decode 
Blob decodedBlob = EncodingUtil.base64Decode(base64);

// convert to string
String result = decodedBlob.toString();

system.debug(result);

You have now passed the easy part of this... the challenging part is going to be iterating the values to insert them into an object!

Let me know how you go!

Your Method converted:

 @AuraEnabled
 public static void onProcessBatch(String fileContents){
     Blob decodedBlob = EncodingUtil.base64Decode(base64);
     String result = decodedBlob.toString();

     // this will split into a few lines on the system debug! 
     system.debug(result);
 }
Related Topic