【问题标题】:cakephp 2.0 upload file using jQuery Ajax with parameters - still can't get any answercakephp 2.0 使用带有参数的 jQuery Ajax 上传文件 - 仍然无法得到任何答案
【发布时间】:2017-09-21 19:13:10
【问题描述】:

我仍然无法得到这个问题的任何答案。谁能帮我解决这个问题。

我正在尝试向选定人员发送带有附件的电子邮件。因此,我已经过滤了数据库中的数据,并将带有附件的电子邮件发送给选定的用户。

我已经完成了以下答案:

How can I upload files asynchronously?

我已经尝试过以下 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;
}

当我尝试获取上传的文件名时,我得到的只是文件名而不是tmp_name

任何人都可以告诉我如何获取tmp_name 并将文件移动到确切路径。

请帮忙。

【问题讨论】:

    标签: php jquery ajax file-upload cakephp-2.0


    【解决方案1】:

    您需要使用Form和FormData如下

    HTML:

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

    查询:

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

    【讨论】:

    • 感谢您的回答,我会试试这个并会更新您。
    猜你喜欢
    • 1970-01-01
    • 2012-03-12
    • 1970-01-01
    • 2013-05-21
    • 1970-01-01
    • 1970-01-01
    • 2011-02-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多