cancel
Showing results for 
Search instead for 
Did you mean: 

Download button for customer Magento

Download button for customer Magento

Hi,

 

I've run into a problem when creating a button to download pdf file from custom tab on the product page. While I don't really think place matters, as it probably wouldn't work anywhere.

 

So I have two questions. 

 

Is it possible to use such simple html to create downloadable files in magento? 

Where should I store such files? I used /pub/media/custom folder path but I also tried different one.

2 REPLIES 2

Re: Download button for customer Magento

You can use any file to create a download button
According to Magento standards, downloads should be saved to /pub/media/custom

If issue solved, Click Kudos & Accept as Solution.
LitCommerce - The Most Simple & Affordable Multi-channel Selling Tool

Re: Download button for customer Magento

@seomeryem0edf3 

Modify the code as described below:

1. Replace the button code from :

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

to:

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

2. You need to add the below code at the end of the file:

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

3. Create one controller and use the code shown as an 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;
    }

}
?>

Before checking the result, clear the cache once.

Problem solved? Click Kudos and "Accept as Solution".
200+ Magento 2 Extensions for Enhanced Shopping Experience.