1

This is the helper class in lightning:

({
    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-- --
})

From component I am calling this save method but I am getting this error:

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

Please try again.

Please help me.

Gustavo Morales
  • 2,614
  • 9
  • 29
  • 37
sivanee
  • 11
  • 1
  • 4
  • In case you were wondering, you can't do this: `MAX_FILE_SIZE: 750 000` - numbers don't have formatting in any programming language - instead you should write: `MAX_FILE_SIZE: 750000`. I don't know if this will solve your problem, but it will definitely make your program compile and run better. – Caspar Harmer Mar 12 '17 at 04:05

2 Answers2

0

When there is no files in fileInput, you cannot get files by index value. Check the files before access.

({
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();

})

Arun
  • 101
  • 3
0

Use below one line of code for file upload it is possible to upload upto 2 gb size file

<lightning:fileUpload label="Attach File"
                      multiple="false" 
                      recordId="{!v.myRecordId}" 
                      onuploadfinished="{!c.handleUploadFinished}" 
                      />
Dmitriy
  • 5,525
  • 12
  • 25
  • 38
prattt
  • 11