使用Dropzone.js上传的示例代码


Posted in Javascript onOctober 10, 2017

本文介绍了使用Dropzone.js上传的示例代码,分享给大家,具体如下:

说明:后台用的python的flask框架,后台对你理解这篇文章没什么影响,你可以使用php

form作为上传区

引入Dropzone.js和dropzone.css然后使用表单form定义一个class=”dropzone”即可完成

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>Flask upload with Dropzone example</title>
  <link rel="stylesheet" href="{{ url_for('static', filename='dropzone.css') }}" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" type="text/css" />
  <script src="{{ url_for('static', filename='jquery.js') }}"></script>
  <script src="{{ url_for('static', filename='dropzone.js') }}"></script>
</head>
<body>
<!-- 第一种上传 -->    
  <form id ="myAwesomeDropzone" action="{{ url_for('upload_file') }}" class="dropzone" method="POST" enctype="multipart/form-data"></form>
<!-- 第一种上传 --> 
</body>
</html>

效果

使用Dropzone.js上传的示例代码 

div作为上传区

div作为上传区也很简单

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>Flask upload with Dropzone example</title>
  <link rel="stylesheet" href="{{ url_for('static', filename='dropzone.css') }}" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" type="text/css" />
  <script src="{{ url_for('static', filename='jquery.js') }}"></script>
  <script src="{{ url_for('static', filename='dropzone.js') }}"></script>
</head>
<body>
<div id="myId" class="dropzone" style="width: 800px; height: 300px;">点我上传</div>
<script type="text/javascript">
    //下面两行是js和jquery的方式实现绑定div的例子,你选择一种即可
    //var myDropzone = new Dropzone("#myId", { url: "{{ url_for('upload_file') }}" });
    $("#myId").dropzone({ url: "{{ url_for('upload_file') }}" });
   </script>
   
</body>
</html>

效果

使用Dropzone.js上传的示例代码 

form作为上传区配置

配置也分为两种,如果使用的form表单上传的就用如下方式配置

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>Flask upload with Dropzone example</title>
  <link rel="stylesheet" href="{{ url_for('static', filename='dropzone.css') }}" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" type="text/css" />
  <script src="{{ url_for('static', filename='jquery.js') }}"></script>
  <script src="{{ url_for('static', filename='dropzone.js') }}"></script>
</head>
<body>
  <form id ="myAwesomeDropzone" action="{{ url_for('upload_file') }}" class="dropzone" method="POST" enctype="multipart/form-data">    
    <div class="fallback">
      <input name="file" type="file" multiple />
    </div> 
  </form>
<script type="text/javascript">
//两种配置方式,第一种,表单上传时的配置方式,可以打开form表单的注释,myAwesomeDropzone是表单的id
  Dropzone.options.myAwesomeDropzone = {
     paramName: "file", // The name that will be used to transfer the file
     maxFilesize: 2, // MB
     accept: function(file, done) {
      if (file.name != "justinbieber.jpg") {
         done("Naha, you don't.");
      }else { 
        done();
      }
    }
   };
</script>
   
</body>
</html>

效果

使用Dropzone.js上传的示例代码 

div作为上传区配置

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>Flask upload with Dropzone example</title>
  <link rel="stylesheet" href="{{ url_for('static', filename='dropzone.css') }}" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" type="text/css" />
  <script src="{{ url_for('static', filename='jquery.js') }}"></script>
  <script src="{{ url_for('static', filename='dropzone.js') }}"></script>
</head>
<body>
 <div id="myId" class="dropzone" style="width: 800px; height: 300px;">点我上传</div>
<script type="text/javascript">
    //第二种配置,这种使用的是div做上传区域时使用的配置
    Dropzone.autoDiscover = false;//不知道该行有什么用,欢迎高手下方评论解答
    $("#myId").dropzone({ 
    url: "{{ url_for('upload_file') }}",
    addRemoveLinks: true,    
    method: 'post',
    filesizeBase: 1024    
    });
</script>
   
</body>
</html>

说明:关于其他的配置请看最后的链接

主题

第一种

