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 '15 at 9:26
  • exactly i want to view the document in the browser which is of type html...
    – abey
    Nov 25 '15 at 9:27
  • hey @rajasekhar Pls check my ans.
    – Ratan Paul
    Nov 26 '15 at 6:51
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
    – abey
    Nov 26 '15 at 9:46
  • 1
    @rajasekhar use ContentVersion field VersionData instead of ContentDocument.
    – Ratan Paul
    Nov 26 '15 at 9:52
  • SELECT ContentDocumentId FROM ContentVersion where ContentDocumentId = '' you can get the contentVersion using this query
    – Ratan Paul
    Nov 26 '15 at 10:09
  • Most Welcome @rajasekhar
    – Ratan Paul
    Nov 26 '15 at 10:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.