免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3627 | 回复: 0
打印 上一主题 下一主题

HTML5和jquery实现图片拖拉上传 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-09-30 13:47 |只看该作者 |倒序浏览
HTML5和jquery实现图片拖拉上传




今天跟大家一起学习的是,使用HTML5中新提供的文件API,实现从客户端用户的文件夹中,拖拉文件到你的浏览器中,然后实现文件的上传.目前HTML5中只有FIREFOX和Chrome支持这种拖拉上传功能.下面具体代码学习之:

1) 首先本文会用到https://github.com/weixiyen/jquery-filedrop这里的一个
jquery drop的插件,它很方便的帮我们封装了不少东西,先下载之.

2) index.html 代码:
  Java代码
  1. <!DOCTYPE html>
  2. <html>
  3.     <head>
  4.         <meta charset="utf-8" />
  5.         <title>HTML5 File Drag and Drop Upload with jQuery and PHP | Tutorialzine Demo</title>

  6.         <!-- Our CSS stylesheet file -->
  7.         <link rel="stylesheet" href="assets/css/styles.css" />

  8.         <!--[if lt IE 9]>
  9.           <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
  10.         <![endif]-->
  11.     </head>

  12.     <body>

  13.                 <header>
  14.                         <h1>HTML5 File Upload with jQuery and PHP</h1>
  15.                 </header>

  16.                 <div id="dropbox">
  17.                         <span class="message">Drop images here to upload.
  18. <i>(they will only be visible to you)</i></span>
  19.                 </div>

  20.         <!-- Including The jQuery Library -->
  21.                 <script src="http://code.jquery.com/jquery-1.6.3.min.js"></script>

  22.                 <!-- Including the HTML5 Uploader plugin -->
  23.                 <script src="assets/js/jquery.filedrop.js"></script>

  24.                 <!-- The main script file -->
  25.         <script src="assets/js/script.js"></script>

  26.     </body>
  27. </html>
复制代码
其中,引入了jquery库和jquery.filedrop.js这个库,而dropbox这个DIV
是用来做拖放文件用的区域.
  接下来,再放置一个区域,是显示拖放图片后,对图片进行预览,并显示上传进度条的div,如下:
  Java代码


  1. <div class="preview done">

  2.         <span class="imageHolder">
  3.                 <img src="" />
  4.                 <span class="uploaded"></span>
  5.         </span>

  6.         <div class="progressHolder">
  7.                 <div class="progress"></div>
  8.         </div>

  9. </div>
复制代码
由于jquey filedrop这个插件已经帮我们做了不少东西了,所以我们只需要利用这个插件就可以了,其中asset/js/script.js代码如下:
  Java代码

  1. var template = '<div class="preview">'+
  2.                                                 '<span class="imageHolder">'+
  3.                                                         '<img />'+
  4.                                                         '<span class="uploaded"></span>'+
  5.                                                 '</span>'+
  6.                                                 '<div class="progressHolder">'+
  7.                                                         '<div class="progress"></div>'+
  8.                                                 '</div>'+
  9.                                         '</div>';

  10.         function createImage(file){

  11.                 var preview = $(template),
  12.                         image = $('img', preview);

  13.                 var reader = new FileReader();

  14.                 image.width = 100;
  15.                 image.height = 100;

  16.                 reader.onload = function(e){

  17.                        
  18.                         image.attr('src',e.target.result);
  19.                 };

  20.                
  21.                 reader.readAsDataURL(file);

  22.                 message.hide();
  23.                 preview.appendTo(dropbox);

  24.                
  25.                 $.data(file,preview);
  26.         }


复制代码
稍微解析下,这里var reader = new FileReader();  

  是调用了HTML5的API,然后当用户从客户端的文件夹中拖拉了一张图片到拖放区域后,
