日期:2014-05-17 浏览次数:20976 次
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>HTML5 File Drag and Drop Upload with jQuery and PHP | Tutorialzine Demo</title> <!-- Our CSS stylesheet file --> <link rel="stylesheet" href="assets/css/styles.css" /> <!--[if lt IE 9]> <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script> <![endif]--> </head> <body> <header> <h1>HTML5 File Upload with jQuery and PHP</h1> </header> <div id="dropbox"> <span class="message">Drop images here to upload. <i>(they will only be visible to you)</i></span> </div> <!-- Including The jQuery Library --> <script src="http://code.jquery.com/jquery-1.6.3.min.js"></script> <!-- Including the HTML5 Uploader plugin --> <script src="assets/js/jquery.filedrop.js"></script> <!-- The main script file --> <script src="assets/js/script.js"></script> </body> </html>
<div class="preview done"> <span class="imageHolder"> <img src="" /> <span class="uploaded"></span> </span> <div class="progressHolder"> <div class="progress"></div> </div> </div>
var template = '<div class="preview">'+ '<span class="imageHolder">'+ '<img />'+ '<span class="uploaded"></span>'+ '</span>'+ '<div class="progressHolder">'+ '<div class="progress"></div>'+ '</div>'+ '</div>'; function createImage(file){ var preview = $(template), image = $('img', preview); var reader = new FileReader(); image.width = 100; image.height = 100; reader.onload = function(e){ image.attr('src',e.target.result); }; reader.readAsDataURL(file); message.hide(); preview.appendTo(dropbox); $.data(file,preview); }
$(function(){ var dropbox = $('#dropbox'), message = $('.message', dropbox); dropbox.filedrop({ //用来标识上传文件的数组名 paramname:'pic', //上传文件个数 maxfiles: 5, maxfilesize: 2, // 每个文件最大大小,为2MB url: 'post_file.php', uploadFinished:function(i,file,response){ $.data(file).addClass('done'); }, error: function(err, file) { switch(err) { case 'BrowserNotSupported': showMessage('Your browser does not support HTML5 file uploads!'); break; case 'TooManyFiles': alert('Too many files! Please select 5 at most!'); break; case 'FileTooLarge': alert(file.name+' is too large! Please upload files up to 2mb.'); break; default: break;