0

I am trying to read the CSV values in apex. I am getting the base64 file in apex, however not able to read the data.

Helper method:

uploadHelper: function(component, event) {

        var fileInput = component.find("fileId").get("v.files");
        // get the first file using array index[0]  
        var file = fileInput[0];

        var action = component.get("c.passFile");
        // create a FileReader object 
        var objFileReader = new FileReader();
        // set onload function of FileReader object   
        objFileReader.onload = $A.getCallback(function() {
            var fileContents = objFileReader.result; 
            action.setParams({ file : encodeURIComponent(objFileReader.result) } );
            action.setCallback(self, function(actionResult) {
                console.log((actionResult.getReturnValue()));
            });
            $A.enqueueAction(action);            
        });

        objFileReader.readAsDataURL(file);

    }

Class:

 @AuraEnabled
    public static void passFile(String file)
    {
        //System.debug(LoggingLevel.Error,'Inside Apex '+ file);
        String myFile = EncodingUtil.urlDecode(file, 'UTF-8');
        System.debug('Inside Apex '+myFile);

    }

Result:

Inside Apex

data:application/vnd.ms-excel;base64,SUQsUGVybWlzc2lvbiBTZXQgSUQNCjJGMDA1MzgwMDAwMDVnUWZ5QUFFLDBQUzB0MDAwMDAwNExjZUdBRQ0KMDA1MzgwMDAwMDRmT2lvLDBQUzB0MDAwMDAwNExjZUdBRQ0KMDA1NTAwMDAwMDNRVWE2LDBQUzB0MDAwMDAwNExjZUdBRQ0KMDA1NTAwMDAwMDJRUDZsLDBQUzB0MDAwMDAwNExjZUdBRQ0KMDA1MzgwMDAwMDUwN3lnLDBQUzB0MDAwMDAwNExjZUdBRQ0KMDA1MzgwMDAwMDUwODk1QUFBLDBQUzB0MDAwMDAwNExjZUdBRQ0KMDA1MzgwMDAwMDUwODg3QUFBLDBQUzB0MDAwMDAwNExjZUdBRQ0KMDA1MzgwMDAwMDVmaUd5LDBQUzB0MDAwMDAwNExjZUdBRQ0KMDA1NTAwMDAwMDNQOUpKLDBQUzB0MDAwMDAwNExjZUdBRQ0KMDA1MzgwMDAwMDVkSmdULDBQUzB0MDAwMDAwNExjZUdBRQ0KMDA1MzgwMDAwMDU4ZjRqLDBQUzB0MDAwMDAwNExjZUdBRQ0KM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0KMDA1MzgwMDAwMDRvS3hkLDBQUzB0MDAwMDAwNExjZUdBRQ0KMDA1MzgwMDAwMDNNVWFnLDBQUzB0MDAwMDAwNExjZUdBRQ0KMDA1NTAwMDAwMDFmR3l3LDBQUzB0MDAwMDAwNExjZUdBRQ0KMDA1NTAwMDAwMDFvZDRhLDBQUzB0MDAwMDAwNExjZUdBRQ0KMDA1MzgwMDAwMDRuMDA1LDBQUzB0MDAwMDAwNExjZUdBRQ0KMDA1MzgwMDAwMDRhRFQ0LDBQUzB0MDAwMDAwNExjZUdBRQ0KMDA1NTAwMDAwMDFDc0ZrLDBQUzB0MDAwMDAwNExjZUdBRQ0KMDA1MzgwMDAwMDRybXI1LDBQUzB0MDAwMDAwNExjZUdBRQ0KMDA1MzgwMDAwMDNOaVhkLDBQUzB0MDAwMDAwNExjZUdBRQ0KMDA1MzgwMDAwMDRhVzdILDBQUzB0MDAwMDAwNExjZUdBRQ0KMDA1MzgwMDAwMDRpQXhILDBQUzB0MDAwMDAwNExjZUdBRQ0KMDA1MzgwMDAwMDVlRkVELDBQUzB0MDAwMDAwNExjZUdBRQ0KMDA1NTAwMDAwMDNCVGNELDBQUzB0MDAwMDAwNExjZUdBRQ0KMDA1MzgwMDAwMDVKUlNGLDBQUzB0MDAwMDAwNExjZUdBRQ0KMDA1MzgwMDAwMDVSVVdMLDBQUzB0MDAwMDAwNExjZUdBRQ0KMDA1NTAwMDAwMDFwS1ZLLDBQUzB0MDAwMDAwNExjZUdBRQ0KMDA1NTAwMDAwMDFBYUl3QUFLLDBQUzB0MDAwMDAwNExjZUdBRQ0KMDA1NTAwMDAwMDJmOTRpLDBQUzB0MDAwMDAwNExjZUdBRQ0KMDA1MzgwMDAwMDVLMmNDLDBQUzB0MDAwMDAwNExjZUdBRQ0KMDA1NTAwMDAwMDRLOXZsLDBQUzB0MDAwMDAwNExjZUdBRQ0K

