在jquery里面的cakephp 2.x中创建下载链接

时间:2018-06-17 10:03:15

标签: javascript jquery cakephp cakephp-2.0

我必须创建动态html下载链接,但我在jquery部分中获取文件名和文件夹名称。那么如何将$ id和$ filename从jquery传递给输入字段,或者是否有任何其他方式直接在jquery中创建与cakephp的动态html链接。?

      <td class="broucher">
      </td>

我正在尝试Jquery,但是它没有工作并且出现意外的标识符错误。

 var html='';
 var filename=ui.item.brouchersFile;
 var id=ui.item.brouchersId;
 html = '<?php echo $this->Html->Link("",array("controller"=> "download","action"=>"brochures",${id},${filename}),array("class"=>"fa fa-download fa-fw","download"=>${filename},"title"=>"Download"));  ?>' ;
 $(elmt).find(".broucher").append("<span class='msgspan'>${html}</span>");

1 个答案:

答案 0 :(得分:0)

You can define hidden field to access base url and then use the same in jquery to created the link for your desired action.

$this->Form->input('webroot', ['type' => 'hidden', 'id' => 'webroot', value=>$this->request->webroot]);

and then use that in your jquery like below

 var html='';
 var filename=ui.item.brouchersFile;
 var id=ui.item.brouchersId;
 var webroot= $("#webroot").val();
 html = '<a href="'+webroot+'/download/brochures/'+id+'/'+filename+'" class="fa fa-download fa-fw" download>Download</a>';
 $(elmt).find(".broucher").append("<span class='msgspan'>"+html+"</span>");