2015-03-02 54 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