Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/247.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript CodeIgniter文件上载不理解ajax文件提交_Javascript_Php_Jquery_Ajax_Codeigniter - Fatal编程技术网

Javascript CodeIgniter文件上载不理解ajax文件提交

Javascript CodeIgniter文件上载不理解ajax文件提交,javascript,php,jquery,ajax,codeigniter,Javascript,Php,Jquery,Ajax,Codeigniter,我编写了一个JS脚本、一个CodeIgniter控制器和一个html表单,通过jQueryAjax上传文件。 它不上传文件。我已经解决了所有的问题,但最后一个问题超出了我的能力。 AJAX请求成功完成,但是codeigniterupload类错误“您没有选择要上载的文件” 这是我的HTML文件和按钮: <input type="file" class="form-file" id="file" multiple name="file[]" />

我编写了一个JS脚本、一个CodeIgniter控制器和一个html表单,通过jQueryAjax上传文件。 它不上传文件。我已经解决了所有的问题,但最后一个问题超出了我的能力。 AJAX请求成功完成,但是
codeigniter
upload类错误“您没有选择要上载的文件”

这是我的HTML文件和按钮:

<input type="file" class="form-file" id="file" multiple name="file[]" />
                    <input value='add picture' type="button" name="file-submit" class="form-submit" id='file-submit'/>
下面是jQuery AJAX脚本:

$(document).ready(function()
{


       var files; // main variable to keep images into it

       // lets put the files into a variable
       var file_field = $('input[type=file]');
       file_field.on('change', appendFiles);
       function appendFiles (event)
       {
             files = event.target.files;
             console.log(files);
       }

       // now attach click event to upload

       $('#file-submit').on('click',

       function uploadFiles (event)
       {

            event.preventDefault();  

            // create a form data
            var data = new FormData();
            $.each(files, function (key, val)
            {
                data.append(key, val);
                // now all the contents of the files have been assigned to a form-data variable
            });

            // begin the AJAX
            $.ajax({
                url : base_path("upload/pictures/"),
                data: data,
                cache:false,
                processData: false,
                contentType: false,
                success: function(msg)
                {
                        console.log(msg);
                },              
                error : function()
                {
                    console.log("Error");   
                }
            });

       });                 

    //   form_file
});

使用jQueryFileUpload插件:

下载JS:

-看法-

<!doctype html>
<html>
 <head>
   <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
   <script src="<?php echo base_url('js/ajaxfileupload.js')?>"></script>
</head>
<body>
  <h1>Upload File</h1>
  <form method="post" action="" id="upload_file">
     <label for="title">Title</label>
     <input type="text" name="title" id="title" value="" />

     <label for="userfile">File</label>
     <input type="file" name="userfile" id="userfile" size="20" />

     <input type="submit" name="submit" id="submit" />
  </form>
<h2>Files</h2>
<div id="files"></div>
</body>
</html>

您可以使用jQueyr插件来实现它
<!doctype html>
<html>
 <head>
   <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
   <script src="<?php echo base_url('js/ajaxfileupload.js')?>"></script>
</head>
<body>
  <h1>Upload File</h1>
  <form method="post" action="" id="upload_file">
     <label for="title">Title</label>
     <input type="text" name="title" id="title" value="" />

     <label for="userfile">File</label>
     <input type="file" name="userfile" id="userfile" size="20" />

     <input type="submit" name="submit" id="submit" />
  </form>
<h2>Files</h2>
<div id="files"></div>
</body>
</html>
$(function() {
   $('#upload_file').submit(function(e) {
      e.preventDefault();
      $.ajaxFileUpload({
         url         :'./upload/upload_file/', 
         secureuri      :false,
         fileElementId  :'userfile',
         dataType    : 'json',
         data        : {
            'title'           : $('#title').val()
         },
         success  : function (data, status)
         {
            if(data.status != 'error')
            {
               $('#files').html('<p>Reloading files...</p>');
               refresh_files();
               $('#title').val('');
            }
            alert(data.msg);
         }
      });
      return false;
   });
});
public function upload_file()
{
   $status = "";
   $msg = "";
   $file_element_name = 'userfile';
   if (empty($_POST['title']))
   {
      $status = "error";
      $msg = "Please enter a title";
   }
   if ($status != "error")
   {
      $config['upload_path'] = './files/';
      $config['allowed_types'] = 'gif|jpg|png|doc|txt';
      $config['max_size']  = 1024 * 8;
      $config['encrypt_name'] = TRUE;

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

      if (!$this->upload->do_upload($file_element_name))
      {
         $status = 'error';
         $msg = $this->upload->display_errors('', '');
      }
      else
      {
         $data = $this->upload->data();
         $file_id = $this->files_model->insert_file($data['file_name'],     $_POST['title']);
         if($file_id)
         {
            $status = "success";
            $msg = "File successfully uploaded";
         }
         else
         {
            unlink($data['full_path']);
            $status = "error";
            $msg = "Something went wrong when saving the file, please try again.";
         }
      }
      @unlink($_FILES[$file_element_name]);
   }
   echo json_encode(array('status' => $status, 'msg' => $msg));
}