1. [图片] 5375acf5gw1dusqsscfksj.jpg


​2. [代码][HTML]代码

<!DOCTYPE html>
<html lang="en" >
    <head>
        <meta charset="utf-8" />
        <title>OSCTools JsBin在线演示-HTML5 Drag & Drop 多文件上传 from Script Tutorials</title>
        <script class="jsbin" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
      <style>
        .container {
            overflow:hidden;
            width:960px;
            margin:20px auto;
        }
        .contr {
            background-color: #212121;
            color: #FFFFFF;
            padding: 10px 0;
            text-align: center;
         
            border-radius:10px 10px 0 0;
            -moz-border-radius:10px 10px 0 0;
            -webkit-border-radius:10px 10px 0 0;
        }
        .upload_form_cont {
            background: -moz-linear-gradient(#ffffff, #f2f2f2);
            background: -ms-linear-gradient(#ffffff, #f2f2f2);
            background: -webkit-gradient(linear, left top, left bottom, color-stop(0%, #ffffff), color-stop(100%, #f2f2f2));
            background: -webkit-linear-gradient(#ffffff, #f2f2f2);
            background: -o-linear-gradient(#ffffff, #f2f2f2);
            filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#ffffff', endColorstr='#f2f2f2');
            -ms-filter: "progid:DXImageTransform.Microsoft.gradient(startColorstr='#ffffff', endColorstr='#f2f2f2')";
            background: linear-gradient(#ffffff, #f2f2f2);
         
            color: #000;
            overflow: hidden;
        }
        .info {
            background-color: #EEEEEE;
            border: 1px solid #DDDDDD;
            float: left;
            font-weight: bold;
            height: 530px;
            margin: 20px;
            position: relative;
            width: 560px;
        }
        .info > div {
            font-size: 14px;
            font-weight: bold;
            padding: 10px 15px 5px;
        }
        .info > h2 {
            padding: 0 15px;
        }
        .info > canvas {
            margin-left: 15px;
            margin-bottom: 10px;
        }
        .info #url {
            width: 400px;
        }
        #dropArea {
            background-color: #DDDDDD;
            border: 3px dashed #000000;
            float: left;
            font-size: 48px;
            font-weight: bold;
            height: 530px;
            line-height: 530px;
            margin: 20px;
            position: relative;
            text-align: center;
            width: 300px;
        }
        #dropArea.hover {
            background-color: #CCCCCC;
        }
        #dropArea.uploading {
            background: #EEEEEE url(loading.gif) center 30% no-repeat;
        }
        #result .s, #result .f {
            font-size: 12px;
            margin-bottom: 10px;
            padding: 10px;
         
            border-radius:10px;
            -moz-border-radius:10px;
            -webkit-border-radius:10px;
        }
        #result .s {
            background-color: #77fc9f;
        }
        #result .f {
            background-color: #fcc577;
        }
 
      </style>
  </head>
    <body>
    <div class="container">
      <div class="contr"><h2><a href='http://www.osctools.net/' target='_blank' style='color:white;'>osctools</a>: 将你的图片拖拽到“Drop区域”(一次最多上传五个, 文件大小小于256kb)</h2></div>
        <div class="upload_form_cont">
            <div id="dropArea">文件拖到这里</div>
            <div class="info">
                <div>上传文件剩余: <span id="count">0</span></div>
                <div>上传目录: <input id="url" value="http://www.script-tutorials.com/demos/257/upload.php"/></div>
                <h2>结果:</h2>
                <div id="result"></div>
                <canvas width="500" height="20"></canvas>
            </div>
        </div>
    </div>
    </body>
</html>
3. [代码][JavaScript]代码    
// variables
var dropArea = document.getElementById('dropArea');
var canvas = document.querySelector('canvas');
var context = canvas.getContext('2d');
var count = document.getElementById('count');
var destinationUrl = document.getElementById('url');
var result = document.getElementById('result');
var list = [];
var totalSize = 0;
var totalProgress = 0;
 
