【问题标题】:Code Igniter File Uploading Class with Jquery Ajax使用 Jquery Ajax 的 Codeigniter 文件上传类
【发布时间】:2014-03-09 05:12:36
【问题描述】:

所以我想要完成的只是使用 File Uploading Class 上传带有 codeigniter 的文件

对于我的控制器,我有以下代码:

function do_upload()
{
    $config['upload_path'] = './assets/productImages/';
    $config['allowed_types'] = 'gif|jpg|png';
    $config['max_size'] = '100';
    $config['max_width']  = '1024';
    $config['max_height']  = '768';

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

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

        var_dump($error);
    }
    else
    {
        $data = array('upload_data' => $this->upload->data());
    }
}

然后我的表格如下:

$data['productImage'] = array(
   "name" => "userfile",
   "id" => "product_image",
);

 // displays out as: 
 <input type="file" name="userfile" value="" id="product_image">

我的 javascript 如下,它使用 onchange 操作来触发上传过程。

$("document").ready(function(){
            $("#product_image").change(function() {
                var path = $(this).val();
                var dataString = 'userfile=' + path;
                //alert(dataString); return false;
                $.ajax({
                    type: "POST",
                    url: "/account/do_upload/",
                    data: dataString,
                    success: function(data) {
                        alert(data);
                    }
                });
                return false;
            });
        });

当我运行此代码时,我在 javascript 警报中收到此消息

You did not select a file to upload.

我需要添加什么才能完成这项工作?我猜这与通过 javascript ajax 处理上传有关。

*因评论而更新 *

我的表单标签如下:

$data['newPostForm'] = array('id' => 'newPostForm', 'type' => 'multipart');

// And echoes out as :
 <form action="http://myurl.com/index.php/account/submitPost" method="post" accept-charset="utf-8" id="newPostForm" type="multipart">

【问题讨论】:

  • 控制器中的 print_r($_FILES) 得到了什么?
  • 你能显示你的表单标签代码吗?
  • 检查更新的问题。
  • type="multipart"?这是新事物。你发明的?
  • 对不起,应该是enctype="multipart/form-data"

标签: javascript php jquery ajax codeigniter


【解决方案1】:

您的 codeigniter 代码看起来不错。
问题是您不能直接通过 Ajax 上传文件。您需要通过 iframe 执行此操作。
这个插件在这里很有效。
或者,如果您只需要它在 html 5 中工作,那么这应该可以解决问题 http://www.devbridge.com/projects/html5-ajax-file-upload/

【讨论】:

  • 告诉我如何在不重新加载页面和代码点火器友好的情况下完成这项工作,我会接受答案。不接受链接。
  • -1 提供了一个误导性的答案。您当然可以在除 IE9 和更早版本之外的所有浏览器中通过 Ajax 上传文件。如果使用旧版浏览器,请回退到仅为这些浏览器上传 iframe。
猜你喜欢
  • 1970-01-01
  • 2012-05-25
  • 2012-07-18
  • 2015-05-11
  • 2013-03-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多