<!DOCTYPE html>
<html>
<head>
 <meta charset=="utf-8">
 <!-- Latest compiled and minified CSS -->
 <link rel="stylesheet" href="http://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="external nofollow" >
 <!-- Optional theme -->
 <link rel="stylesheet" href="http://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap-theme.min.css" rel="external nofollow" >
 <script src="{{ url_for('static', filename='jquery.js') }}"></script>
 <script src="{{ url_for('static', filename='dropzone.js') }}"></script>
 <script>
  Dropzone.autoDiscover = false;
 </script>
 <style>
  html, body {
   height: 100%;
  }
  #actions {
   margin: 2em 0;
  }
  /* Mimic table appearance */
  div.table {
   display: table;
  }
  div.table .file-row {
   display: table-row;
  }
  div.table .file-row > div {
   display: table-cell;
   vertical-align: top;
   border-top: 1px solid #ddd;
   padding: 8px;
  }
  div.table .file-row:nth-child(odd) {
   background: #f9f9f9;
  }
  /* The total progress gets shown by event listeners */
  #total-progress {
   opacity: 0;
   transition: opacity 0.3s linear;
  }
  /* Hide the progress bar when finished */
  #previews .file-row.dz-success .progress {
   opacity: 0;
   transition: opacity 0.3s linear;
  }
  /* Hide the delete button initially */
  #previews .file-row .delete {
   display: none;
  }
  /* Hide the start and cancel buttons and show the delete button */
  #previews .file-row.dz-success .start,
  #previews .file-row.dz-success .cancel {
   display: none;
  }
  #previews .file-row.dz-success .delete {
   display: block;
  }
 </style>
</head>
<body>
 
 <div class="container" id="container">
  <h2 class="lead">Configuration Demo</h2>
  <div id="actions" class="row">
   <div class="col-lg-7">
    <!-- 控制总体的三个按钮 -->
    <span class="btn btn-success fileinput-button">
      <i class="glyphicon glyphicon-plus"></i>
      <span>Add files...</span>
    </span>
    <button type="submit" class="btn btn-primary start">
      <i class="glyphicon glyphicon-upload"></i>
      <span>Start upload</span>
    </button>
    <button type="reset" class="btn btn-warning cancel">
      <i class="glyphicon glyphicon-ban-circle"></i>
      <span>Cancel upload</span>
    </button>
   </div>
   <div class="col-lg-5">
    <!-- 总体的进度 -->
    <span class="fileupload-process">
     <div id="total-progress" class="progress progress-striped active" role="progressbar" aria-valuemin="0" aria-valuemax="100" aria-valuenow="0">
      <div class="progress-bar progress-bar-success" style="width:0%;" data-dz-uploadprogress></div>
     </div>
    </span>
   </div>
  </div>
  <!--
  data-dz-thumbnail:使用后代表该标签是存放缩略图的标签【这里必须是一个 <img /> 元素 ,并且alt 和 src 属性将被 Dropzone改变】
  data-dz-name:存放文件名
  data-dz-errormessage:存放错误信息
  data-dz-size:存放文件大小
  data-dz-remove :删除队列中的文件,或者取消正在从队列上传到服务器的文件
  data-dz-uploadprogress:上传进度【( 当这里有一个 uploadprogress事件时, Dropzone 将更改 style.width 属性从 0% 到 100% )】
  -->
  
  <div class="table table-striped files" id="previews">
   <div id="template" class="file-row">
    <div>
      <span class="preview"><img data-dz-thumbnail /></span>
    </div>
    <div>
      <p class="name" data-dz-name ></p>
      <strong class="error text-danger" data-dz-errormessage></strong>
    </div>
    <div>
      <p class="size" data-dz-size></p>
      <div class="progress progress-striped active" role="progressbar" aria-valuemin="0" aria-valuemax="100" aria-valuenow="0">
        <div class="progress-bar progress-bar-success" style="width:0%;" data-dz-uploadprogress></div>
      </div>
    </div>
    <div>
     <button class="btn btn-primary start">
       <i class="glyphicon glyphicon-upload"></i>
       <span>Start</span>
     </button>
     <button data-dz-remove class="btn btn-warning cancel">
       <i class="glyphicon glyphicon-ban-circle"></i>
       <span>Cancel</span>
     </button>
     <button data-dz-remove class="btn btn-danger delete">
      <i class="glyphicon glyphicon-trash"></i>
      <span>Delete</span>
     </button>
    </div>
   </div>
  </div>
