1

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"]);
        }

        return(0);
    }

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

For PDF

enter image description here

For HTML

enter image description here

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

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

1 Answer 1

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}"/> 

    </apex:form>
</apex:page>
4
  • 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
    Nov 26, 2015 at 9:46
  • 1
    @rajasekhar use ContentVersion field VersionData instead of ContentDocument.
    – Ratan Paul
    Nov 26, 2015 at 9:52
  • SELECT ContentDocumentId FROM ContentVersion where ContentDocumentId = '' you can get the contentVersion using this query
    – Ratan Paul
    Nov 26, 2015 at 10:09
  • Most Welcome @rajasekhar
    – Ratan Paul
    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 .