On my Community page, I'm trying to provide a download for a file that resides outside of Salesforce. I want the user to be authenticated prior to download and I don't want the user to know the URL of the file.

Currently, I have a link to a Visualforce page on the Community. An Apex controller checks for a PermissionSet and returns a PageReference with the URL of the file.

With this method, the URL of the file can be determined by any download manager.

Is there a way to accomplish this in Salesforce? Maybe I'm going about this the wrong way....

I did find this https://developer.salesforce.com/forums/ForumsMain?id=906F00000008w0oIAA , which lets me retrieve the file in Apex controller, but can I serve the Blob to the user as a file download?


Yes you can, using a href="data:" url.

You'll need to determine your mime type, using something like this (note the mimetype map is just for example sake - get yourself a better list):

String mimeType {get;set;}
String fileName = 'blabblay.bco.pdf';

Map<String,String> mimeTypeMap = new Map<String,String>{'pdf'=>'application/pdf','doc'=>'application/worddoc'};

String[] fileNameParts = fileName.split('\\.');
String ext = fileNameParts[fileNameParts.size()-1];

mimeType = mimeTypeMap.get(ext);

Once you have that, you can add this to your VisualForce page:

<a href="data:{!mimeType};content-disposition:attachment;base64,{!base64Value}">Download PDF</a>

Here's a list of mime types to save you a couple of clicks.

  • Thanks @CaspNZ. This would've worked, but my file size was too big for the Attachment class – cfann61 May 14 '15 at 21:04
  • Oh bummer. There's not much you can do about that, as heap size. I think the limit is 6MB now. Actually, if you make the call with a Continuation which is async, you might get up to 12MB. – Caspar Harmer May 14 '15 at 21:15
  • You know, you could prob do this all in javascript - just supply the url to a javascript or jQuery function, grab the data and set it as an attribute on the anchor tag. Of course, you have exposed the url in javascript, but only a developer will know about that. – Caspar Harmer May 14 '15 at 21:18
  • Ended up creating a small authentication app on the server where the files are stored. So any requests to the file url are authenticated through Salesforce. Would've been cleaner to do it in Salesforce, but this offered a quick solution. Thanks for you help @CaspNZ – cfann61 May 15 '15 at 21:00

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.