1

Is there a way to pass a csv file to an Apex method in lightning? There doesn't seem to be a apex:inputFile type of component. I suppose that I can use a standard html input type="file", but how can I get the actual file value (Blob or String) into an apex class?

There is a question about how to clear out an input (How do I reset a lightning file input after submission?) so I imagine that this is possible... I just want to know how to get the value over to the apex class that expects a String/Blob of the file.

1 Answer 1

1

You can use a standard input type="file" and parse the file on the client side. Ideally I would've like to parse the file in Apex, but I ended up converting the CSV to JSON in Javascript. I then can easily pass the JSON string to Apex for processing. Here is some sample controller code. The helper contains a convert CSVtoJSON function that can be easily found online. Here's a fiddle : http://jsfiddle.net/sturtevant/AZFvQ/

upload : function(component, event, helper) {
    var file = component.find("upload").getElement().files[0];
    if (file) {
        var reader = new FileReader();
        reader.readAsText(file, "UTF-8");
        reader.onload = function (evt) {
            var result = helper.CSV2JSON(evt.target.result);
            console.log(JSON.parse(result));
        }
        reader.onerror = function (evt) {
            console.log("error reading file");
        }
    }
}

You must log in to answer this question.

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