From a visual force page on a button click i am opening documents using following javascript function

<button type="button" class="wpsButtonText" onclick="javascript:download()">I Accept</button>

 function download()
        var params = getParams();
        if (params["id"])

            location.href = "https://test.salesforce.com/sfc/servlet.shepherd/version/download/" + unescape(params["id"]);


But the problem is of type pdf it is opening in the same tab instead of downloading but when the document is of type html it is directly downloading without any preview


enter image description here


enter image description here

is there any approach to make even html to open directly in the browser without downloading

  • are you trying to view document record?
    – Ratan Paul
    Commented Nov 25, 2015 at 9:26
  • exactly i want to view the document in the browser which is of type html...
    – Rajasekhar
    Commented Nov 25, 2015 at 9:27
  • hey @rajasekhar Pls check my ans.
    – Ratan Paul
    Commented Nov 26, 2015 at 6:51

1 Answer 1


A workaround for above issue.

You can create a VF page and get the document body blob data and convert into string. Now display in VF page. like below example

public class test1 {
public String strHTML    {get;set;}

public test1() {

       Document doc = [Select d.Body From Document d where Id= '01590000009jQTB'];
       Blob bodyBlob = doc.Body;
       strHTML = bodyBlob.toString();

VF Page

<apex:page controller="test1" showHeader="false" standardStylesheets="false">
    <apex:form >
        <apex:outputText escape="false" value="{!strHTML}"/> 

  • thanks Ratan,this is best approach i have ever found . Can u help me with soql query to fetch the document body from 'ContentDocument'.I unable to figure out how to fetch the document body from a ContentDocument record
    – Rajasekhar
    Commented Nov 26, 2015 at 9:46
  • 1
    @rajasekhar use ContentVersion field VersionData instead of ContentDocument.
    – Ratan Paul
    Commented Nov 26, 2015 at 9:52
  • SELECT ContentDocumentId FROM ContentVersion where ContentDocumentId = '' you can get the contentVersion using this query
    – Ratan Paul
    Commented Nov 26, 2015 at 10:09
  • Most Welcome @rajasekhar
    – Ratan Paul
    Commented Nov 26, 2015 at 10:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .