Chinaunix首页 | 论坛 | 博客
  • 博客访问: 268246
  • 博文数量: 59
  • 博客积分: 1368
  • 博客等级: 中尉
  • 技术积分: 1071
  • 用 户 组: 普通用户
  • 注册时间: 2012-02-02 06:06
文章分类

全部博文(59)

文章存档

2012年(59)

我的朋友

分类: 系统运维

2012-04-11 08:48:10

jquery.upload 

控制器

点击(此处)折叠或打开

  1. <?php

  2. class Jupload extends CI_Controller
  3. {
  4.     protected $upload_config = array(
  5.         'upload_path'         => './uploads/images/',
  6.         'allowed_types'     => 'gif|jpg|png|jpeg',
  7.         'max_size'             => '100',
  8.         'max_width'         => '1024',
  9.         'max_height'         => '768'
  10.     );
  11.     protected $resize_config = array(
  12.         'image_library'     => 'gd2',
  13.         'new_image'         => './uploads/thumbs/',
  14.         'create_thumb'         => TRUE,
  15.         'thumb_marker'         => '',
  16.         'maintain_ratio'     => TRUE,
  17.         'width'             => 100,
  18.         'height'             => 100
  19.     );
  20.     
  21.     function __construct()
  22.     {
  23.         parent::__construct();
  24.     }
  25.     
  26.     function index()
  27.     {
  28.         $this->load->view('jupload');
  29.     }
  30.     
  31.     function upload()
  32.     {        
  33.         $this->load->library('upload', $this->upload_config);
  34.         
  35.         if ( ! $this->upload->do_upload('file'))
  36.         {
  37.             $data = array('error' => $this->upload->display_errors('', "\n"));
  38.         }
  39.         else
  40.         {
  41.             $data = $this->upload->data();
  42.             
  43.             $this->resize_config['source_image'] = $data['full_path'];
  44.             $this->load->library('image_lib', $this->resize_config);
  45.             $this->image_lib->resize();
  46.         }

  47.         echo json_encode($data);
  48.     }
  49. }
视图

点击(此处)折叠或打开

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
  2. <html xmlns="" xml:lang="utf-8" lang="utf-8">
  3. <head>
  4. <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
  5. <title>Jquery Upload</title>
  6. <style type="text/css">
  7. </style>
  8. <script type="text/javascript" src="js/jquery-1.7.1.min.js"></script>
  9. <script type="text/javascript" src="js/jquery.upload-1.0.2.js"></script>
  10. <script type="text/javascript">
  11. $(document).ready(function(){
  12.     $('#file1').change(function() {
  13.         $(this).upload('jupload/upload');?>', function(res) {
  14.             if (res.error) {
  15.                 alert(res.error);
  16.             } else {
  17.                 $('#image').attr('src', 'uploads/thumbs/'+res.file_name).show();
  18.             }
  19.         }, 'json');
  20.     });
  21. })
  22. </script>
  23. </head>
  24. <body>
  25. <img id="image" style="display:none;" border="0" />
  26. <br />
  27. <input type="file" name="file" id="file1">
  28. </body>
  29. </html>
end
阅读(2086) | 评论(0) | 转发(0) |
0

上一篇:i18N

下一篇:codeigniter无法上传zip,rar的问题

给主人留下些什么吧!~~