0

I can insert an image, but it's without preview. I can open the image after downloading, but a need preview. It writes in files "No preview available". How to fix that?

Here is my code.

js:

        let action = component.get("c.save");
        let fileReader = new FileReader();

        fileReader.onload = function() {
            let fileContents = fileReader.result;
            let base64Mark = 'base64,';
            let dataStart = fileContents.indexOf(base64Mark) + base64Mark.length;

            fileContents = fileContents.substring(dataStart);

            action.setParams({
                customerId : 'a01f400000Nx9vgAAB',  //some Id
                versionData : fileContents
            });
            $A.enqueueAction(action);
        };
        fileReader.readAsDataURL(file);

apex:

        @AuraEnabled
        public static void save(String customerId , String versionData){
            ContentVersion currentContentVersion = new ContentVersion(
                Title = 'test',
                PathOnClient = 'test',
                //Is something wrong here? :
                VersionData = EncodingUtil.base64Decode(versionData),
                FirstPublishLocationId = customerId 
            ); 
            insert currentContentVersion;
        }
0

Looking around it seems, there is no direct way to implement this feature.

If you refer to the Content file preview issues knowledge article, it mentions the below (emphasis mine):

Salesforce does not guarantee all file types and their contained features will properly generate previews in Content. he Previewing section of the Salesforce CRM Content documentation outlines this limitation. Viewing and Editing Content Details mentions this limitation as well and also details which file types are currently supported for previewing.

For this reason, it is best practice and recommended to thoroughly review the online version of all content to ensure you are happy with the quality of it and decide whether it should only be downloaded and viewed in it's original file format accordingly.

If the file being inserted does support preview as mentioned in the knowledge article, then you could take a look at Regenerate a Content or File Preview to see if that helps.

You can find further details on the knowledge articles mentioned in the links.

  • But it works for me in Visual Firce variant, where we don't use js. And version data there looks like this: <apex:inputFile onchange="loadFile(event)" style="width: 200px" value="{!versionData}" contentType="{!contentType}" title="{!title}" fileName="{!pathOnClient}" accept="image/*" /> Maybe it's a problem with VersionData = EncodingUtil.base64Decode(versionData)? – user60684 Feb 5 at 17:28
  • If you are able to preview same file that you saved from Lightning component, then I wouldn't think its data issue. It could be then related to the experience - Classic vs. LEX. – Jayant Das Feb 5 at 17:29
0

It works! I added filename and change PathOnClient. My resolution:

    let action = component.get("c.save");
    let fileReader = new FileReader();

    fileReader.onload = function() {
        let fileContents = fileReader.result;
        let base64Mark = 'base64,';
        let dataStart = fileContents.indexOf(base64Mark) + base64Mark.length;

        fileContents = fileContents.substring(dataStart);

        action.setParams({
            fileName : file.name,
            customerId : 'a01f400000Nx9vgAAB',  //some Id
            versionData : fileContents
        });
        $A.enqueueAction(action);
    };
    fileReader.readAsDataURL(file);

apex:

    @AuraEnabled
    public static void save(String fileName, String customerId , String versionData){
        ContentVersion currentContentVersion = new ContentVersion(
            Title = fileName,
            PathOnClient = '/' + fileName,
            VersionData = EncodingUtil.base64Decode(versionData),
            FirstPublishLocationId = customerId 
        ); 
        insert currentContentVersion;
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.