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
dj_saaspointdj_saaspoint 

Trying to download binary content via VF page

I want to expose some content on a sites VF page. There are some complex business rules to determine which users get access to which content, so I can't use any standard functionality.

 

Here's the VF page

 

<apex:page action="{!showContent}" contentType="application/octet-stream" controller="MyDocsController" showHeader="false" sidebar="false">
<apex:outputText value="{!contentBody}" escape="false"/>
</apex:page>

 

<apex:page action="{!showContent}" contentType="application/octet-stream" controller="MyDocsController" showHeader="false" sidebar="false">

<apex:outputText value="{!contentBody}" escape="false"/>

</apex:page>

 

Here's the relevant part of the controller:

 

 

public pageReference showContent() {

...

ContentVersion theDoc = [select id, VersionData from contentVersion where id = :docId];

contentBody = theDoc.VersionData;

...

 

 

 

When I invoke this page from  .../apex/ShowContent?docId=068... my browser downloads a file called "ShowContent".

The body of this file is the text core.filemanager.FileBlobValue@1e74039

 

I have 2 questions:

1) Is there any way for me to change the file to something sensible, such as the filename in the contentVersion object

2) How can I get at the actual BLOB as a stream of bytes instead of a pointer reference.

 

 

Thanks,

David.

Best Answer chosen by Admin (Salesforce Developers) 
ahab1372ahab1372

can you display a page that has a link to the document directly (similar to the "View" link on Documents or Attachments)?

 

Or redirect with a pageReference to the URL of the Document

All Answers

SuperfellSuperfell

You can't have pure binary data responses with VF.

dj_saaspointdj_saaspoint

Hmm. That's a bother.

 

The purpose is to expose documents to customers. The documents are sensitive product sheets so we want to limit visibility of all documents related to product X to only those customers who have purchased product X, i.e. with a related closed/won opportunity.

 

We had hoped to use sites & a customer portal with high volume licenses for this functionality, but this is now looking like it may not be an option.

 

Is there any other way to implement this?

 

 

 

 

ahab1372ahab1372

can you display a page that has a link to the document directly (similar to the "View" link on Documents or Attachments)?

 

Or redirect with a pageReference to the URL of the Document

This was selected as the best answer
dj_saaspointdj_saaspoint

Good suggestion Ahab

 

A redirect to the URL of the document brings me to the content download screen, from where users can search the content workspace and then get access to other docs :(

 

A snoop around the HTML and netlog with firebug shows that the download button doesn't go directly to the content as with docs and attachments but invokes another servlet.

 

I got it to work with the following code

return new pageReference ('/sfc/servlet.shepherd/version/download/'+docId+'?asPdf=false');

but scraping URLs like this makes me nervous as they tend to break with releases and there doesn't appear to be any URLFOR $Action...View variable for content :(

 

I'll log a feature request to have $Action's for content included.


Thanks,

David.

ahab1372ahab1372

I was hoping that there was a $Action for that. Are you going to put it on ideas as well? Please post the URL to the idea so that we can vote

dj_saaspointdj_saaspoint

I found something similar on ideas and added a comment. Here's the link

http://sites.force.com/ideaexchange/ideaView?c=09a30000000D9xt&id=087300000007kHsAAI