I tried decoding using same with "https://www.base64decode.org/", it works perfectly fine. However I am unable to read the data in apex. May be I am missing something here.

3 Answers 3

4

You need to take data and base-64 decode it:

String myFile = EncodingUtil.base64Decode(file.split(',')[1]).toString();

Alternatively, you could have decoded it in JavaScript and sent the string in directly.


Also, do not encode the data:

    objFileReader.onload = $A.getCallback(function() {
        var fileContents = objFileReader.result; 
        action.setParams({ file : objFileReader.result });
        action.setCallback(self, function(actionResult) {
            console.log((actionResult.getReturnValue()));
        });
        $A.enqueueAction(action);            
    });
2
  • getting "System.ListException: List index out of bounds: 1", probably decoding at javascript is better option I believe as you said.
    – Ajay
    Commented Apr 12, 2018 at 22:46
  • 1
    @Ajay You shouldn't be using encodeURIComponent on the client side, though.
    – sfdcfox
    Commented Apr 12, 2018 at 22:49
4

Use:

EncodingUtil.base64Decode(file).toString()

Instead of:

EncodingUtil.urlDecode(file, 'UTF-8')
2
  • I am getting "Illegal assignment from Blob to String" error.
    – Ajay
    Commented Apr 12, 2018 at 22:18
  • Updated with a link to documentation, that should help you move in the right direction.
    – gNerb
    Commented Apr 12, 2018 at 22:20
0

Finally decoded at client side:

b64DecodeUnicode: function (component, str) {
    return decodeURIComponent(atob(str).split('').map(function(c) {
        return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
    }).join(''));
    }

Thanks @sfdcfox for the suggestion.

4
  • If you used someone else's answer, accept their answer, do not post your own answer to take credit.
    – gNerb
    Commented Apr 13, 2018 at 15:09
  • @gNerb: I don't take any credit here, Please be gentle when you are replying. I don't run behind the credits. I took the sfdcfox suggestion and however I implemented on my own. If that is not appropriate, you can tell me that in a gentle way. Please be noted that, I also like to contribute to community, not for credit. I am really not happy the way you have replied here. If Admin asks, I can give whatever credit I have with me to anyone.
    – Ajay
    Commented Apr 13, 2018 at 19:26
  • Try to understand that if everyone posted an answer after "implementing it on their own" then everyone would be answering their own questions all the time. Of course you "implemented it on your own". SFDC Fox gave you the solution. You need to click the check mark next to his answer, not post your own.
    – gNerb
    Commented Apr 13, 2018 at 20:05
  • You need to understand here, that I posted my answer because someone else will be benefited not for the credit. That is what I wanted to clarify. I did not accept post as answer. Please check.
    – Ajay
    Commented Apr 13, 2018 at 21:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .