codeigniter ajaxfileupload.js脚本错误 - 您没有选择要上载的文件

时间:2012-05-07 17:45:17

标签: jquery codeigniter image-uploading

这是我的codeigniter代码中使用ajaxfileupload和Jquery上传图像的代码。但是codeigniter代码在日志文件中给出了错误。它的上传。但是当我在没有jquery和ajaxfileupload脚本的情况下测试相同的代码时。

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<script src="//localhost/AjaxFileUploader/ajaxfileupload.js"></script>    
<script type="text/javascript">        
    $(document).ready(function () {
        $("#form").change(function () {
            $("#upload").html('<img src="loader.gif"/>');
            $.ajaxFileUpload({
                url: 'http://localhost/ci/index.php/upload/do_upload',
                secureuri: false,
                fileElementId: 'userfile',
                dataType: 'json',
                success: function (data, status) {
                    if (typeof (data.error) != 'undefined') {
                        if (data.error != '') {
                            alert(data.error);
                        } else {
                            alert(data.msg);
                        }
                    }
                },
                error: function (data, status, e) {
                    alert(e);
                }
            });
        });
    });
</script></head>
<body>
    <form action="http://localhost/ci/index.php/upload/do_upload" method="post" accept-charset="utf-8" enctype="multipart/form-data">
        <input type="file" name="userfile" size="20" id="form" />
        <br /><br />
        <input type="submit" value="upload" id="submit" />
        <span id="upload"></span>
    </form></body>
</html>

我的codeigniter代码是

function do_upload()
    {
        $config['upload_path'] = 'c:\upload';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size'] = '100';
        $config['max_width']  = '1024';
        $config['max_height']  = '768';
         $config['encrypt_name'] = TRUE;

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

        if ( ! $this->upload->do_upload())
        {
            $error = array('error' => $this->upload->display_errors());

            $this->load->view('upload_form', $error);
        }
        else
        {
            $data = array('upload_data' => $this->upload->data());

            $this->load->view('upload_success', $data);
        }
    }

但是codeigniter没有上传文件。它在日志文件中给出错误,例如“您没有选择要上传的文件。”

1 个答案:

答案 0 :(得分:0)

我认为你需要改变这一行:

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