在 Jquery 中在页面加载时下载文件

Download a file on page load in Jquery

我的服务器文件夹中有一个文件,现在我想在页面加载时下载该文件。

示例:

如果我点击我们的产品,则会自动从我的服务器下载一个文件到用户系统中。

// 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"
));

另读Cakephp Media View

这是我的答案:

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