图片实际上以dataurl形式保存(实际上是BASE64编码),
    reader.readAsDataURL(file);  
   这里执行后,就会触发reader.onload 事件,这里就将img的src属性设置为图片的实际内容了,即显示一张100*100的图片预览图了.
  接下来,当每张图片在dropbox区域中被显示后,这个时候可以触发file drop这个插件的上传功能了,接下来看这个插件的功能代码:
  Java代码

  1.   $(function(){

  2.         var dropbox = $('#dropbox'),
  3.                 message = $('.message', dropbox);

  4.         dropbox.filedrop({
  5.                 //用来标识上传文件的数组名
  6.                 paramname:'pic',
  7.                
  8.                   //上传文件个数
  9.                 maxfiles: 5,
  10.             maxfilesize: 2, // 每个文件最大大小,为2MB
  11.                 url: 'post_file.php',

  12.                 uploadFinished:function(i,file,response){
  13.                         $.data(file).addClass('done');
  14.                                         },

  15.             error: function(err, file) {
  16.                         switch(err) {
  17.                                 case 'BrowserNotSupported':
  18.                                         showMessage('Your browser does not support HTML5 file uploads!');
  19.                                         break;
  20.                                 case 'TooManyFiles':
  21.                                         alert('Too many files! Please select 5 at most!');
  22.                                         break;
  23.                                 case 'FileTooLarge':
  24.                                         alert(file.name+' is too large! Please upload files up to 2mb.');
  25.                                         break;
  26.                                 default:
  27.                                         break;
  28.                         }
  29.                 },

  30.                 //判断每个上传的图片是否文件格式
  31.                 beforeEach: function(file){
  32.                         if(!file.type.match(/^image\//)){
  33.                                 alert('Only images are allowed!');

  34.                                                                 return false;
  35.                         }
  36.                 },

  37.                 uploadStarted:function(i, file, len){
  38.                         createImage(file);
  39.                 },

  40.                 progressUpdated: function(i, file, progress) {
  41.                         $.data(file).find('.progress').width(progress);
  42.                 }

  43.         });

  44.         var template = '...';

  45.         function createImage(file){
  46.                 // ... see above ...
  47.         }

  48.         function showMessage(msg){
  49.                 message.html(msg);
  50.         }

  51. });
复制代码
重点关注的方法为uploadStarted和progressUpdated.

而PHP部分接收文件的话,可以这样:
  Java代码
  1. if(array_key_exists('pic',$_FILES) && $_FILES['pic']['error'] == 0 ){     
  2.   
  3.         $pic = $_FILES['pic'];     
  4.   
  5. }   
  6. //然后自己调用PHP代码去管理上传  
复制代码
CSS代码为:
Java代码

  1. /*-------------------------  
  2.     Dropbox Element  
  3. --------------------------*/  
  4.   
  5. #dropbox{   
  6.     background:url('../img/background_tile_3.jpg');   
  7.   
  8.     border-radius:3px;   
  9.     position: relative;   
  10.     margin:80px auto 90px;   
  11.     min-height: 290px;   
  12.     overflow: hidden;   
  13.     padding-bottom: 40px;   
  14.     width: 990px;   
  15.   
  16.     box-shadow:0 0 4px rgba(0,0,0,0.3) inset,0 -3px 2px rgba(0,0,0,0.1);   
  17. }   
  18.   
  19. #dropbox .message{   
  20.     font-size: 11px;   
  21.     text-align: center;   
  22.     padding-top:160px;   
  23.     display: block;   
  24. }   
  25.   
  26. #dropbox .message i{   
  27.     color:#ccc;   
  28.     font-size:10px;   
  29. }   
  30.   
  31. #dropbox:before{   
  32.     border-radius:3px 3px 0 0;   
  33. }   
  34.   
  35. /*-------------------------  
  36.     Image Previews  
  37. --------------------------*/  
  38.   
  39. #dropbox .preview{   
  40.     width:245px;   
  41.     height: 215px;   
  42.     float:left;   
  43.     margin: 55px 0 0 60px;   
  44.     position: relative;   
  45.     text-align: center;   
  46. }   
  47.   
  48. #dropbox .preview img{   
  49.     max-width: 240px;   
  50.     max-height:180px;   
  51.     border:3px solid #fff;   
  52.     display: block;   
  53.   
  54.     box-shadow:0 0 2px #000;   
  55. }   
  56.   
  57. #dropbox .imageHolder{   
  58.     display: inline-block;   
  59.     position:relative;   
  60. }   
  61.   
  62. #dropbox .uploaded{   
  63.     position: absolute;   
  64.     top:0;   
  65.     left:0;   
  66.     height:100%;   
  67.     width:100%;   
  68.     background: url('../img/done.png') no-repeat center center rgba(255,255,255,0.5);   
  69.     display: none;   
  70. }   
  71.   
  72. #dropbox .preview.done .uploaded{   
  73.     display: block;   
  74. }   
  75.   
  76. /*-------------------------  
  77.     Progress Bars  
  78. --------------------------*/  
  79.   
  80. #dropbox .progressHolder{   
  81.     position: absolute;   
  82.     background-color:#252f38;   
  83.     height:12px;   
  84.     width:100%;   
  85.     left:0;   
  86.     bottom: 0;   
  87.   
  88.     box-shadow:0 0 2px #000;   
  89. }   
  90.   
  91. #dropbox .progress{   
  92.     background-color:#2586d0;   
  93.     position: absolute;   
  94.     height:100%;   
  95.     left:0;   
  96.     width:0;   
  97.   
  98.     box-shadow: 0 0 1px rgba(255, 255, 255, 0.4) inset;   
  99.   
  100.     -moz-transition:0.25s;   
  101.     -webkit-transition:0.25s;   
  102.     -o-transition:0.25s;   
  103.     transition:0.25s;   
  104. }   
  105.   
  106. #dropbox .preview.done .progress{   
  107.     width:100% !important;   
  108. }  


复制代码
整个运行的DEMO为:
http://demo.tutorialzine.com/201 ... -upload-jquery-php/

代码下载地址为:
  http://demo.tutorialzine.com/201 ... ml5-file-upload.zip
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP