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
Richie DRichie D 

Writing a documents body directly to a browser



I know there have been some posts about this but none seem to suggest a working answer.


I'd like to be able to write a documents body directly to the browser and set the content type so that the contents are treated correctly by the browser.


So far I have for an image:-

apex: public string theBody {get; set;} public const(){ Document d = [select body from document limit 1]; theBody = d.EncodingUtil.base64encode(d.body); VF: ... <img src="data:image/jpeg:base64, {!theBody}"/>




Which works ok.


For a document e.g. application/msword this approach won't work.


I have tried writing out the body directly and body.toString() but none work (even with setting the contentType correctly). If the contentType is set then word just decides to open the data as text inside the document.


Has anybody managed this? Is it possible?


Thanks for any help.




contentType currently only supports PDF (actually, I think PowerPoint, too, but I haven't tested that yet)-- Salesforce uses a "HTML to PDF" driver that causes the HTML output to be rerendered into a PDF file which is ultimately delivered to your computer (in other words, the file that comes over to your computer is an actual PDF file, not an HTML file with a PDF extension). Until Salesforce supports Word as a valid contentType, setting the contentType will not cause the page to be rendered in a Word-friendly format, but instead results in the page's code being viewed in Microsoft Word. Ultimately, I'm afraid the answer for right now is "no, it does not work like you hoped it would."

Richie DRichie D

Thanks for your response sfdcfox.


I'm not sure your answer is correct. The reason being is that I don't require sfdc to do anything with the content (via a driver ala pdf), all I require is for the binary data to be output in its raw state (base64 encoded perhaps). The browser will handle the viewing application by the mime contentType - set by the contentType page attribute.


In microsoft I used to be able to do a 'Response.binaryWrite(binaryData)' and set the mime type and 'bob's your uncle' you have an image or a worddoc etc... If I could do the equivalent 'binaryWrite' in sfdc then it'd work as the contentType can be set to e.g. application/ms-word and the content is actually an existing uploaded document.



Oh. I thought you were rendering a Word document natively within Salesforce (i.e. from HTML/Visualforce). If all you are attempting to do is pass binary data, I would think that the contentType would be acceptable (I actually realized a short while I after I responded that it's renderAs that sets the rendering engine/driver). That being said, let me experiment with this this afternoon and get back in touch with you. I'm sure there must be a way to trick Salesforce into delivering content (after all, contentType is there for a reason).

Hi guys,


Just wondering if there's a solution to this as yet?


I'm trying to build some VF/Apex that will interrogate a remote web service, recieve a binary stream in response, and then deliver this stream to the user/browser.


I'll try playing with content-type args on the apex:page tag, just if there's already someone who's figured this out, that would be great to know!





Is there a solution yet?


I've always wanted to create a imageServelet in SF natively, any solutions to this yet since we have some new parameters to surpress the output of tags.