2

I have a problem with download function from frontend. I make a simple Download function in my block.

Practice/Attachment/Block/Frontend/Attachment.php:

protected function DownloadFile() {

    $file = 'media/attachment/Doc1.pdf';

    if (file_exists($file)) {
        header('Content-Description: File Transfer');
        header('Content-Type: application/octet-stream');
        header('Content-Disposition: attachment; filename="'.basename($file).'"');
        header('Expires: 0');
        header('Cache-Control: must-revalidate');
        header('Pragma: public');
        header('Content-Length: ' . filesize($file));
        readfile($file);
        exit;
    }

}

This is my template design/frontend/rwd/default/template/catalog/product/view/attachement.phtml:

<div class="attachment">
    <?php
    foreach ($this->getCollection() as $status)
    {
        $image_of_download_button = 'Doc1.pdf';
        $value = $this->getCollection()->getItemById($status->entity_id)->getData('title');
        $link  = $this->getCollection()->getItemById($status->entity_id)->getData('uploaded_file');
        $link2 = 'media/attachment/Doc1.pdf';
        $elo = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA).'attachment/'.$image_of_download_button;
        ?>
        <a href="<?php echo $linkModel?> ">
            <img src="<?php echo Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA).'attachment/'.$image_of_download_button?>" width=1200px height="100px" align="center" style="margin-left: 20px">
            <p> AAAAAAAA </p>
        </a>
        <br>
        <button name="button_download" onclick=" <?php  $this->DownloadFile(); ?> ">Click me</button>
        <?php
    }
    ?>
</div>

But if i reload the page the file is autodownloaded and if i click the button nothing is happening. I wanna download the file when i press click on button, this is the reason for use oneclick. Why i do wrong?

I just wanna make a function for frontend to download a file (pdf for exmaple) for a product.

  • Should it be onclick=" <?php $this->DownloadFile(); ?> " instead of onclick=" <?php $this->sendFile(); ?> " ? – Raphael at Digital Pianism Jun 3 '16 at 11:43
  • no, sorry, i make a mistake on my code post here. i correct the code. – RedoColor Jun 3 '16 at 11:46
  • my function was sendFile, but i change here in DownloadFile to make it clear – RedoColor Jun 3 '16 at 11:47
  • Write this function in your module controller and on button button click just hit that controller action your file will start downloading on click. – Aman Alam Jun 3 '16 at 12:48
  • i try to write the function in the controller, but i dont know how to acces the controller in my block. To use my function in my block, i use layout. – RedoColor Jun 5 '16 at 7:44
3

You need to make some modification:

Step 1 : Replace your button code:

From:

<button name="button_download" onclick=" <?php  $this->DownloadFile(); ?> ">Click me</button>

To :

<button name="button_download" onclick="myFunction()">Click me</button>

Step 2: add following code at the end of file :

<script>
function myFunction() {
    /* practice = your module name,index = controller, index function */
    window.location = "<?php echo $this->getUrl('practice/index/index/');?>";
}
</script>

Step 3: Create a controller IndexController.php for example:

<?php 
class Practice_Attachment_IndexController extends Mage_Core_Controller_Front_Action{
public function index() {

    $file = 'media/attachment/Doc1.pdf';

    if (file_exists($file)) {
        header('Content-Description: File Transfer');
        header('Content-Type: application/octet-stream');
        header('Content-Disposition: attachment; filename="'.basename($file).'"');
        header('Expires: 0');
        header('Cache-Control: must-revalidate');
        header('Pragma: public');
        header('Content-Length: ' . filesize($file));
        readfile($file);
        exit;
    }

}
?>

Clear cache and check this.

| improve this answer | |
  • i want to download wishlist item list from frontend as a pdf formate. is it work for this? – Neeya Aug 3 '17 at 10:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.