dropzone的原理是模擬表單來上傳文件,html中的元素有多重形式。php
<form id="dropz" action="/upload.php" enctype="multipart/form-data"> <input type="file" name="file"> </form>
<div id="dropz"></div>
引入dropzone.min.css以後會有更漂亮的外觀;css
而後能夠本身添加些外觀樣式覆蓋它,如:html
<style> #filedropzone{ width: 900px; height: 220px; margin-left: 200px; margin-top: 100px; border: 3px dashed green; border-radius: 2%; box-shadow: 3px 3px 5px #888888; } </style>
必須配置js才能上傳
1.若是沒有引入jquery: python
var myDropzone = new Dropzone("div#mydropzone", {url: "/upload"});
2.若是引入了jquery:jquery
$("#dropz").dropzone({url: "/upload"})
<input>
元素的name屬性,默認爲file。$("#dropz").dropzone({ init: function() { this.on("addedfile", function(file) { // actions... }); } });
沒有添加jquery時:ajax
dropz.on("addedfile", function(file) { // actions... });
html文件demodjango
<link rel="stylesheet" href="/static/plugins/dropzone/dropzone.css"> <div id="upload_div"> <p>請上傳您的代碼(如包含文件夾須要打包後再上傳)</p> <form id="filedropzone" method="post" action="/upload" class="dropzone dz-clickable" > <div class="dz-default dz-message"> <div class="dz-icon icon-wrap icon-circle icon-wrap-md"> <i class="fa fa-cloud-upload fa-3x"></i> </div> <div> <p class="dz-text">把打包後的代碼拖放到這裏</p> <p class="text-muted">最多可上傳2張照片</p> </div> </div> </form> </div> <script src="/static/plugins/dropzone/dropzone.js"></script> <script> $(document).ready(function () { Dropzone.options.filedropzone = { url:"{{ request.path }}", paramName: "file", // The name that will be used to transfer the file maxFilesize: 1, // MB, addRemoveLinks:true, maxFiles:5, uploadMultiple:true, accept: function(file, done) { if (! file.name.endsWith(".zip") ) { alert("只能上傳.zip格式的壓縮包") done("文件爲上傳") myDropzone.removeFile(file); } else { done(); } } }; Dropzone.autoDiscover = false; myDropzone = new Dropzone("#filedropzone"); myDropzone.on("addedfile", function(file) { /* Maybe display some more file information on your page */ }); myDropzone.on("success", function(file,response) { /* Maybe display some more file information on your page */ console.log('filex upload done...', response); } ) }) </script>
$("#dropz").dropzone({ url: "/files/uploading", maxFiles: 1, maxFilesize: 1024, acceptedFiles: ".jpg,.jpeg,.doc,.docx,.ppt,.pptx,.txt,.avi,.pdf,.mp3,.zip", autoProcessQueue: false, paramName: "file", dictDefaultMessage: "拖入須要上傳的文件", init: function () { var myDropzone = this, submitButton = document.querySelector("#qr"), cancelButton = document.querySelector("#cancel"); myDropzone.on('addedfile', function (file) { //添加上傳文件的過程,可再次彈出彈框,添加上傳文件的信息 }); myDropzone.on('sending', function (data, xhr, formData) { //向後臺發送該文件的參數 formData.append('watermark', jQuery('#info').val()); }); myDropzone.on('success', function (files, response) { //文件上傳成功以後的操做 }); myDropzone.on('error', function (files, response) { //文件上傳失敗後的操做 }); myDropzone.on('totaluploadprogress', function (progress, byte, bytes) { //progress爲進度百分比 $("#pro").text("上傳進度:" + parseInt(progress) + "%"); //計算上傳速度和剩餘時間 var mm = 0; var byte = 0; var tt = setInterval(function () { mm++; var byte2 = bytes; var remain; var speed; var byteKb = byte/1024; var bytesKb = bytes/1024; var byteMb = byte/1024/1024; var bytesMb = bytes/1024/1024; if(byteKb <= 1024){ speed = (parseFloat(byte2 - byte)/(1024)/mm).toFixed(2) + " KB/s"; remain = (byteKb - bytesKb)/parseFloat(speed); }else{ speed = (parseFloat(byte2 - byte)/(1024*1024)/mm).toFixed(2) + " MB/s"; remain = (byteMb - bytesMb)/parseFloat(speed); } $("#dropz #speed").text("上傳速率:" + speed); $("#dropz #time").text("剩餘時間"+arrive_timer_format(parseInt(remain))); if(bytes >= byte){ clearInterval(tt); if(byteKb <= 1024){ $("#dropz #speed").text("上傳速率:0 KB/s"); }else{ $("#dropz #speed").text("上傳速率:0 MB/s"); } $("#dropz #time").text("剩餘時間:0:00:00"); } },1000); }); submitButton.addEventListener('click', function () { //點擊上傳文件 myDropzone.processQueue(); }); cancelButton.addEventListener('click', function () { //取消上傳 myDropzone.removeAllFiles(); }); } }); //剩餘時間格式轉換: function arrive_timer_format(s) { var t; if(s > -1){ var hour = Math.floor(s/3600); var min = Math.floor(s/60) % 60; var sec = s % 60; var day = parseInt(hour / 24); if (day > 0) { hour = hour - 24 * day; t = day + "day " + hour + ":"; } else t = hour + ":"; if(min < 10){t += "0";} t += min + ":"; if(sec < 10){t += "0";} t += sec; } return t; }
#urls.py from app01 import views urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^test/', views.dropzoneTest), ] #views.py from django.shortcuts import render def dropzoneTest(request): if request.is_ajax(): file = request.FILES.get('file') with open('file.jpg','wb') as f: for line in file: f.write(line) return render(request,'dropzoneTest.html') #dropzoneDemo.html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> {% load static %} <link rel="stylesheet" href="{% static 'dropzone.css' %}"> <script src="{% static 'jquery-3.2.1.min.js' %}"></script> <script src="{% static 'dropzone.js' %}"></script> </head> <body> <p>請上傳身份照正反面照片</p> <form id="filedropzone" method="post" action="{{ request.path }}" class="dropzone dz-clickable" >{% csrf_token %} <div class="dz-default dz-message"> <div class="dz-icon icon-wrap icon-circle icon-wrap-md"> <i class="fa fa-cloud-upload fa-3x"></i> </div> <div> <p class="dz-text">把證件信息拖放到這裏</p> <p class="text-muted">最多可上傳2張照片</p> </div> </div> </form> <!---------------------------------------------------------------> <script> $(document).ready(function () { $("#filedropzone").dropzone({ url: "{{ request.path }}", maxFiles: 5, maxFilesize: 1024, acceptedFiles: ".jpg,.jpeg,.doc,.docx,.ppt,.pptx,.txt,.avi,.pdf,.mp3,.zip", autoProcessQueue: false, paramName: "file", dictDefaultMessage: "拖入須要上傳的文件", init: function () { var myDropzone = this, submitButton = document.querySelector("#qr"), cancelButton = document.querySelector("#cancel"); myDropzone.on('addedfile', function (file) { //添加上傳文件的過程,可再次彈出彈框,添加上傳文件的信息 }); myDropzone.on('sending', function (data, xhr, formData) { //向後臺發送該文件的參數 formData.append('watermark', jQuery('#info').val()); }); myDropzone.on('success', function (files, response) { //文件上傳成功以後的操做 }); myDropzone.on('error', function (files, response) { //文件上傳失敗後的操做 }); myDropzone.on('totaluploadprogress', function (progress, byte, bytes) { //progress爲進度百分比 $("#pro").text("上傳進度:" + parseInt(progress) + "%"); //計算上傳速度和剩餘時間 var mm = 0; var byte = 0; var tt = setInterval(function () { mm++; var byte2 = bytes; var remain; var speed; var byteKb = byte / 1024; var bytesKb = bytes / 1024; var byteMb = byte / 1024 / 1024; var bytesMb = bytes / 1024 / 1024; if (byteKb <= 1024) { speed = (parseFloat(byte2 - byte) / (1024) / mm).toFixed(2) + " KB/s"; remain = (byteKb - bytesKb) / parseFloat(speed); } else { speed = (parseFloat(byte2 - byte) / (1024 * 1024) / mm).toFixed(2) + " MB/s"; remain = (byteMb - bytesMb) / parseFloat(speed); } $("#dropz #speed").text("上傳速率:" + speed); $("#dropz #time").text("剩餘時間" + arrive_timer_format(parseInt(remain))); if (bytes >= byte) { clearInterval(tt); if (byteKb <= 1024) { $("#dropz #speed").text("上傳速率:0 KB/s"); } else { $("#dropz #speed").text("上傳速率:0 MB/s"); } $("#dropz #time").text("剩餘時間:0:00:00"); } }, 1000); }); submitButton.addEventListener('click', function () { //點擊上傳文件 myDropzone.processQueue(); }); cancelButton.addEventListener('click', function () { //取消上傳 myDropzone.removeAllFiles(); }); } }); //剩餘時間格式轉換: function arrive_timer_format(s) { var t; if (s > -1) { var hour = Math.floor(s / 3600); var min = Math.floor(s / 60) % 60; var sec = s % 60; var day = parseInt(hour / 24); if (day > 0) { hour = hour - 24 * day; t = day + "day " + hour + ":"; } else t = hour + ":"; if (min < 10) { t += "0"; } t += min + ":"; if (sec < 10) { t += "0"; } t += sec; } return t; } }) </script> </body> </html>