function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Nikita Yadav 17Nikita Yadav 17 

Convert VersionData to actual file

Hello,
Its urgent !!

I have public site where i want to give a access to user to view and download the files stored in VersionData field of ContentVersion object.

I am using aura and apex for the above requirement and I am uanble to find out the way to convert VersionData to actual file. Can anybody help me to convert VersionData to actual file using aura and apex.

Thank you.
SubratSubrat (Salesforce Developers) 
Hello Nikita ,

You can use Apex to retrieve the binary data stored in the VersionData field of the ContentVersion object, and then convert it to an actual file that can be downloaded by the user. Here is some sample code that shows how to do this:

First, create an Apex controller that retrieves the binary data from the VersionData field and returns it as a Blob object:
public with sharing class ContentController {
    @AuraEnabled
    public static Blob getFileContent(Id contentVersionId) {
        ContentVersion content = [SELECT Id, VersionData FROM ContentVersion WHERE Id = :contentVersionId];
        return content.VersionData;
    }
}

Next, create an Aura component that calls the Apex controller to retrieve the file content and convert it to an actual file. Here is an example component that allows the user to download a PDF file:
<aura:component controller="ContentController">
    <aura:attribute name="contentVersionId" type="Id" />
    <lightning:button label="Download PDF" onclick="{!c.downloadPdf}" />
</aura:component>

Finally, create a controller for the Aura component that retrieves the file content from the Apex controller, converts it to a file, and initiates the download:
 
({
    downloadPdf: function(component, event, helper) {
        var action = component.get("c.getFileContent");
        action.setParams({
            contentVersionId: component.get("v.contentVersionId")
        });
        action.setCallback(this, function(response) {
            var blob = response.getReturnValue();
            var filename = "MyFile.pdf";
            var a = document.createElement('a');
            a.download = filename;
            a.href = URL.createObjectURL(blob);
            a.style.display = 'none';
            document.body.appendChild(a);
            a.click();
            URL.revokeObjectURL(a.href);
        });
        $A.enqueueAction(action);
    }
})

This code retrieves the binary data from the ContentVersion object using the Apex controller, creates a new Blob object, creates a link to the Blob object with a download attribute, and then clicks the link to initiate the download.

You can modify this code to support other file types by changing the filename and/or the MIME type in the Blob object.


Hope the above information is helpful !
Thank you.
Nikita Yadav 17Nikita Yadav 17
Hi @Subrat thank you for response.!

This is not working.I am not getting any error but VersionData Returns value like Blob[149865].

Do i need to convert this value to blob ?