duannan4486 2017-04-24 23:49
浏览 73
已采纳

cakephp 2.0使用带参数的jQuery Ajax上传文件 - 仍无法得到任何答案

I am still can't get any answer of this question. Could any one help me out this issue.

I am trying to send an email with attachment to select person. So, I have filtered the data from database and send email with attachment to selected users.

I have gone through the below answer:

How can I upload files asynchronously?

I have tried the below code of JQuery, Ajax, Controller Action:

<script type="text/javascript">
function SendEmail(){
  var checked=0;
  jQuery("#action_row").val('delete');
  var filename = jQuery("#UserPdf").val();
  var audits    =   '';
  $('.checkboxes:checked').each(function(){
     checked=1;         
     audits +=  this.value+',';  
  });
 var auditsval  =   audits.slice(0, -1);
  //alert(auditsval);
  if(checked==1){
      var con   =   confirm('Are you sure you want to send email?');
      if(con){
          jQuery.ajax({
            type: "POST",
            url: CommanPath.basePath+'admin/users/send_email',
            enctype: 'multipart/form-data',
            data: {
                file: filename,
                ids: auditsval,
            },
            success: function (data) {
                alert("Email sent to selected users ");
            }
        });
      }

  }else{
    alert("Please select atleast one to send email");  
  }

}
</script>

UsersCOntroller.php

public function admin_send_email($id = null){
    //echo 'here'; die;

    $this->layout = 'ajax';
    echo '<pre>'; print_r($_FILES); die;

    if(isset($this->params['data']['file']) && $this->params['data']['file']){

        $filename = time().$this->params['data']['file'];
        move_uploaded_file($_FILES['data']['tmp_name']['file']['name'], WWW_ROOT.'uploads/sellingtools/' . $filename);
    }
    $userids = explode(",", $this->params['data']['ids']);
    echo '<pre>'; print_r($userids); die;
}

When I am trying to get uploaded file name, I am getting only file name not getting the tmp_name.

Anyone can let me know how I can get the tmp_name and move file to exact path.

Please help.

  • 写回答

1条回答 默认 最新

  • dongxian7489 2017-04-27 13:31
    关注

    You need to use Form and FormData as below

    HTML:

    <form id="myForm" method="post" enctype="multipart/form-data">
        <input type="file" name="file">
    </form>
    

    JQUERY:

    function SendEmail(){
      var checked=0;
      jQuery("#action_row").val('delete');
      var filename = jQuery("#UserPdf").val();
      var audits    =   '';      
      $('.checkboxes:checked').each(function(){
         checked=1;         
         audits +=  this.value+',';  
      });
      var auditsval  =   audits.slice(0, -1);
      //alert(auditsval);
    
      var formdata = new FormData($('#myForm')[0]); /////formdata object
      formdata.append('ids',auditsval); /// add additional fields 
    
      if(checked==1){
          var con   =   confirm('Are you sure you want to send email?');
          if(con){
              jQuery.ajax({
                type: "POST",
                url: CommanPath.basePath+'admin/users/send_email',
                processData: false,  ///required to upload file
                contentType: false,  /// required
                data: formdata,       /// send form data
                success: function (data) {
                    alert("Email sent to selected users ");
                }
            });
          }
    
      }else{
        alert("Please select atleast one to send email");  
      }
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料