[SalesForce] Append files to the first upload in

I am using the following code in my lightning component to upload multiple files

<lightning:input aura:id="fileId" onchange="{!c.handleUploadFinished}" type="file" name="file" label="Upload Attachment" multiple="true"/>

JS Controller Code
handleUploadFinished: function (component, event, helper) {

    // Get the list of uploaded files

    var fileInput = component.find("fileId").get("v.files");

}

  1. So what I can see is that if I upload 2 files together, then upload another one, the previous two files get removed. My question is that is there any possible way to append the 3 files together instead of the first two getting removed?

  2. My second question would be that can I assign the fileInput to an array attribute of the corresponding component? If so then what would be attribute type?

I am using the following blog as the base.
http://sfdcmonkey.com/2017/09/25/file-upload-lightning-component

Best Answer

So what I can see is that if I upload 2 files together, then upload another one, the previous two files get removed. My question is that is there any possible way to append the 3 files together instead of the first two getting removed?

No, this is a browser-level API. It's strictly read-only and depends on what the user chooses as the files.

My second question would be that can I assign the fileInput to an array attribute of the corresponding component? If so then what would be attribute type?

It's not a real array, it's an "array-like object." The design decision for this is based on the fact that it is a read-only object called FileList, and so you can't do normal array stuff to it directly. Converting to a normal array is easy enough:

var filesAsArray = [].slice.call(fileList);

As long as you do this first, you can save the File objects in to a List. I wouldn't recommend trying to store the original fileList directly, as it will be modified when a user selects new files.

Related Topic