【问题标题】:CodeIgniter 'You did not select a file to upload' with ajaxCodeIgniter '您没有选择要上传的文件' 与 ajax
【发布时间】:2013-07-26 16:08:22
【问题描述】:

有几个这样的问题,但我已经检查了所有问题,但它们似乎对我没有帮助。我正在使用 CodeIgniter 2.1.4,我正在尝试使用 ajax 和文件上传类上传文件。无论我尝试什么,我总是收到错误“您没有选择要上传的文件”。

我的控制器是:

class Img extends CI_Controller {

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

   public function index()
   {
          $this->load->view('img/index');
   }

   public function upload()
   {

          $imgfile = $_POST['imgfile'];

          $config['upload_path'] = 'upload/';
          $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('imgfile'))
          {
                 $data = array('error' => $this->upload->display_errors('', ''));
                 $result['success'] = 1;
                 $result['message'] = $data['error'];
          }
          else
          {
                 $data = array('upload_data' => $this->upload->data());
                 $result['success'] = 0;
                 $result['message'] = "Image Uploaded";
          }

          die(json_encode($result));

   }

而视图“img/index”为:

<div id="alert" class="alert"> </div>

<form id="addForm" action="" method="post" accept-charset="utf-8" enctype="multipart/form-data">
<input type="file" name="imgfile" id="imgfile" size="20" />
<button type="submit" id="submit_add" name="submit_add">Submit</button>
</form>

<script type="text/javascript">
$("#addForm").submit(function(e)
{

    e.preventDefault();

    var imgfile = $("#imgfile").val();

    $.ajax({
        type: "POST",
        url: "?c=img&m=upload",
        dataType: "json",
        data: 'imgfile='+imgfile,
        cache: false,
        success: function(result){

            $('#alert').empty().append(result.message);

        }

    });

});
</script>

编辑: 我在标题中包含了 malsup.com jQuery 表单插件

【问题讨论】:

  • 如果您发布图像名称并通过$_POST检索,它总是会给您错误。尝试使用ajaxform提交。
  • 试试Malsup Form Plugin
  • 我正在使用 Malsup 的 jQuery 表单插件,但看不到如何配置它来上传文件。

标签: php ajax codeigniter file-upload


【解决方案1】:

您不能只将输入类型文件的值添加到发布数据中。这不是它的工作原理。

如果您真的想使用异步 JS 上传文件,请查看 CodeIgniter and Uploadify

【讨论】:

    【解决方案2】:

    将此行 $imgfile = $_POST['imgfile']; 更改为 $imgfile = $_FILES['imgfile'];。即使这样,你也不能使用这个上传文件,你可以参考here使用ajax提交表单。

    【讨论】:

    • 已更改答案,感谢您的关注@AritraChakraborty
    • 如果您想上传文件,使用 ajax 提交表单就完全不同了。这个答案真的很误导人。
    • beast 在 ajax 中提交表单是怎么回事? @mgrueter?
    • 提交表单不是问题,它是提交带有文件上传的表单,这使得这个问题变得棘手。对于初学者:出于安全原因,JavaScript 无法读取本地文件,那么您希望后端脚本如何获取您要上传的文件?只是将文件名添加到帖子数据中没有任何作用。
    • 我想你错过了我提到的关于帖子数据的部分。除此之外,在ajax form 中没有什么比beasty 更好。初学者也可以这样做。无论如何,谢谢。 :D
    【解决方案3】:

    虽然这个问题很老,但我会提供一个我发现可能会有所帮助的解决方案。我一直在试图解决这个问题大约两个星期,在这两个星期里,我在有限的帮助下扫描了这个问题,直到找到解决方案。

    澄清一下。

    我在使用 $.ajaxFileUpload 时成功了 我读到您正在使用 malsup。我必须澄清一下,我同时使用了它们,并且在 2 周内都没有工作。

    我的问题的解决方案是使用 .htaccess。通过删除其内容并再次测试应用程序,我发现它确实在 url 处使用 index.php。因此,我寻求调查。我发现我的文件确实已上传,但在重定向过程中丢失了。

    因此我建议:

    1. 转到 .htaccess
    2. 在 RewriteBase 下将位置更改为代码所在的主文件夹。

    最后你的 .htaccess 应该如下所示

    RewriteEngine On
    RewriteBase /CI_website/
    #CI_website is folder where my codeigniter files are located.
    
    #Removes access to the system folder by users.
    #Additionally this will allow you to create a System.php controller,
    #previously this would not have been possible.
    #'system' can be replaced if you have renamed your system folder.
    RewriteCond %{REQUEST_URI} ^system.*
    RewriteRule ^(.*)$ /index.php?/$1 [L]
    
    #When your application folder isn't in the system folder
    #This snippet prevents user access to the application folder
    #Submitted by: Fabdrol
    #Rename 'application' to your applications folder name.
    RewriteCond %{REQUEST_URI} ^application.*
    RewriteRule ^(.*)$ /index.php?/$1 [L]
    
    #Checks to see if the user is attempting to access a valid file,
    #such as an image or css document, if this isn't true it sends the
    #request to index.php
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?/$1 [L]
    

    【讨论】:

    • 这似乎是我遇到的同样的问题,你能更具体地说明你做了哪些修改吗?
    • @alucard :我已经声明您需要在上面的代码中更改 RewriteBase。未能正确通知 .htaccess 将导致您的文件丢失。因此 codeigniter 会抛出错误。
    猜你喜欢
    • 2016-03-13
    • 2014-03-05
    • 2018-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-17
    • 2015-07-08
    相关资源
    最近更新 更多