codeigniter通过ajax上传图片

时间:2016-03-11 08:15:00

标签: php jquery html ajax codeigniter

我正在尝试使用codeigniter通过ajax将图像上传到某个位置以及如何在控制器中接收。

我搜索太多但不知道问题出在哪里!

Ajax代码:

 $('#btnform2').click(function(e){
        e.preventDefault();
 // valadation_form();
 $("form#data").submit(function(event){

//disable the default form submission
 event.preventDefault();

  //grab all form data  
var formData = new FormData($(this)[0]);

 $.ajax({
  url: '<?php echo base_url(); ?>"+"index.php/club/post_ajax',
  type: 'POST',
   data: formData,
  async: false,
  cache: false,
  contentType: false,
  processData: false,
  success: function (returndata) {
  alert(returndata);
 }
 });

 return false;
 alert("test");
});
 });

我的表单

<form method="post" class="form-horizontal" id="form_members" role="form" name="uploader">
<label for="fullname" class="col-sm-2">Full Name</label>
<input type="text" class="form-control" name="fullname" id="firstname" placeholder="Full Name" >
<label for="lastname" class="col-sm-2">Computer Number</label>
<input type="text" class="form-control" name="computernumber" id="lastname" placeholder="Computer Number">
<label for="address" class="col-sm-2">E- mail Address</label>
<input type="email" class="form-control" name="email" id="address" placeholder="Email Address">
<label for="city" class="col-sm-2">Mobile Number</label>
<input type="text" class="form-control" list="cities" name="mobilenumber" id="mobilenumber" placeholder="Mobile Number">
<label for="phone" class="col-sm-2">Nationality</label>
 <input type="tel" class="form-control" name="Nationality" id="nationality" placeholder="nationality">
<label for="email" class="col-sm-2">Upload Photo</label>
<input id="file" class="file" type="file" multiple data-min-file-count="1">
<button name="submit" class="btn btn-warning" id="btnform2">Update Player Profile</button>
</form>

2 个答案:

答案 0 :(得分:2)

在没有提交事件的情况下尝试此操作:

 $("form#form_members button").click(function(event){

//disable the default form submission
 event.preventDefault();

  //grab all form data  
var formData = new FormData($(this).parent('form')[0]);

 $.ajax({
  url: '<?php echo base_url(); ?>/index.php/club/post_ajax',
  type: 'POST',
   data: formData,
  success: function (returndata) {
  alert(returndata);
 }
 });

});

答案 1 :(得分:0)

此行语法不正确..

url: '<?php echo base_url(); ?>"+"index.php/club/post_ajax',

更改为

url: '<?php echo base_url(); ?>'+'index.php/club/post_ajax',

你也应该尝试这个而不是假..

contentType: 'multipart/form-data',

看看是否有效,如果没有,请告诉我,我还会看到我能找到的其他内容。