繁体   English   中英

无法在Codeigniter中使用Ajax上传图片

[英]Cannot upload image using ajax in Codeigniter

我想在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);
}

用这个:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM