0

Cant upload file to content folder from standard file uploader

I have tried several times uploading files to the folder. But every time it shows can't upload file.

If I try to upload file to library it works. For this I am passing library id as record id in lightning file uploader.

Similarly if i am passing folder id as record id it returns error

'Cant upload 'filename' '

1 Answer 1

0

You cannot specify ContentFolder Id as recordId directly in the lightning:fileUpload component but you can handle this as an update in an apex controller method.

Assuming that by default the file will be uploaded to a library, then once file is uploaded invoke an apex method from the event onuploadfinished to update folder id to move it to appropriate folder.

onuploadfinished is called once you click on done after file upload.

Here is a sample code:

FileUploader.cmp

<aura:component implements="flexipage:availableForAllPageTypes,force:appHostable,flexipage:availableForRecordHome"
                access="global"
                controller="FileUploader" >
    <aura:attribute name="myRecordId" type="String" 
                        description="Record to which the files should be attached"
                    default="0581t000000HHiAAAW"/> <!-- hardcoded library id for illustration purpose only, adapt as per your use case -->
    <lightning:fileUpload label="Attach doc"
        name="fileUploader"
        multiple="false"
        accept=".pdf, .png"
        recordId="{!v.myRecordId}"
        onuploadfinished="{!c.handleUploadFinished}" />
</aura:component>

FileUploaderController.js

({
    handleUploadFinished: function (cmp, event) {
        // Get the list of uploaded files
        var uploadedFiles = event.getParam("files");
        var documentId = uploadedFiles[0].documentId;//this retrieves the ContentDocumentId
        var fileName = uploadedFiles[0].name;

        var action = cmp.get("c.serverMovetoFolder");
        action.setParams({ docId : documentId });

        // Create a callback that is executed after 
        // the server-side action returns
        action.setCallback(this, function(response) {
            var state = response.getState();
            if (state === "SUCCESS") {
                // do something here on success
                console.log("From server: " + response.getReturnValue());    
            }
            else{
              //handle error here
            }
        });

        $A.enqueueAction(action);
    }
})

FileUploader.cls (apex class)

ContentFolderMember stores the folder association in the field ParentContentFolderId, so query the current folder for the document. Then query the folder name you want to move the file into. Then perform an update on ContentFolderMember record to set the new folder id for the file.

public with sharing class FileUploader {

    @AuraEnabled
    public static Boolean serverMovetoFolder(String docId) {
        try{
            ContentFolderMember folderMember = [SELECT Id, ParentContentFolderId,ChildRecordId 
                                                from ContentFolderMember where ChildRecordId = :docId limit 1];

            ContentFolder folder = [SELECT Id, name from contentfolder where name ='YourFolderName' limit 1];
            folderMember.ParentContentFolderId = folder.Id;
            update folderMember;
            return true;
        }catch(Exception ex){
            throw new AuraHandledException(ex.getMessage()); 
        }

    }
}
2
  • Thanks for the reply. This was the exact thing i was looking for. You saved my time
    – user75427
    Jan 3, 2020 at 5:37
  • @NBR Happy to help :)
    – Shamina
    Jan 3, 2020 at 6:39

You must log in to answer this question.