One of the most beautiful parts of Magento lies in its usage. From the admin control you can basically oversee and spearhead any feature you wish to utilize. Though proper implementation and feasibility may seem difficult or impossible at times but even these intricate features can be utilized very adeptly.
One such feature that I implemented was for a client who wanted to provide a direct download link of a file stored in a “var” folder.
Basically I have a custom admin module and client want to provide a link in grid that will directly download the file. File may be an image or a pdf. File location is within var folder and as it is known we cannot access var folder directly so we need to create admin controller to achieve this functionality.
First we need to call render function in grid.php file.
Path of file is – app/code/local/ModuleNamespace/ModuleName/Block/Adminhtml/ModuleName/Grid.php
[php]$this->addColumn("column_name", array(
"header" => Mage::helper("registration")->__("Column Name"),
"index" => "column_name",
'sortable' => true,
'type' => 'text',
'renderer' => 'ModuleNamespace_ModuleName_Block_Adminhtml_Template_Grid_Renderer_File' )); [/php]
This will call render function of file.php and pass path of the file that we need to download.
Path of file should be – app/code/local/ModuleNamespace/ModuleName/Block/Adminhtml/Template/Grid/Renderer/File.php
Now file.php should be like this-
[php]<?php class ModuleNamespace_ModuleName_Block_Adminhtml_Template_Grid_Renderer_File extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract {
public function render(Varien_Object $row) {
return $this->_getValue($row);
}
protected function _getValue(Varien_Object $row) {
$val = $row->getData($this->getColumn()->getIndex());
$parts = explode("/", $val);
$filename = end($parts);
$url = Mage::helper("adminhtml")->getUrl("moduleName/adminhtml_moduleName/downloadfile", array('filename' => $filename)); $out = "<a href='".$url."'>". $val ."</a>";
return $out;
}
}[/php]
here $val contain the path of file which should be like
“var/www/projectName/var/folderName/file.jpg”
$filename will get the file name like “file.jpg”. If you already have the file name then there is no need to explode it. Now I am cal