cakephp 2.0 使用 jQuery Ajax 和参数上传文件 - 仍然无法得到任何答案
cakephp 2.0 upload file using jQuery Ajax with parameters - still can't get any answer
我仍然得不到这个问题的任何答案。谁能帮我解决这个问题。
我正在尝试向 select 人发送一封带附件的电子邮件。因此,我从数据库中过滤了数据,并将带附件的电子邮件发送给 selected 用户。
我已经完成了以下回答:
How can I upload files asynchronously?
我已经尝试了 JQuery、Ajax 的以下代码,控制器操作:
<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
并将文件移动到确切路径。
请帮忙。
您需要使用如下表格和FormData
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");
}
}
我仍然得不到这个问题的任何答案。谁能帮我解决这个问题。
我正在尝试向 select 人发送一封带附件的电子邮件。因此,我从数据库中过滤了数据,并将带附件的电子邮件发送给 selected 用户。
我已经完成了以下回答:
How can I upload files asynchronously?
我已经尝试了 JQuery、Ajax 的以下代码,控制器操作:
<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
并将文件移动到确切路径。
请帮忙。
您需要使用如下表格和FormData
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");
}
}