1

When user clicks the download button in VF page, the apex code has to generate a xdp file on the fly and should get downloaded in the user machine. The below code creates the attachment. I don't know how to return a download action.

In fact this file need not have to be saved as an attachment.I am saving this as an attachment as I don't know how to do it without it. Anyway please explain how this file can be downloaded in this action method.

List<ContentVersion > lstContentVersion = [SELECT FileType,IsLatest,Title,VersionData FROM ContentVersion where title = 'Application Data Sheet' and islatest = true and fileextension = 'XML'];
        string xmlContent; 

        if(lstContentVersion.size()>0){                
            Blob b = lstContentVersion[0].VersionData;                
            xmlContent  = b.toString();  
            Attachment attachment = new Attachment();
            attachment.Body = Blob.valueOf(xmlContent);
            attachment.Name = 'Application data sheet.xdp';
             attachment.ContentType ='application/vnd.adobe.xdp+xml';
            attachment.ParentId = ApexPages.currentPage().getParameters().get('id');
            insert attachment;
        }
3

You would need to set up a download action. Arguably, this is probably most easily done with a download link:

<!-- visualforce -->
<a href="data:application/vnd.adobe.xdp+xml;base64,{!xmlContent}"
   download="Application Data Sheet.xdp">Download Me!</a>

On slightly older browsers, this may fail if the file is significantly large enough; in that case, you'll want to consider saving said attachment and then deriving the url from Url.getFileFieldURL, which is supposed to return an appropriate link to use.

| improve this answer | |
  • Brilliant. The file was big, so the first solution did not work. With getFileFieldURL, I am not sure what the second parameter should be. Tried both AttachmentBody and Body. Both did not work. Please suggest. – Saranya Mar 6 '18 at 10:09

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.