// main initialization
(function(){
 
    // init handlers
    function initHandlers() {
        dropArea.addEventListener('drop', handleDrop, false);
        dropArea.addEventListener('dragover', handleDragOver, false);
    }
 
    // draw progress
    function drawProgress(progress) {
        context.clearRect(0, 0, canvas.width, canvas.height); // clear context
 
        context.beginPath();
        context.strokeStyle = '#4B9500';
        context.fillStyle = '#4B9500';
        context.fillRect(0, 0, progress * 500, 20);
        context.closePath();
 
        // draw progress (as text)
        context.font = '16px Verdana';
        context.fillStyle = '#000';
        context.fillText('上传进度: ' + Math.floor(progress*100) + '%', 50, 15);
    }http://www.huiyi8.com/huawen/​
 
    // drag over花纹
    function handleDragOver(event) {
        event.stopPropagation();
        event.preventDefault();
 
        dropArea.className = 'hover';
    }
 
    // drag drop
    function handleDrop(event) {
        event.stopPropagation();
        event.preventDefault();
 
        processFiles(event.dataTransfer.files);
    }
 
    // process bunch of files
    function processFiles(filelist) {
        if (!filelist || !filelist.length || list.length) return;
 
        totalSize = 0;
        totalProgress = 0;
        result.textContent = '';
 
        for (var i = 0; i < filelist.length && i < 5; i++) {
            list.push(filelist[i]);
            totalSize += filelist[i].size;
        }
        uploadNext();
    }
 
    // on complete - start next file
    function handleComplete(size) {
        totalProgress += size;
        drawProgress(totalProgress / totalSize);
        uploadNext();
    }
 
    // update progress
    function handleProgress(event) {
        var progress = totalProgress + event.loaded;
        drawProgress(progress / totalSize);
    }
 
    // upload file
    function uploadFile(file, status) {
 
        // prepare XMLHttpRequest
        var xhr = new XMLHttpRequest();
        xhr.open('POST', destinationUrl.value);
        xhr.onload = function() {
            result.innerHTML += this.responseText;
            handleComplete(file.size);
        };
        xhr.onerror = function() {
            result.textContent = this.responseText;
            handleComplete(file.size);
        };
        xhr.upload.onprogress = function(event) {
            handleProgress(event);
        }
        xhr.upload.onloadstart = function(event) {
        }
 
        // prepare FormData
        var formData = new FormData();
        formData.append('myfile', file);
        xhr.send(formData);
    }
 
    // upload next file
    function uploadNext() {
        if (list.length) {
            count.textContent = list.length - 1;
            dropArea.className = 'uploading';
 
            var nextFile = list.shift();
            if (nextFile.size >= 262144) { // 256kb
                result.innerHTML += '<div class="f">文件过大 (max filesize exceeded)</div>';
                handleComplete(nextFile.size);
            } else {
                uploadFile(nextFile, status);
            }
        } else {
            dropArea.className = '';
        }
    }
 
    initHandlers();
})();

