Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

General Questions /

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

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

Link to force download of file?


Go to End


2 Posts   3031 Views

Avatar
flipsidenz

Community Member, 49 Posts

27 June 2011 at 10:15am

Edited: 27/06/2011 10:26am

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.

Avatar
flipsidenz

Community Member, 49 Posts

27 June 2011 at 9:18pm

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?