php - cakephp 2.0 upload file using jQuery Ajax with parameters - still can't get any answer

507

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 thetmp_name.

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

Please help.

483

Answer

Solution:

You need to use Form andFormData 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");  
  }

}

People are also looking for solutions to the problem: php - Select data from 4 tables using Active Record Class in CodeIgniter

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.