1

这是闪电网络中的辅助类:

({
    MAX_FILE_SIZE: 750 000,
    save: function(component) {
        var fileInput = component.find("file").getElement();
        var file = fileInput.files[0];
        var fr = new FileReader();
        -- --some logic-- --
})

从组件中,我正在调用此保存方法,但出现此错误:

Something has gone wrong. Action failed: c$fileUpload$controller$save [TypeError: Cannot read property '0' of undefined]
Failing descriptor: {c$fileUpload$controller$save}.

请再试一次。

请帮我。

4

2 回答 2

0

使用下面的一行代码进行文件上传,最多可以上传 2 GB 大小的文件

<lightning:fileUpload label="Attach File"
                      multiple="false" 
                      recordId="{!v.myRecordId}" 
                      onuploadfinished="{!c.handleUploadFinished}" 
                      />
于 2018-09-17T11:52:21.370 回答
0

当 fileInput 中没有文件时,不能通过索引值获取文件。访问前检查文件。

({
MAX_FILE_SIZE: 750 000,
save: function(component) {
    var fileInput = component.find("file").getElement();
    if(fileInput.files){
       var file = fileInput.files[0];
    }
    var fr = new FileReader();

})

于 2017-06-19T13:37:31.040 回答