【问题标题】:Cannot upload image using ajax in Codeigniter无法在 Codeigniter 中使用 ajax 上传图片
【发布时间】:2015-10-20 16:57:41
【问题描述】:

我想在 Codeigniter 中使用 ajax 上传图像,我搜索了很多但找不到解决方案。我想使用 jquery 提交表单。下面是我的代码: 当我通过以下代码从视图中检索控制器中的值时,我得到了空白值:

$file_names=$this->input->post('save_movie123');

查看:

<html>
$(document.body).on('click', '.postbtn' ,function(e){
     $('#post_video_255').submit();
});


function sendVideoData()
{
      var form = new FormData(document.getElementById("#post_video_255"));

      $.ajax({
        url: "dashboard/do_upload",
        type: 'POST',
        data: form, 
        mimeType:"multipart/form-data",           
        cache: false,
        contentType: false, 
        processData: false, 

        success: function(data) {
            console.log(data);
            alert(data);
        },
        complete: function(XMLHttpRequest) {
            var data = XMLHttpRequest.responseText;
            console.log(data);
        },
        error: function() {
            alert("ERROR");
        }
    }).done(function() { 
        console.log('Done');

    }).fail(function() {       
        alert("fail!");
    });
}
</script>



<form name="post_video" id="post_video_255" method="post" onsubmit="return sendVideoData()">        
<span style="margin-left: 8px;">
<input type="file" name="save_movie123" id="movie123" />
<input type="button" class="postbtn" id="submit_movie_289" value="Upload Video File"/>
</form>
</html>

控制器:

function do_upload()
{
$file_names=$this->input->post('save_movie123');
$config['upload_path'] ='./uploads/';
$config['allowed_types'] = 'gif|jpg|png||jpeg';
$config['max_width'] = 1000;
$config['max_height'] = 1000;
$config['max_size'] = 200000;
$config['encrypt_name'] = FALSE;

$this->load->library('upload', $config);
$this->upload->do_upload($file_names);
}

【问题讨论】:

  • 你在控制台有任何错误。
  • @Niranjan N Raju 不,我没有收到任何错误 nut ajax 错误部分已执行。

标签: php jquery ajax codeigniter codeigniter-2


【解决方案1】:

使用这个:

$.ajaxSetup({
    data: {
        csrf_test_name: $.cookie('csrf_cookie_name')
    }
});

【讨论】:

  • 谢谢@Radu 但我必须通过图像传递其他值你能指导我使用上面的代码吗?
  • 看看这个:jerel.co/blog/2012/03/…
  • 也将其添加到您的表单中:enctype="multipart/form-data"
猜你喜欢
  • 2020-06-26
  • 2020-07-02
  • 2012-03-25
  • 2012-12-31
  • 1970-01-01
  • 1970-01-01
  • 2012-05-29
  • 2015-01-31
  • 1970-01-01
相关资源
最近更新 更多