1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
| #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>
|