21287 Posts in 5733 Topics by 2602 members
| Go to End | Next > | |
| Author | Topic: | 1495 Views |
-
Menu item link to download document

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
-
Re: Menu item link to download document

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/15832or 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.
-
Re: Menu item link to download document

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 linktHANKS
-
Re: Menu item link to download document

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 -
Re: Menu item link to download document

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
-
Re: Menu item link to download document

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
} -
Re: Menu item link to download document

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.phpSource
======
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.phpTabSet->push(FileIFrameField)
line 128 of FieldSet.phpFieldSet->addFieldsToTab(Root.Content,Array)
line 14 of FilePage.phpFilePage->getCMSFields(CMSMain)
line 441 of CMSMain.phpCMSMain->getEditForm(60)
line 1039 of LeftAndMain.phpLeftAndMain->EditForm()
line 389 of LeftAndMain.phpLeftAndMain->getitem(SS_HTTPRequest)
line 193 of Controller.phpController->handleAction(SS_HTTPRequest)
line 143 of RequestHandler.phpRequestHandler->handleRequest(SS_HTTPRequest)
line 147 of Controller.phpController->handleRequest(SS_HTTPRequest)
line 282 of Director.phpDirector::handleRequest(SS_HTTPRequest,Session)
line 125 of Director.phpDirector::direct(/admin/getitem)
line 127 of main.php</ul>
If you can take a look it would be great
-
Re: Menu item link to download document

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)
| 1495 Views | ||
| Go to Top | Next > |


