无法使用Lightning组件中的FileReader对象读取和上传文件

问题描述 投票:0回答:1

我正在尝试使用Lightning输入组件将文件上传到Salesforce:

Component

<aura:component controller="FileUploadController">

<lightning:input 
    aura:id="fileId" 
    onchange="{!c.readFile}" 
    type="file" 
    name="file" 
    multiple="false"/>

</aura:component>

Controller

({
    readFile : function(component) {
        var file = component.find("fileId").get("v.files")[0];
        var reader  = new FileReader();

        reader.onload = function(e) {
            var fileContent = e.target.result;
            var base64 = 'base64,';
            var dataStart = fileContent.indexOf(base64) + base64.length;

            fileContent = fileContent.substring(dataStart);
            uploadFile(file, fileContent, component);
        }
        reader.readAsDataURL(file); 
    },

    uploadFile : function(file, fileContent, component) {
        var action = component.get("c.createFile"); 

        action.setParams({
            fileName: file.name,
            base64Data: encodeURIComponent(fileContent)
        });

        $A.enqueueAction(action);
    }
})

我遇到的问题是我无法从readFile()中调用uploadFile()。任何建议都很好。谢谢。

javascript salesforce salesforce-lightning
1个回答
0
投票

我最终将uploadFile方法移到了一个帮助器类,并且解决了该问题:

© www.soinside.com 2019 - 2024. All rights reserved.