HTML5 实现文件拖放上传的更多相关文章

  1. 赞!带进度条的 jQuery 文件拖放上传插件

    jQuery File Uploader 是一个 jQuery 文件拖放上传插件,包括 Ajax 上传和进度条效果.作者编写这个插件的想法是要保持它非常简单,不像其他的插件,很多的标记,并提供一些 H ...

  2. 带进度条的 jQuery 文件拖放上传插件

    jQuery File Uploader :jQuery File Uploader 是一个 jQuery 文件拖放上传插件 兼容性判断 下载:https://github.com/danielm/u ...

  3. html5 实现 文件夹上传

    先插个背景:最近所在项目有个小需求,就是上传文件要可以同时选择文件夹及文件,然后把文件夹内得文件及所选单文件全部选择上传,借助于搜索关键词没搜到想要的结果(相关文章貌似很好,要么就是遍历文件夹内的文件 ...

  4. 【方法】Html5实现文件异步上传

    1 简介 开发文件上传功能从来不是一件愉快的事,异步上传更是如此,使用过iframe和Flash的上传方案,也都感觉十分的别扭.本文简要简绍利用Html5的FormData实现文件的异步上传,还可以实 ...

  5. 【实用】Html5实现文件异步上传

    1 简介 开发文件上传功能从来不是一件愉快的事,异步上传更是如此,使用过iframe和Flash的上传方案,也都感觉十分的别扭.本文简要简绍利用Html5的FormData实现文件的异步上传,还可以实 ...

  6. HTML实现文件拖动上传

    在大型企业的开发过程中,很多比较有趣而实际的功能往往都是让大家望而却步,我给大家带来一个百度云盘和360云盘的HTML5多文件拖动上传技术: 1:记得导入:common-fileupload.jar包 ...

  7. js分割文件快速上传

    <?php header('Content-type:text/html;charset=UTF-8'); ?> <?php if ($_FILES) { if(!file_exis ...

  8. 利用Jquery使用HTML5的FormData属性实现对文件的上传

    1.利用Jquery使用HTML5的FormData属性实现对文件的上传 在HTML5以前我们如果需要实现文件上传服务器等功能的时候,有时候我们不得不依赖于FLASH去实现,而在HTML5到来之后,我 ...

  9. HTML5+J2EE实现文件异步上传

    P.S. HTML5经过了W3C的8年努力,终于正式推广了.这次升级最大的就是升级了XMLHTTPRequest,让它变成了XMLHTTPRequest Level II(这有啥奇怪的?).这个对象现 ...

随机推荐

  1. DNA的复制

    半保留复制 DNA分子复制时, DNA分子的双螺旋将解开, 互补的碱基之间的氢键断裂, 解开的两条单链作为复制的模板, 游离的脱氧核苷酸依据碱基互补配对的原则, 通过形成氢键结合到作为模板的单链上. ...

  2. 在C#的数据类型中,什么属于值类型,什么属于引用类型

    转自原文 在C#的数据类型中,什么属于值类型,什么属于引用类型 类型:整数,浮点数,高精度浮点数,布尔,字符,结构,枚举引用类型:对象(Object),字符串,类,接口,委托,数组除了值类型和引用类型 ...

  3. 产品如何进行大屏数据可视化.md

    最近接到一个需求,需要给公司的竞赛平台面对省/校/竞赛进行大屏的可视化话数据展示,闲暇之余对自己最近的工作进行一些总结; 一.数据可视化的定义 数据可视化主要是关于数据_视觉表现形式的科学技术研究 - ...

  4. android studio 在线更新android sdk,遇到无法Fetching https://dl-ssl.google.com/...的解决方案

    最近实在受不了eclipse的“迟钝”,准备入手Android studio开发环境,但是貌似不太顺利,成功安装了Android studio,在线更新Android adk的时候,总是遇到如下错误: ...

  5. GridView数据绑定控件的模版列时设置显示的格式

    形式 语法 结果 数字 {0:N2} 12.36   数字 {0:N0} 13   货币 {0:c2} $12.36   货币 {0:c4} $12.3656   货币 "¥{0:N2}&q ...

  6. 快讯 | FireEye在GitHub上开源密码破解工具GoCrack

    近日,FireEye 开源了一款密码破解工具 GoCrack,可在多机器上部署破解任务. GoCrack 是由 FireEye’s Innovation and Custom Engineering ...

  7. HDU 5304(Eastest Magical Day Seep Group&#39;s Summer-环加外向树生成树计数)[Template:Kirchhoff矩阵]

    Eastest Magical Day Seep Group's Summer Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 655 ...

  8. 猫猫学iOS之小知识之_xcode插件的删除方法_自己主动提示图片插件KSImageNamed有时不灵_分类或宏之类不能自己主动提示,

    猫猫分享,必须精品 原创文章.欢迎转载. 转载请注明:翟乃玉的博客 地址:http://blog.csdn.net/u013357243 一:解决解决自己主动提示图片插件KSImageNamed有时不 ...

  9. sed系列:行或者模式匹配删除特定行

    “p” command prints the buffer (remember to use -n option with “p”) “d” command is just opposite, its ...

  10. vuex 介绍

    vuex是为vue.js开发的状态管理模式,负责vue的状态管理,状态管理是干啥的呢,举个栗子,比如一个酒店,哪间屋子入住了客人,哪间屋子客人退房了,客人退房后,房间有没有清扫过,这些都需要去记录,以 ...