Jump to:

23457 Posts in 18905 Topics by 2877 members

General Questions

SilverStripe Forums » General Questions » Link to force download of file?

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: 1881 Views
  • flipsidenz
    Avatar
    Community Member
    49 Posts

    Link to force download of file? Link to this post

    Hi all,

    I'm trying to setup a front end list of files for users to be able to click on a link and download, rather than the file opening in the browser window. The file types are to be an assortment of PDF's, Images, and other documents...

    The lists of files for download are sitting on Product pages, and the files are DataObjects. Here's my relationship inside Product.php:

    public static $has_many = array(
          'ProductFiles' => 'Product_File'
          
       );

    And my control loop in the template Product.ss:

    <% if ProductFiles %>
          <% control ProductFiles %>
          <div class="download-item<% if Last %> last-download<% end_if %>">
          <a href="">$Title</a> - <small>$Attachment.Extension ($Attachment.Size)</small><br>
          $Description
          </div>
          <% end_control %>
          <% else %>
             No downloads available
          <% end_if %>
          </div>
          </div>
          <% end_if %>

    I've followed this post http://silverstripe.org/general-questions/show/15832 to be able to force a download to start. My controller function is below.

    This controller function is sitting inside the Products.php controller.

    public function download(){

        $assetID = $this->request->param('ID');
       
        $do = DataObject::get_by_id('Product_File', $assetID);
       
        if($do){
          return SS_HTTPRequest::send_file(file_get_contents(Director::absoluteBaseURL() . $do->Attachment()->Filename), $do->Attachment()->Name);
        }else{
          return false;
        }
       
       }

    I'm unsure whether I should be setting up a function inside Products class controller, or whether I should be adding it into class Product_File?

    If I manually enter mysite.com/product/download/1 into the browsers address bar, the download prompt window appears, so far so good.

    What I am unsure about is how to create that URL in my template, so that on each page, each file is being linked to the download prompt. download/$ID (If I am looping through my file control), will attempt to open mysite.com/download/1, which isn't what I am after, as it is not including the product in the URL that I am on at the time.

    Hopefully this is a basic problem and someone can help this noob out ;)

    Thanks.

  • flipsidenz
    Avatar
    Community Member
    49 Posts

    Re: Link to force download of file? Link to this post

    Trying to answer my own questions

    http://api.silverstripe.org/2.4/cms/SiteTree.html#methodLink

    Although I don't know if I am using it properly? In my product.ss template:

    <% if ProductFiles %>
          <% control ProductFiles %>
          <div class="download-item<% if Last %> last-download<% end_if %>">
          <a href="$Top.Link(download)/$ID">$Title</a> - <small>$Attachment.Extension ($Attachment.Size)</small><br>
          $Description
          </div>
          <% end_control %>
          <% else %>
             No downloads available
          <% end_if %>

    Would this be the correct usage to call the download function in my first post?

    1881 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.