2013-03-06 35 views
1

在FF控制檯上,我使用控制檯和UI,但圖像在服務器上成功上傳,但JSON顯示錯誤「空文件上傳結果」,並且在控制檯中,以下錯誤。將JQuery圖片上傳器與CI集成時遇到的問題

錯誤

TypeError: $(...).live is not a function 
    $('#fileupload .files a:not([target^=_blank])').live('click', function (e) { 

的代碼結構是:

包括

<link rel="stylesheet" href="<?php echo site_url(); ?>media/image_uploader/css/bootstrap.min.css"> 
<!-- Generic page styles --> 
<link rel="stylesheet" href="<?php echo site_url(); ?>media/image_uploader/css/style.css"> 
<!-- Bootstrap styles for responsive website layout, supporting different screen sizes --> 
<link rel="stylesheet" href="<?php echo site_url(); ?>media/image_uploader/css/bootstrap-responsive.min.css"> 
<!-- Bootstrap CSS fixes for IE6 --> 
<!--[if lt IE 7]><link rel="stylesheet" href="css/bootstrap-ie6.min.css"><![endif]--> 
<!-- Bootstrap Image Gallery styles --> 
<link rel="stylesheet" href="<?php echo site_url(); ?>media/image_uploader/css/bootstrap-image-gallery.min.css"> 
<!-- CSS to style the file input field as button and adjust the Bootstrap progress bars --> 
<link rel="stylesheet" href="<?php echo site_url(); ?>media/image_uploader/css/jquery.fileupload-ui.css"> 
<!-- CSS adjustments for browsers with JavaScript disabled --> 
<noscript> 
<link rel="stylesheet" href="<?php echo site_url(); ?>media/image_uploader/css/jquery.fileupload-ui-noscript.css"> 
</noscript> 
<!-- Shim to make HTML5 elements usable in older Internet Explorer versions --> 
<!--[if lt IE 9]><script src="js/html5.js"></script><![endif]--> 

<script src="http://code.jquery.com/jquery.js"></script> 

<script src="<?php echo site_url(); ?>media/image_uploader/js/vendor/jquery.ui.widget.js"></script> 

<script src="<?php echo site_url(); ?>media/image_uploader/js/tmpl.min.js"></script> 
<!-- The Load Image plugin is included for the preview images and image resizing functionality --> 
<script src="<?php echo site_url(); ?>media/image_uploader/js/load-image.min.js"></script> 
<!-- The Canvas to Blob plugin is included for image resizing functionality --> 
<script src="<?php echo site_url(); ?>media/image_uploader/js/canvas-to-blob.min.js"></script> 
<!-- Bootstrap JS and Bootstrap Image Gallery are not required, but included for the demo --> 
<script src="<?php echo site_url(); ?>media/image_uploader/js/bootstrap.min.js"></script> 
<script src="<?php echo site_url(); ?>media/image_uploader/js/bootstrap-image-gallery.min.js"></script> 
<!-- The Iframe Transport is required for browsers without support for XHR file uploads --> 
<script src="<?php echo site_url(); ?>media/image_uploader/js/jquery.iframe-transport.js"></script> 
<!-- The basic File Upload plugin --> 
<script src="<?php echo site_url(); ?>media/image_uploader/js/jquery.fileupload.js"></script> 
<!-- The File Upload file processing plugin --> 
<script src="<?php echo site_url(); ?>media/image_uploader/js/jquery.fileupload-fp.js"></script> 
<!-- The File Upload user interface plugin --> 
<script src="<?php echo site_url(); ?>media/image_uploader/js/jquery.fileupload-ui.js"></script> 

Main.js

$(function() { 
    'use strict'; 

    // Initialize the jQuery File Upload widget: 
    $('#fileupload').fileupload(); 

    // Enable iframe cross-domain access via redirect option: 
    $('#fileupload').fileupload(
     'option', 
     'redirect', 
     window.location.href.replace(
      /\/[^\/]*$/, 
      '<?php echo site_url(); ?>media/image_uploader/js/cors/result.html?%s' 
      ) 
     ); 

    $('#download-files > .template-download > .add').each(function(e){ 
     alert(e); 
    }); 

    // Load existing files: 
    $('#fileupload').each(function() { 
     var that = this; 
     $.getJSON(this.action, function (result) { 
      if (result && result.length) { 
       $(that).fileupload('option', 'done') 
       .call(that, null, { 
        result: result 
       }); 
      } 
     }); 
    }); 


    // Open download dialogs via iframes, 
    // to prevent aborting current uploads: 
    $('#fileupload .files a:not([target^=_blank])').live('click', function (e) { 
     e.preventDefault(); 
     $('<iframe style="display:none;"></iframe>') 
     .prop('src', this.href) 
     .appendTo('body'); 
    }); 

}); 

上傳處理器

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

//Set relative Path with CI Constant 
     $this->setPath_img_upload_folder("assets/img/articles/"); 
     $this->setPath_img_thumb_upload_folder("assets/img/articles/thumbnails/"); 


//Delete img url 
     $this->setDelete_img_url(base_url() . 'admin/deleteImage/'); 


//Set url img with Base_url() 
     $this->setPath_url_img_upload_folder(base_url() . "assets/img/articles/"); 
     $this->setPath_url_img_thumb_upload_folder(base_url() . "assets/img/articles/thumbnails/"); 
    } 


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



    public function upload_img() { 
     $name = $_FILES['userfile']['name']; 
     $name = strtr($name, 'ÀÁÂÃÄÅÇÈÉÊËÌÍÎÏÒÓÔÕÖÙÚÛÜÝàáâãäåçèéêëìíîïðòóôõöùúûüýÿ', 'AAAAAACEEEEIIIIOOOOOUUUUYaaaaaaceeeeiiiioooooouuuuyy'); 

// remplacer les caracteres autres que lettres, chiffres et point par _ 

     $name = preg_replace('/([^.a-z0-9]+)/i', '_', $name); 

     //Your upload directory, see CI user guide 
     $config['upload_path'] = $this->getPath_img_upload_folder(); 

     $config['allowed_types'] = 'gif|jpg|png|JPG|GIF|PNG'; 
     $config['max_size'] = '1000'; 
     $config['file_name'] = $name; 

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

     if ($this->do_upload()) { 

      //If you want to resize 
      $config['new_image'] = $this->getPath_img_thumb_upload_folder(); 
      $config['image_library'] = 'gd2'; 
      $config['source_image'] = $this->getPath_img_upload_folder() . $name; 
      $config['create_thumb'] = FALSE; 
      $config['maintain_ratio'] = TRUE; 
      $config['width'] = 193; 
      $config['height'] = 94; 

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

      $this->image_lib->resize(); 

      $data = $this->upload->data(); 

      //Get info 
      $info = new stdClass(); 

      $info->name = $name; 
      $info->size = $data['file_size']; 
      $info->type = $data['file_type']; 
      $info->url = $this->getPath_img_upload_folder() . $name; 
      $info->thumbnail_url = $this->getPath_img_thumb_upload_folder() . $name; //I set this to original file since I did not create thumbs. change to thumbnail directory if you do = $upload_path_url .'/thumbs' .$name 
      $info->delete_url = $this->getDelete_img_url() . $name; 
      $info->delete_type = 'DELETE'; 


      //Return JSON data 
      if (IS_AJAX) { //this is why we put this in the constants to pass only json data 
       echo json_encode(array($info)); 
       //this has to be the only the only data returned or you will get an error. 
       //if you don't give this a json array it will give you a Empty file upload result error 
       //it you set this without the if(IS_AJAX)...else... you get ERROR:TRUE (my experience anyway) 
      } else { // so that this will still work if javascript is not enabled 
       $file_data['upload_data'] = $this->upload->data(); 
       echo json_encode(array($info)); 
      } 
     } else { 

      // the display_errors() function wraps error messages in <p> by default and these html chars don't parse in 
      // default view on the forum so either set them to blank, or decide how you want them to display. null is passed. 
      $error = array('error' => $this->upload->display_errors('','')); 

      echo json_encode(array($error)); 
     } 


     } 
// } 


//Function for the upload : return true/false 
    public function do_upload() { 

     if (!$this->upload->do_upload()) { 

      return false; 
     } else { 
      //$data = array('upload_data' => $this->upload->data()); 

      return true; 
     } 
    } 

public function deleteImage() { 

     //Get the name in the url 
     $file = $this->uri->segment(3); 

     $success = unlink($this->getPath_img_upload_folder() . $file); 
     $success_th = unlink($this->getPath_img_thumb_upload_folder() . $file); 

     //info to see if it is doing what it is supposed to 
     $info = new stdClass(); 
     $info->sucess = $success; 
     $info->path = $this->getPath_url_img_upload_folder() . $file; 
     $info->file = is_file($this->getPath_img_upload_folder() . $file); 
     if (IS_AJAX) {//I don't think it matters if this is set but good for error checking in the console/firebug 
      echo json_encode(array($info)); 
     } else {  //here you will need to decide what you want to show for a successful delete 
      var_dump($file); 
     } 
    } 

    public function get_files() { 

     $this->get_scan_files(); 
    } 

    public function get_scan_files() { 

     $file_name = isset($_REQUEST['file']) ? 
       basename(stripslashes($_REQUEST['file'])) : null; 
     if ($file_name) { 
      $info = $this->get_file_object($file_name); 
     } else { 
      $info = $this->get_file_objects(); 
     } 
     header('Content-type: application/json'); 
     echo json_encode($info); 
    } 

    protected function get_file_object($file_name) { 
     $file_path = $this->getPath_img_upload_folder() . $file_name; 
     if (is_file($file_path) && $file_name[0] !== '.') { 

      $file = new stdClass(); 
      $file->name = $file_name; 
      $file->size = filesize($file_path); 
      $file->url = $this->getPath_url_img_upload_folder() . rawurlencode($file->name); 
      $file->thumbnail_url = $this->getPath_url_img_thumb_upload_folder() . rawurlencode($file->name); 
      //File name in the url to delete 
      $file->delete_url = $this->getDelete_img_url() . rawurlencode($file->name); 
      $file->delete_type = 'DELETE'; 

      return $file; 
     } 
     return null; 
    } 

    protected function get_file_objects() { 
     return array_values(array_filter(array_map(
      array($this, 'get_file_object'), scandir($this->getPath_img_upload_folder()) 
        ))); 
    } 


    public function getPath_img_upload_folder() { 
     return $this->path_img_upload_folder; 
    } 

    public function setPath_img_upload_folder($path_img_upload_folder) { 
     $this->path_img_upload_folder = $path_img_upload_folder; 
    } 

    public function getPath_img_thumb_upload_folder() { 
     return $this->path_img_thumb_upload_folder; 
    } 

    public function setPath_img_thumb_upload_folder($path_img_thumb_upload_folder) { 
     $this->path_img_thumb_upload_folder = $path_img_thumb_upload_folder; 
    } 

    public function getPath_url_img_upload_folder() { 
     return $this->path_url_img_upload_folder; 
    } 

    public function setPath_url_img_upload_folder($path_url_img_upload_folder) { 
     $this->path_url_img_upload_folder = $path_url_img_upload_folder; 
    } 

    public function getPath_url_img_thumb_upload_folder() { 
     return $this->path_url_img_thumb_upload_folder; 
    } 

    public function setPath_url_img_thumb_upload_folder($path_url_img_thumb_upload_folder) { 
     $this->path_url_img_thumb_upload_folder = $path_url_img_thumb_upload_folder; 
    } 

    public function getDelete_img_url() { 
     return $this->delete_img_url; 
    } 

    public function setDelete_img_url($delete_img_url) { 
     $this->delete_img_url = $delete_img_url; 
    } 

任何幫助將是非常可觀的。 謝謝。

+0

如果您正在使用jQuery 1.9+,jQuery對象沒有'live'方法。 – undefined 2013-03-06 16:50:34

+0

是的,我使用1.9,所以我該怎麼辦? – 2013-03-06 16:54:59

回答

1

在jQuery 1.9 jQuery對象不具備live方法,你可以使用on方法來代替:

$('#fileupload').on('click', '.files a:not([target^=_blank])', function (e) { 
// ^        | 
// |         ----- target element 
// ----- A static parent or document object 
+0

是的,它爲我工作。謝謝。 – 2013-03-06 16:57:30

+0

當我嘗試上傳文件在服務器上,但我得到這個「空文件上傳結果」 – 2013-03-06 16:58:06

+0

@ mohsin.mr我已經使用JQuery文件上傳插件,這是您上傳文件時得到的迴應,所以什麼問題是什麼? – undefined 2013-03-06 17:04:42