How to create contentdistribution public link?
I have to create a public link for files. I have inserted 'ContentVersion' and 'ContentDistribution' and created 'DistributionPublicUrl'. I am able to create a public link ('DistributionPublicUrl') but once I open a browser file it opens but 'No preview available' option is coming on the screen. While the download option is there.
Please find my code below:
ContentVersion contentVersionRec = new ContentVersion(); contentVersionRec.Title = fileName; contentVersionRec.PathOnClient = '/' + fileName; contentVersionRec.FirstPublishLocationId = contactRec.Id; contentVersionRec.VersionData = EncodingUtil.base64Decode(base64File); contentVersionRec.IsMajorVersion = true; Insert contentVersionRec; //update public link on Task ContentDistribution cdl = new ContentDistribution(); cdl.ContentVersionId = contentVersionRec.Id; cdl.Name = fileName; insert cdl; ContentDistribution cd = [SELECT DistributionPublicUrl FROM ContentDistribution WHERE Id =: cdl.Id LIMIT 1];
add this to create contentdistribution public link -
cdl.PreferencesAllowViewInBrowser= true;
and try once