I have below code to add content document versionData into standard attachment.

    Map<Id, Blob> bodyContent = new Map<Id, Blob>();
    List<ContentVersion> contentDocs =  [SELECT Id, Description, title,versionData,ContentDocumentId FROM ContentVersion WHERE Id IN : contentIds];
    for(ContentVersion cVersion : contentDocs){

    for (ContentVersion attach : contentDocs) {
           Attachment conAttach = new Attachment();
           conAttach.ParentId = customAttach.Id;
           conAttach.Name = attach.title;
           conAttach.Body = bodyContent.get(attach.ContentDocumentId);
           conAttach.Description = attach.Id;

I have created the standard attachment with contentDocument versionData successfully. But when I download the attachment it is downloaded as type file.

enter image description here

But I want to download the file extension. Could anyone please help me to resolve this issue?

  • you probably need to append the FileExtension value to the filename, e.g. conAttach.Name = attach.Title + '.' + attach.FileExtension – David Cheng Mar 31 '19 at 18:49

How can I compose a multipart/form-data request?

Try like this.

Encode your contentversion data and convert as a blob

String strVersionData = EncodingUtil.base64Encode(cntver.VersionData);
Blob bBlob = EncodingUtil.base64Decode(strVersionData);
| improve this answer | |
  • Hi Raja vel,, Thank you.. But still it is not working as expected. It downloaded as file type – JURY Mar 7 '19 at 6:36
  • get filetype like below...ContentDocument cntdoc = [Select Id,Title,OwnerId,ParentId,FileExtension,FileType,CreatedDate, ContentSize,CreatedById,LatestPublishedVersionId from ContentDocument where Id=:DocumentId]; Contentversion cntver = [select Id,VersionData,FileType from contentversion where Id=:cntdoc.LatestPublishedVersionId]; – Raja vel Mar 7 '19 at 8:07

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.