1. [图片] 5375acf5gw1dusqsscfksj.jpg 004 | < meta charset = "utf-8" /> |
005 | < title >OSCTools JsBin在线演示-HTML5 Drag & Drop 多文件上传 from Script Tutorials</ title > |
006 | < script class = "jsbin" src = "http://ajax./ajax/libs/jquery/1/jquery.min.js" ></ script > |
014 | background-color: #212121; |
019 | border-radius:10px 10px 0 0; |
020 | -moz-border-radius:10px 10px 0 0; |
021 | -webkit-border-radius:10px 10px 0 0; |
024 | background: -moz-linear-gradient(#ffffff, #f2f2f2); |
025 | background: -ms-linear-gradient(#ffffff, #f2f2f2); |
026 | background: -webkit-gradient(linear, left top, left bottom, color-stop(0%, #ffffff), color-stop(100%, #f2f2f2)); |
027 | background: -webkit-linear-gradient(#ffffff, #f2f2f2); |
028 | background: -o-linear-gradient(#ffffff, #f2f2f2); |
029 | filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#ffffff', endColorstr='#f2f2f2'); |
030 | -ms-filter: "progid:DXImageTransform.Microsoft.gradient(startColorstr='#ffffff', endColorstr='#f2f2f2')"; |
031 | background: linear-gradient(#ffffff, #f2f2f2); |
037 | background-color: #EEEEEE; |
038 | border: 1px solid #DDDDDD; |
049 | padding: 10px 15px 5px; |
062 | background-color: #DDDDDD; |
063 | border: 3px dashed #000000; |
075 | background-color: #CCCCCC; |
077 | #dropArea.uploading { |
078 | background: #EEEEEE url(loading.gif) center 30% no-repeat; |
080 | #result .s, #result .f { |
086 | -moz-border-radius:10px; |
087 | -webkit-border-radius:10px; |
090 | background-color: #77fc9f; |
093 | background-color: #fcc577; |
099 | < div class = "container" > |
100 | < div class = "contr" >< h2 >< a href = 'http://www./' target = '_blank' style = 'color:white;' >osctools</ a >: 将你的图片拖拽到“Drop区域”(一次最多上传五个, 文件大小小于256kb)</ h2 ></ div > |
101 | < div class = "upload_form_cont" > |
102 | < div id = "dropArea" >文件拖到这里</ div > |
104 | < div >上传文件剩余: < span id = "count" >0</ span ></ div > |
105 | < div >上传目录: < input id = "url" value = "http://www./demos/257/upload.php" /></ div > |
107 | < div id = "result" ></ div > |
108 | < canvas width = "500" height = "20" ></ canvas > |
002 | var dropArea = document.getElementById( 'dropArea' ); |
003 | var canvas = document.querySelector( 'canvas' ); |
004 | var context = canvas.getContext( '2d' ); |
005 | var count = document.getElementById( 'count' ); |
006 | var destinationUrl = document.getElementById( 'url' ); |
007 | var result = document.getElementById( 'result' ); |
010 | var totalProgress = 0; |
016 | function initHandlers() { |
017 | dropArea.addEventListener( 'drop' , handleDrop, false ); |
018 | dropArea.addEventListener( 'dragover' , handleDragOver, false ); |
022 | function drawProgress(progress) { |
023 | context.clearRect(0, 0, canvas.width, canvas.height); |
026 | context.strokeStyle = '#4B9500' ; |
027 | context.fillStyle = '#4B9500' ; |
028 | context.fillRect(0, 0, progress * 500, 20); |
032 | context.font = '16px Verdana' ; |
033 | context.fillStyle = '#000' ; |
034 | context.fillText( '上传进度: ' + Math.floor(progress*100) + '%' , 50, 15); |
038 | function handleDragOver(event) { |
039 | event.stopPropagation(); |
040 | event.preventDefault(); |
042 | dropArea.className = 'hover' ; |
046 | function handleDrop(event) { |
047 | event.stopPropagation(); |
048 | event.preventDefault(); |
050 | processFiles(event.dataTransfer.files); |
054 | function processFiles(filelist) { |
055 | if (!filelist || !filelist.length || list.length) return ; |
059 | result.textContent = '' ; |
061 | for ( var i = 0; i < filelist.length && i < 5; i++) { |
062 | list.push(filelist[i]); |
063 | totalSize += filelist[i].size; |
069 | function handleComplete(size) { |
070 | totalProgress += size; |
071 | drawProgress(totalProgress / totalSize); |
076 | function handleProgress(event) { |
077 | var progress = totalProgress + event.loaded; |
078 | drawProgress(progress / totalSize); |
082 | function uploadFile(file, status) { |
085 | var xhr = new XMLHttpRequest(); |
086 | xhr.open( 'POST' , destinationUrl.value); |
087 | xhr.onload = function () { |
088 | result.innerHTML += this .responseText; |
089 | handleComplete(file.size); |
091 | xhr.onerror = function () { |
092 | result.textContent = this .responseText; |
093 | handleComplete(file.size); |
095 | xhr.upload.onprogress = function (event) { |
096 | handleProgress(event); |
098 | xhr.upload.onloadstart = function (event) { |
102 | var formData = new FormData(); |
103 | formData.append( 'myfile' , file); |
108 | function uploadNext() { |
110 | count.textContent = list.length - 1; |
111 | dropArea.className = 'uploading' ; |
113 | var nextFile = list.shift(); |
114 | if (nextFile.size >= 262144) { |
115 | result.innerHTML += '<div class="f">文件过大 (max filesize exceeded)</div>' ; |
116 | handleComplete(nextFile.size); |
118 | uploadFile(nextFile, status); |
121 | dropArea.className = '' ; |
|