2015-03-02 50 views
-1

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

例子:

如果我对我们的产品比点击一个文件已被自动用户系统中从我的服务器下载。

+0

看看[此链接](http://stackoverflow.com/questions/1296085/download-文件 - 使用 - jQuery的) – lshettyl 2015-03-02 13:49:24

回答

0

这是我的答案:

$(document).ready(function(){ 
    if('<?php echo $filename ?>' !=''){ 
     window.location.href ='/files/<?php echo $filename ?>.xls'; 
     } 
}); 
</script> 
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" 
)); 

又读Cakephp Media View