AJAX Codeigniter - 您没有选择要上传的文件(错误) - 如何通过AJAX传递文件 [英] AJAX Codeigniter - You did not select a file to upload(error)- How to pass the file through AJAX

查看:407
本文介绍了AJAX Codeigniter - 您没有选择要上传的文件(错误) - 如何通过AJAX传递文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过AJAX调用和Codeigniter上传图片:

I am trying to upload images via an AJAX call and Codeigniter:

我的视图:

            <?php echo form_open_multipart('upload/do_upload'); ?>
            <input type="file" name="userfile" id="userfile" size="20" />
            <br />
            <input type="submit" value="upload" id="upload_file_1" />
            </form>

我的Ajax调用:

$(document).ready(function(){
    $(function() {
        $('#upload_file_1').click(function(e) {
            e.preventDefault();
            var filename = $("#userfile").val();
            $.ajax({
                url         :'index.php/upload/do_upload', 
                secureuri      :false,                   
                 fileElementId: 'userfile',
                dataType    : 'json',
                type : 'POST',
                done  : function (data)
                {

                    alert(data);
                }
            });               
        });
    });
});

和我的控制器:

class Upload extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->load->helper(array('form', 'url'));
    }

    function index() {
        $this->load->view('upload_form', array('error' => ' '));
    }

    function do_upload() {
        $config['upload_path'] = './uploads/';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size'] = '1000000';
        $config['max_width'] = '10240';
        $config['max_height'] = '7680';

        $this->load->library('upload', $config);
        if (!$this->upload->do_upload('userfile')) {                        
            $error = array('error' => $this->upload->display_errors());    
            echo "error!";
            echo "<pre>";
            print_r($error);
            echo "<pre/>";
        } else {
            echo "done!";
        }
    }

}

给我一个错误,说:你没有选择要上传的文件。没有AJAX它工作正常,可能是我的AJAX调用是不正确的!

but it gives me an error saying : "You did not select a file to upload. "; without AJAX it works fine, probably my AJAX call is not right! Could you please let me know if I am doing something wrong?

感谢

推荐答案

我使用这个javascript库
https://github.com/blueimp/ jQuery-File-Upload
,并且使用ajax - 拖放/拖放+进度条来获得文件上传,并返回可以立即下载的相应文件图标或图像缩略图。很难得到它的完全工作和抽象,所以同一个例程处理多种类型的上传,每个都有自己的限制。
链接将给出一些基本的示例代码,值得一看。

i used this javascript library https://github.com/blueimp/jQuery-File-Upload and got file uploading working with ajax - drag/drop + progress bars, and returns the appropriate file icon or image thumbnail that could be downloaded straight away. it was quite difficult to get it fully working and abstracted so the same routine handled multiple types of uploads each with their own restrictions. the link will give some basic example code, it is worth a look.

pete

这篇关于AJAX Codeigniter - 您没有选择要上传的文件(错误) - 如何通过AJAX传递文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