我使用CI 3.0.1上传和插入图像数据库成功之前,我用ajax,我想尝试用ajax做我失去了一些不是甚至发送数据上传也许是因为我们必须在窗体中使用multipart(),而在ajax中,我们只是直接向控制器发送数据,另一件事我不知道如何接收响应中的变量
我的Ajax请求函数是:
Codeigniter:上传图像通过Ajax和存储在分区
<script type="text/javascript">
$(document).ready(function() {
alert("thirddddddddd");
$('#btnsubmit').click(function()
{
alert("i got submitted");
event.preventDefault();
var userfile = $("input#pfile").val();
alert("uploading");
$.ajax({
url: '<?php echo base_url(); ?>upload/do_upload', //how to receive var here ?
type: 'POST',
cache: false,
data: {userfile: userfile},
success: function(data) {
alert(data);
$('.img_pre').attr('src', "<?php echo base_url().'uploads/' ?>");
$('.dltbtn').hide();
},
error: function(data)
{
console.log("error");
console.log(data);
alert("Error :"+data);
}
});
});
});
我的控制器上传的功能do_upload是:
public function do_upload()
{
$config['upload_path'] = './uploads/'; #$this->config->item('base_url').
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$this->upload->initialize($config);
if (! $this->upload->do_upload('userfile'))
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('layouts/header');
$this->load->view('home_page', $error);
$this->load->view('layouts/footer');
}
else
{
$data = array('upload_data' => $this->upload->data());
$imagedata = $this->input->post('uerfile');
$session_data = $this->session->userdata('logged_in');
$id = $session_data['id'];
$result = $this->model_edit->update_dp($id, $imagedata);
$image_name = $result->images;
$this->session->set_userdata('image', $image_name);
echo json_encode($name = array('image' => $image_name));
// $image_name = $this->upload->data('file_name');
// $data = array('upload_data' => $this->upload->data());
// redirect("account");
}
}
现在,图像甚至不打算上传文件夹,如果需要任何其他文件告诉我,我会在这里发布。由于
是可上传的文件夹吗?它位于公共根? –
是它的根,正如我所说的一切工作正常,当我这样做没有ajax –
Javascript实际上看不到文件的内容在文件输入,所以它无法读取内容,以便发送数据通过Ajax发送到服务器。对此有一些解决方法,比如将表单提交到页面上的iframe以避免重新加载。 – shamsup