<script>
   // Get the template HTML and remove it from the doument
   var previewNode = document.querySelector("#template");
   previewNode.id = "";
   var previewTemplate = previewNode.parentNode.innerHTML;
   //开始先删除单个文件的布局
   previewNode.parentNode.removeChild(previewNode);
   var myDropzone = new Dropzone(document.body, { // 指定拖拽区为body
    url: "{{ url_for('upload_file') }}", // Set the url
    thumbnailWidth: 80,
    thumbnailHeight: 80,
    parallelUploads: 20,
    previewTemplate: previewTemplate,
    autoQueue: false, // 当队列有文件,是否立刻自动上传到服务器
    previewsContainer: "#previews", // 指定存放文件队列区
    clickable: ".fileinput-button" // 点击某个按钮或区域后出现选择电脑中本地图片,默认是previewsContainer指定的区域
   });
   myDropzone.on("addedfile", function(file) {
    // 让模版中的单个文件可以点击上传
    file.previewElement.querySelector(".start").onclick = function() { myDropzone.enqueueFile(file); };
   });
   // 显示所有文件整体上传进度1-100
   myDropzone.on("totaluploadprogress", function(progress) {
    document.querySelector("#total-progress .progress-bar").style.width = progress + "%";
   });
   
   myDropzone.on("sending", function(file) {
    // 显示整体的上传的进度条,说明:原来是0,所以上面的style.width = progress + "%"即使是100%也看不到
    document.querySelector("#total-progress").style.opacity = "1";
    // 失效上传按钮
    file.previewElement.querySelector(".start").setAttribute("disabled", "disabled");
   });
   // 当没有文件上传时,隐藏进度条
   myDropzone.on("queuecomplete", function(progress) {
    document.querySelector("#total-progress").style.opacity = "0";
   });
   // 上传所有
   document.querySelector("#actions .start").onclick = function() {
     myDropzone.enqueueFiles(myDropzone.getAcceptedFiles());
    //myDropzone.enqueueFiles(myDropzone.getFilesWithStatus(Dropzone.ADDED));与上面一样,可查看源码对比
   };
   //取消所有 
   document.querySelector("#actions .cancel").onclick = function() {
    myDropzone.removeAllFiles(true);
   };
</script>
</body>
</html>

第二种效果与默认的一样

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>Flask upload with Dropzone example</title>
  <link rel="stylesheet" href="{{ url_for('static', filename='dropzone.css') }}" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" type="text/css" />
  <script src="{{ url_for('static', filename='jquery.js') }}"></script>
  <script src="{{ url_for('static', filename='dropzone.js') }}"></script>
</head>
<body>
<div id="myId" class="dropzone" style="width: 500px; height: 300px;"></div>
<div id="aaa"></div>
<div id="preview-template" style="display: none;">
  <div class="dz-preview dz-file-preview ">
    <div class="dz-image"><img data-dz-thumbnail /></div>
    <div class="dz-details">
      <div class="dz-filename"><span data-dz-name></span></div>
      <div class="dz-size" data-dz-size></div>
    </div>
    <div class="dz-progress"><span class="dz-upload" data-dz-uploadprogress></span></div>
    <div class="dz-success-mark"><span>✔</span></div>
    <div class="dz-error-mark"><span>✘</span></div>
    <div class="dz-error-message"><span data-dz-errormessage></span></div>
  </div>
</div>
   <script type="text/javascript">
   
    Dropzone.autoDiscover = false;//解决两次实例Dropzone错误,可在控制台看到该错误
    $("#myId").dropzone({ 
    url: "{{ url_for('upload_file') }}",
    addRemoveLinks: true,    
    method: 'post',
    filesizeBase: 1024,
    previewTemplate: $('#preview-template').html(),//如果去掉该选项就会使用默认的
    autoQueue: true,
    init: function() {
        this.on("addedfile", function(file) {
          $(".start").click (function() { 
          this.enqueueFile(file);    
          })    
        });
      }
    }); 
  
   </script>
   
</body>
</html>

demo文件

