4

Well this is a concept and was trying to implement the same in VF. In salesforce Attachments are served with content-disposition = inline, this makes the attachment to display in the browser.

Now plan was to change the content-disposition to attachment by a "Proxy VF Page". The code looks something like this

public Pagereference downloadit() {
    Pagereference pageRef = new Pagereference('https://c.ap1.content.force.com/servlet/servlet.FileDownload?file=00P90000006T0ZH');
    pageref.getHeaders().put('content-disposition', 'attachment; filename=' + 'myfile.png');
    pageref.setRedirect(false);
    return pageref;
}

<apex:page controller="AttachmentDownloadProxy_Con" action="{!downloadIt}">

</apex:page>

But it doesn't seem to work. The file gets downloaded but seems to be corrupted. Any Idea ?

On checking the file source, this is what I got

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>

<head>
    <meta HTTP-EQUIV="PRAGMA" CONTENT="NO-CACHE">





    <script>
        if (this.SfdcApp && this.SfdcApp.projectOneNavigator) {
            SfdcApp.projectOneNavigator.handleRedirect('https://c.ap1.content.force.com/servlet/servlet.FileDownload?file=00P90000006T0ZH');
        } else
         if (window.parent.location.replace) {
            window.parent.location.replace('https://c.ap1.content.force.com/servlet/servlet.FileDownload?file=00P90000006T0ZH');
        } else {;
            window.parent.location.href = 'https://c.ap1.content.force.com/servlet/servlet.FileDownload?file=00P90000006T0ZH';
        }
    </script>

</head>


</html>
1

I think you'll find the contents of your file is actually HTML for a client-side redirect!

Might be tricky to "proxy" the file in this way, but you can change the Content-Type of your attachment to application/octet-stream to force download instead of display:

Try this:

Attachment attachment = new Attachment(
    Id = '00P90000006T0ZH',
    ContentType = 'application/octet-stream'
);

update attachment;

then link to it as normal and you'll get it as a downloaded file.

  • Well I will try this one. But was wondering if there is way to do it without actually updating the attachment. I was planning to just override the content disposition. By the way I added the source of the file, seems like html is being downloaded – Avidev9 Dec 19 '13 at 9:05
  • Tried doing the contenttype update doesnt seem to work – Avidev9 Dec 19 '13 at 9:10
  • If the file isn't binary, you could select its body out as a Blob and then put it in an apex:outputText with escape="false" on your proxy page. But if the Blob contains any non-UTF8 characters it will barf... – bigassforce Dec 19 '13 at 9:13
  • Did a check again it doesnt seem to work for me.By the way I have a PNG file in the attachment. Well the file is a binary file, it can be PDF or Image. Event after the Update the attachment(PNG) opens in the browser. – Avidev9 Dec 19 '13 at 9:20
  • "Doesn't work" - details? – bigassforce Dec 19 '13 at 9:21

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.