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.