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

Menu item link to download document


Go to End


13 Posts   5548 Views

Avatar
servalman

Community Member, 211 Posts

27 July 2011 at 10:35pm

Hello

Is there a simple way of doing the following :

Creating a menu item from the site tree that would download a file from the assests folders
Would trying to change the redirector page behaviour a good idea ?

Thank you

Avatar
swaiba

Forum Moderator, 1899 Posts

27 July 2011 at 10:47pm

I'd use a redirector page and then add this code within it...
http://www.silverstripe.org/general-questions/show/15832

or you could just take the URL from the asset itself and specify the entire url (I think it's say link to another site) but it should work.

Avatar
servalman

Community Member, 211 Posts

27 July 2011 at 11:04pm

Thanks for your answer it seems to be the begining of a solution
hoewer I need to be able to choose the document to download in the assets folder in a similar I would do it in a regular link

tHANKS

Avatar
swaiba

Forum Moderator, 1899 Posts

27 July 2011 at 11:11pm

ok, create a new page type "file download page" in db add has_one file to it, update cmsfields to pick the file
also add the controller function as described in the link (http://www.silverstripe.org/general-questions/show/15832)
that page can then be placed directly into the sitetree

Avatar
servalman

Community Member, 211 Posts

27 July 2011 at 11:25pm

Thanks

I will try to make it work eventough I think it is beyond my skills especially the getcmsfileds part ;)

thanks

T

Avatar
swaiba

Forum Moderator, 1899 Posts

28 July 2011 at 12:51am

maybe this will help then...

<?php

class FilePage extends Page {

	static $has_one = array(
		'MyFile' => 'File'
	);

	function getCMSFields(){
		$fields = parent::getCMSFields();

		$fields->addFieldsToTab('Root.Content', array(
			new FileIFrameField('MyFile', 'My File')
		));

		return $fields;
	}
}

class FilePage_Controller extends Page_Controller {
	//do send_file stuff here
}

Avatar
servalman

Community Member, 211 Posts

2 August 2011 at 3:09am

Hello Swaiba

I could not try your code before but I did today and I get an error
so here is my code now (I have added the file download part)

<?php

class FilePage extends Page {

   static $has_one = array(
      'MyFile' => 'File'
   );

   function getCMSFields(){
      $fields = parent::getCMSFields();

      $fields->addFieldsToTab('Root.Content', array(
         new FileIFrameField('MyFile', 'My File')
      ));

      return $fields;
   }
}

class FilePage_Controller extends Page_Controller {
   public function download(){

   $assetID = $this->request->param('ID');

   $do = DataObject::get_by_id('File', $assetID);
   
   if($do && file_exists($do->URL)){
    return SS_HTTPRequest::send_file(file_get_contents($do->URL), $do->Name);
   }else{
    echo 'error with ' . $do->URL . ' (' . file_exists($do->URL) . ')';
    return false;
   }

}
}

and this is the error I get

ERROR [User Error]: Uncaught Exception: Object->__call(): the method 'settabset' does not exist on 'FileIFrameField'
IN POST /newsite/admin/getitem?ID=60&ajax=1
Line 724 in /var/www/vhosts/kjebb2.com/httpdocs/newsite/sapphire/core/Object.php

Source
======
  715: 				
  716: 				default :
  717: 					throw new Exception (
  718: 						"Object->__call(): extra method $method is invalid on $this->class:" . var_export($config,
       true)
  719: 					);
  720: 			}
  721: 		} else {
  722: 			// Please do not change the exception code number below.
  723: 			
* 724: 			throw new Exception("Object->__call(): the method '$method' does not exist on '$this->class'",
       2175);
  725: 		}
  726: 	}
  727: 	
  728: 	//
       -----------------------------------------------------------------------------------------------------------------
  729: 	
  730: 	/**

Trace
=====
<ul>Object->__call(setTabSet,Array)

FileIFrameField->setTabSet(TabSet)
line 128 of TabSet.php

TabSet->push(FileIFrameField)
line 128 of FieldSet.php

FieldSet->addFieldsToTab(Root.Content,Array)
line 14 of FilePage.php

FilePage->getCMSFields(CMSMain)
line 441 of CMSMain.php

CMSMain->getEditForm(60)
line 1039 of LeftAndMain.php

LeftAndMain->EditForm()
line 389 of LeftAndMain.php

LeftAndMain->getitem(SS_HTTPRequest)
line 193 of Controller.php

Controller->handleAction(SS_HTTPRequest)
line 143 of RequestHandler.php

RequestHandler->handleRequest(SS_HTTPRequest)
line 147 of Controller.php

Controller->handleRequest(SS_HTTPRequest)
line 282 of Director.php

Director::handleRequest(SS_HTTPRequest,Session)
line 125 of Director.php

Director::direct(/admin/getitem)
line 127 of main.php

</ul>

If you can take a look it would be great

Avatar
swaiba

Forum Moderator, 1899 Posts

2 August 2011 at 3:14am

try ImageField instead of FileIFrameField

that code wasn't tested - there should be something in the basic tutorial about adding a picture (of an author to an article page)

Go to Top