返回顶部

收藏

HTML5 实现文件拖放上传

更多

HTML5 实现文件拖放上传

[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>

[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);
    }

    // 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();
})();

标签:javascript,html5,文件上传,拖拽

收藏

0人收藏

支持

0

反对

0

相关聚客文章
  1. admin 发表 2013-12-15 15:40:59 浅谈浏览器的原生拖拽事件
  2. techug 发表 2018-06-30 06:20:30 前端技术展示:一百种时钟
  3. 博主 发表 2014-09-26 00:00:00 获取用户当前地理信息
  4. 屁屁 发表 2012-12-31 06:10:57 我重新认识的移动端Web App
  5. lite3 发表 2013-01-12 23:54:58 使用表单元素和CSS3代替JavaScript
  6. David Walsh 发表 2013-10-31 18:09:15 Meatspaces Chat is Amazing
  7. 歪脖骇客 发表 2014-05-09 16:43:18 使用async属性异步加载执行JavaScript
  8. oldj 发表 2014-05-18 05:15:33 《Web性能实践日志》
  9. sam 发表 2014-11-09 15:22:26 HTML简单布局 – layout.border
  10. iAzrael 发表 2014-01-02 15:31:08 在浏览器端用JS创建和下载文件
  11. w3cui 发表 2014-08-20 02:48:23 JavaScript异步编程的模式
  12. jhjguxin 发表 2012-04-26 02:45:54 16 Impressive CSS3, HTML5 and Javascript Experimen

发表评论