javascript - Formdata append file upload is not working in codelgniter,Getting error - Stack Overflow

All I am trying to do is upload files using ajax to my CodeIgniter based website.But i cannot get file

All I am trying to do is upload files using ajax to my CodeIgniter based website.But i cannot get file field value in controller.I am getting message like "Undefined index: 'file_1'"

How to solve this issue?

Form

<form method="post" enctype="multipart/form-data">
 <input type="file" id="file_1" name="file_1" value="" class="field1" /> 
 <input type="button" onclick="up_img()" value="Upload" /> 
</form>

Javascript:

<script type="text/javascript">
function up_img()
{   
   formdata = false;
    var imgfile = document.getElementById("file_1"); 
    formdata = new FormData(); 
    formdata.append("file_1",imgfile.files[0]);
    $.ajax({  
        url: "<?php echo base_url(); ?>index.php/save_project_upload/",  
        type: 'POST',   
        data: formdata,
        processData: false,
        contentType: false,
        success: function (data) {  
                alert(data); 
        }  
    });  
} 

Controller:

 function save_project_upload()
 {

echo $upfile_name           =       $_FILES['file_1']['name'];

 }

All I am trying to do is upload files using ajax to my CodeIgniter based website.But i cannot get file field value in controller.I am getting message like "Undefined index: 'file_1'"

How to solve this issue?

Form

<form method="post" enctype="multipart/form-data">
 <input type="file" id="file_1" name="file_1" value="" class="field1" /> 
 <input type="button" onclick="up_img()" value="Upload" /> 
</form>

Javascript:

<script type="text/javascript">
function up_img()
{   
   formdata = false;
    var imgfile = document.getElementById("file_1"); 
    formdata = new FormData(); 
    formdata.append("file_1",imgfile.files[0]);
    $.ajax({  
        url: "<?php echo base_url(); ?>index.php/save_project_upload/",  
        type: 'POST',   
        data: formdata,
        processData: false,
        contentType: false,
        success: function (data) {  
                alert(data); 
        }  
    });  
} 

Controller:

 function save_project_upload()
 {

echo $upfile_name           =       $_FILES['file_1']['name'];

 }
Share edited Nov 19, 2019 at 16:06 Mosè Raguzzini 15.9k1 gold badge34 silver badges46 bronze badges asked Sep 18, 2014 at 8:07 WilliamsWilliams 712 silver badges4 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 4

If you do a print_r( $_FILES ) are you getting any content?

Also, be aware that this is not supported by IE8 and earlier.

You could also try to do:

var formData = new FormData($('#yourformID')[0]);     

EDIT:

formData.append('file_1', $('input[id="file_1"]')[0].files[0]); 

Might also solve your problem, by adding [0] to the DOM element.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745074132a4609731.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信