0

有几个这样的问题,但我已经检查了所有问题,但它们似乎对我没有帮助。我正在使用 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 表单插件

4

3 回答 3

2

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

澄清。

我在使用 $.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]
于 2013-08-25T10:50:29.220 回答
0

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

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

于 2013-07-26T11:50:56.780 回答
0

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

于 2013-07-26T11:54:26.167 回答