我想在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);
}
答案 0 :(得分:0)
使用此:
$.ajaxSetup({
data: {
csrf_test_name: $.cookie('csrf_cookie_name')
}
});