[SalesForce] How to upload a New Version of an existing File using lightning:fileUpload lightning base component

Does anyone have an idea about uploading a new version of a File in Lightning Component using the base lightning component lightning:fileUpload?

I tried passing ContentDocumentId into recordId parameter, but it returns error on upload.

<lightning:fileUpload  name="fileUploader"
                           label= "Demo Upload"
                           multiple="true"
                           recordId="{!v.recordId}" />

Best Answer

This cannot be done with the lightning:fileUpload component.

I solved this problem with the lightning:openFiles event.

This way a user has a preview of the file and inside the previewer you can upload a new version, delete, download and so on:

enter image description here

Here is some code sample:

Markup

<aura:attribute name="file" type="ContentDocumentLink" description="content file - taken from server" />

<div onclick="{!c.previewFile}" id="{!file.ContentDocumentId}">
    <lightning:tile label="{!file.fileName}" title="Click to preview and manage uploaded file">
        <aura:set attribute="media">
            <lightning:icon iconName="doctype:unknown" size="xx-small"/>
        </aura:set>
    </lightning:tile>
</div>

Controller

previewFile: function(component, event, helper) {
    var contentId = event.currentTarget.id;
    $A.get('e.lightning:openFiles').fire({
        recordIds: [contentId]
    });
},