+ Start a Discussion
James CarboneJames Carbone 

Visualforce page to output binary PNG image

Hi,

 

I've been trying to get a Visualforce page to output a PNG image.  For example, you hit the URL and the picture displays in the browser window.  The content-type setting appears to allow this sort of thing.  For example I was able to output a BMP with code similar to this:

 

VF page:

<apex:page controller="TileRedirector" contentType="image/bmp"  showHeader="false" sidebar="false">{!Content}</apex:page>

 

 

Controller snippet:

public string getContent()

{

    string content = 'BM:6( ##Ÿˇ';    

    return content;

}

 

 

When I try this with a PNG I get an invalid image.  My current thinking is that the content type for the page is outputs as image/png; charset=UTF-8;

 

I can force different charset's by adding the charset to my VF page without effect.  I'm reading the image from another source and can successfully view that image from their web site.  The difference is that the content type for those requess always respond as "image/png" without a char set specified.

 

Has anyone successfully returned a PNG from a VF page before?

 

Thanks,

Jim 

sfdcfoxsfdcfox

"Character sets" are for textual data. Binary data does not understand the concept of character sets and should always be output as raw data. The problem here is that the "string" class in Apex Code is always in utf-8, which is consistent with the language that Salesforce is designed in (Java). I suspect that Apex Code is somehow managing to mangle your output stream (and it only takes a single mishap to kill an entire file). You should be able to add a "Content-Encoding" header to the page's headers, using the header name "Content-Encoding" with a value of "base64". Then, use EncodingUtil.base64encode and return the string directly into the browser.

Prajapati.LakhanPrajapati.Lakhan

Hi,

How can I set the page header for content Encoding in SFDC. "PageReference" class doesnot have any method like "setHeader(name, value)" and "<apex:page>" dont have any attribute like "content-encoding". Please provide more details.

 

Thanks,

Lakhan

 

James CarboneJames Carbone

Use the contentType attribute.

 

<apex:page controller="TileRedirector" contentType="image/png; charset=binary;" ....
Prajapati.LakhanPrajapati.Lakhan

Hi,

 

Thanks for the reply but in salesforce I can't convert blob into binary strings the only encoding for binary data in salesforce is base64 and it seems I can't use contentType="images/png;charset:base64" , actually I tried it so that i could display a preview of an image before actual upload to the attachment object.

 

 

Thanks,

Lakhan

bruins202bruins202

Hi folks,

 

We're trying to allow a visitor to hit a VisualForce page address, and have the result be an image/gif content type.  Unfortunately, no matter what we do (even following the first poster's example code above for BMP files) we're never able to have a properly formatted image load.  We're literally using carbonedev code line for line, but are unable to have it load a bmp file.

 

Ideas?

Cheers!