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>

EDIT:ヘッダーにmalsup.com jQuery Form Pluginを含めています

4

3 に答える 3

2

この質問は古いですが、私が見つけた解決策を提供します。私はこの問題を解決するために約 2 週間拘束されていましたが、その 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

この行$imgfile = $_POST['imgfile'];を に変更し$imgfile = $_FILES['imgfile'];ます。それでも、これを使用してファイルをアップロードすることはできません。 ajax を使用したフォームの送信については、こちらを参照してください。

于 2013-07-26T11:54:26.167 に答える
0

入力タイプ ファイルの値を投稿データに追加することはできません。それはこれがどのように機能するかではありません。

本当に非同期 JS でファイルをアップロードしたい場合は、CodeIgniter と Uploadify をチェックしてください。

于 2013-07-26T11:50:56.780 に答える