【问题标题】:File upload Codeigniter and ajax文件上传 Codeigniter 和 ajax
【发布时间】:2012-07-21 03:43:21
【问题描述】:

我不确定我在这里做错了什么。

控制器

function do_test()
{
    $config['upload_path'] = './images';
    $config['allowed_types'] = 'gif|jpg|png';
    $config['max_size'] = '10000';
    $config['max_width']  = '1024';
    $config['max_height']  = '768';
    $config['overwrite'] = TRUE;
    $confit['remove_spaces'] = TRUE;

    $this->load->library('upload', $config);
    $this->load->library('form_validation');

    $this->form_validation->set_rules('file', 'trim|required|xss_clean');
    $test = $this->input->post('file');
    var_dump($test); //var dump return : string(21) "C:\fakepath\truth.jpg"

    if ( ! $this->upload->do_upload($test))
    {
        echo "no";
    }
    else
    {
        $data = array('upload_data' => $this->upload->data());

        echo "yes";
    }
}

表格

<?php echo form_open_multipart('home/do_test', array( 'id' => 'skill-form-test' ) );?>

    <input type="file" name="userfile" id="file" size="20" />

    <br /><br />

    <input type="submit" value="upload" />

    </form>

jquery

$('#skill-form-test').submit(function(e) {
    e.preventDefault();

    $.post(base_url + "index.php/home/do_test", { file : $("#file").val()  }, function(data)
        {
            }, "json");
    alert ( $("#file").val() + "-" + base_url ); //This alerts: C:\fakepath\truth.jpg-http://siteurl.com/
});

【问题讨论】:

  • 我不确定你在这里问什么。简单来说,问题是什么?
  • 为什么图片文件没有上传?
  • @ciprian 为什么要使用json 数据类型来上传图片?
  • 萤火虫没有错误。 @Raminson 我不只是上传图片,我还发送了一些其他信息。图片上传是可选的。但我无法上传文件,所以我决定从头开始。

标签: php jquery codeigniter


【解决方案1】:

更新:该修复适用于 CodeIgniter v3,可能不适用于最新版本。

只是改变

if ( ! $this->upload->do_upload($test))

if ( ! $this->upload->do_upload('userfile'))

将解决问题。

希望这对您有所帮助。谢谢!!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-12-26
    • 1970-01-01
    • 2012-05-25
    • 2012-07-18
    • 2017-10-22
    • 1970-01-01
    • 2014-03-26
    • 2016-04-16
    相关资源
    最近更新 更多