Jump to:

22994 Posts in 11843 Topics by 2828 members

General Questions

SilverStripe Forums » General Questions » Problem with creating export facility

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 478 Views
  • jennymgray
    Avatar
    Community Member
    7 Posts

    Problem with creating export facility Link to this post

    I am trying to add an "export as RTF" facility to my website. The code which handles the /export url call basically creates the appropriate export content and then sends the Content-type header as text/rtf and echo's the export content.

    All is well in most browsers, but in Internet Explorer 8 (i'm not sure about other IE versions) I get "Internet Explorer was not able to open this Internet site. The requested site is either unavailable or cannot be found"

    The same thing happens if I use SS_HTTPRequest::send_file.

    I've been searching around for ages for a solution but can't find anything. This code works perfectly well from a test php file outside SilverStripe. Has any-one seen this before? Does any-one know how to get round the problem?

    TIA
    Jenny

  • Willr
    Avatar
    Forum Moderator
    5464 Posts

    Re: Problem with creating export facility Link to this post

    Could you post the code you're trying to use in SS.

    SS will possibly override your header and include it in the template so after you echo the result (you should just use a template) perhaps include a die().

    Echoing anything kinda goes against SilverStripe, you should instead use renderWidth() and put your RTF text in a template (template files can be anything, not just html);

    function export() {
    return $this->customize(new ArrayData(
    array('Foo' => 'bar')
    ))->renderWith('MyRTFTemplate');
    }

    Then in themes/yourtheme/templates/MyRTFTemplate.ss include your data, that array I passed into customize will provide $Foo to the template and you can add whatever you need.

  • jennymgray
    Avatar
    Community Member
    7 Posts

    Re: Problem with creating export facility Link to this post

    Hi,

    Thanks for your help. I have incorporated your feedback so my code now looks like this

          header("Content-type: text/rtf");
          header("Content-Disposition: attachment; filename=".$this->exportFileName().".rtf");
          $rtf = convert2rtf::convert($content);
          return $this->renderWith(
                array('RTF'),
                array('RTF'=>$rtf));

    As you can see, I'm using a library to convert to RTF so I pass the converted content to my template which contains only $RTF.

    This has fixed my original bug - now content is sent to a file download dialog in all browsers. But, browsers seem to be ignoring my content-type and filename headers.

    I can't find anywhere in the SilverStripe code where I can control those - have I overlooked it, or is this not supported?

  • Willr
    Avatar
    Forum Moderator
    5464 Posts

    Re: Problem with creating export facility Link to this post

    SS is likely overridding your header() calls with it's own. Try setting $this->response->addHeader();

    http://api.silverstripe.org/2.4/sapphire/control/SS_HTTPResponse.html#methodaddHeader

    478 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.