-1

I have a file in my server folder and now I want to download that file on page load.

Example :

If I click on Our products than a file has been downloaded automatically in users system from my server.

Debasis
  • 201
  • 3
  • 9

2 Answers2

1
// in controller 

public function download() {
  $this->viewClass = 'Media';
  // Render app/webroot/files/example.docx
  $params = array(
    'id'        => 'example.docx', // file name
    'name'      => 'example',
    'extension' => 'docx', // its file extension name
    'mimeType'  => array(
        'docx' => 'application/vnd.openxmlformats-officedocument' .
            '.wordprocessingml.document'
    ),
    'path'      => 'files' . DS
);
$this->set($params);
}


//In view file

echo $this->Html->url(array(
    "controller" => "controller_name",
     "action" => "action_name",
     "ext" => "file_extension_name"
));

Also read Cakephp Media View

Supravat Mondal
  • 2,574
  • 2
  • 22
  • 33
0

This is my ans :

$(document).ready(function(){
    if('<?php echo $filename ?>' !=''){
        window.location.href ='/files/<?php echo $filename  ?>.xls';
        }
});
</script>
Debasis
  • 201
  • 3
  • 9