如果是flask框架可进行测试点击此处下载,如果是php或者其他就看看不必下载

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Javascript 相关文章推荐
js 操作css实现代码
Jun 11 Javascript
javascript 写类方式之九
Jul 05 Javascript
js带按钮的提示框可供选择示例代码
Sep 17 Javascript
Javascript aop(面向切面编程)之around(环绕)分析
May 01 Javascript
JS实现的页面自定义滚动条效果
Oct 26 Javascript
基于js实现的限制文本框只可以输入数字
Dec 05 Javascript
jQuery简易时光轴实现方法示例
Mar 13 Javascript
微信小程序中button组件的边框设置的实例详解
Sep 27 Javascript
浅谈ajax在jquery中的请求和servlet中的响应
Jan 22 jQuery
mpvue+vant app搭建微信小程序的方法步骤
Feb 11 Javascript
bootstrap table.js动态填充单元格数据的多种方法
Jul 18 Javascript
JavaScript简单编程实例学习
Feb 14 Javascript
React中上传图片到七牛的示例代码
Oct 10 #Javascript
基于对象合并功能的实现示例
Oct 10 #Javascript
聊聊Vue.js的template编译的问题
Oct 09 #Javascript
Vue组件之全局组件与局部组件的使用详解
Oct 09 #Javascript
Angular2监听页面大小变化的解决方法
Oct 09 #Javascript
JS实现预加载视频音频/视频获取截图(返回canvas截图)
Oct 09 #Javascript
input 标签实现输入框带提示文字效果(两种方法)
Oct 09 #Javascript
You might like
世界第一个无线广播电台 KDKA
2021/03/01 无线电
PHP 获取指定地区的天气实例代码
2017/02/08 PHP
Laravel框架自定义验证过程实例分析
2019/02/01 PHP
简单JS代码压缩器
2006/10/12 Javascript
打开新窗口关闭当前页面不弹出关闭提示js代码
2013/03/18 Javascript
JS 打印功能代码可实现打印预览、打印设置等
2014/10/31 Javascript
js实现鼠标滚轮控制图片缩放效果的方法
2015/02/20 Javascript
javascript实现获取字符串hash值
2015/05/10 Javascript
jquery注册文本框获取焦点清空,失去焦点赋值的简单实例
2016/09/08 Javascript
js防抖和节流的深入讲解
2018/12/06 Javascript
微信小程序用户拒绝授权的处理方法详解
2019/09/20 Javascript
JavaScript语句错误throw、try及catch实例解析
2020/08/18 Javascript
vue+element实现动态加载表单
2020/12/13 Vue.js
Python实现遍历windows所有窗口并输出窗口标题的方法
2015/03/13 Python
Python实现模拟分割大文件及多线程处理的方法
2017/10/10 Python
python中闭包Closure函数作为返回值的方法示例
2017/12/17 Python
python安装模块如何通过setup.py安装(超简单)
2018/05/05 Python
Python实现的redis分布式锁功能示例
2018/05/29 Python
利用Python如何批量更新服务器文件
2018/07/29 Python
pyqt 实现QlineEdit 输入密码显示成圆点的方法
2019/06/24 Python
wxpython+pymysql实现用户登陆功能
2019/11/19 Python
Python第三方包PrettyTable安装及用法解析
2020/07/08 Python
python正则表达式的懒惰匹配和贪婪匹配说明
2020/07/13 Python
python Selenium 库的使用技巧
2020/10/16 Python
HTML5 CSS3给网站设计带来出色效果
2009/07/16 HTML / CSS
html5关于外链嵌入页面通信问题(postMessage解决跨域通信)
2020/07/20 HTML / CSS
物业客服专员岗位职责
2013/11/30 职场文书
培训主管的职业生涯规划
2014/03/06 职场文书
爽歪歪广告词
2014/03/20 职场文书
私人房屋买卖协议书
2014/10/04 职场文书
2015年工商所工作总结
2015/05/21 职场文书
2015年秋季学校开学标语
2015/07/16 职场文书
PyQt5 QThread倒计时功能的实现代码
2021/04/02 Python
详解MySQL主从复制及读写分离
2021/05/07 MySQL
pd.DataFrame中的几种索引变换的实现
2022/06/16 Python
Win11 25163.1010更新补丁KB5016904推送,测试服务验证管道(附更新修复汇总)
2022/07/23 数码科技