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
| <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>jQuery File Upload Demo - Basic version</title> <meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="/js/bootstrap/css/bootstrap.min.css"> <link rel="stylesheet" href="/css/jquery.fileupload.css"> <link rel="stylesheet" href="/css/dropzone.css">
<style> .fileinput-button { width: 150px; } .template { display: none; } </style> </head>
<body style="padding: 50px"> <span class="btn btn-success fileinput-button"> <i class="glyphicon glyphicon-plus"></i> <span>Select files...</span> <input id="fileupload" type="file" name="files" multiple> </span> <div id="dropzone">Drop files here</div> <hr>
<h4>上传进度</h4> <div id="progresses"> <div class="progress template"> <div class="progress-bar progress-bar-info progress-bar-striped" style="width: 0%">0%</div> </div> </div>
<script src="/js/jquery.min.js"></script> <script src="/js/jquery.ui.widget.js"></script> <script src="/js/jquery.iframe-transport.js"></script> <script src="/js/jquery.fileupload.js"></script> <script src="/js/jquery.fileupload-process.js"></script> <script src="/js/jquery.fileupload-validate.js"></script>
<script> $(function() { var uploadUrl = '/ajax-upload-files';
$('#fileupload').fileupload({ url: uploadUrl, dataType: 'json', maxFileSize: 1000000, acceptFileTypes: /(\.|\/)(gif|jpe?g|png|rar|zip)$/i, dropZone: $('#dropzone'), progressInterval: 10, change: function(e, data) { if(data.files.length > 1){ alert("Max 1 files are allowed") return false; } }, drop: function(e, data) { if(data.files.length > 1){ alert("Max 1 files are allowed") return false; } }, progress: function (e, data) { $.each(data.files, function(index, file) { var progress = parseInt(data.loaded / data.total * 100, 10); updateProgressBar(file.name, progress); }); }, processfail: function(e, data) { alert(data.files[0].error); }, processdone: function (e, data) { $.each(data.files, function (index, file) { createProgressBar(file.name); }); }, done: function(e, data) { $.each(data.result, function(index, file) { }); } });
enableDragAndDropEffect(); });
function createProgressBar(fileName) { var $progress = $('#progresses .template').clone().removeClass('template'); var $progressBar = $progress.find('.progress-bar'); $progress.attr('data-file-name', fileName); $progressBar.text(fileName + ' 0%'); $progress.appendTo($('#progresses')); }
function updateProgressBar(fileName, progress) { var $progress = $('#progresses .progress[data-file-name="' + fileName + '"]'); var $progressBar = $progress.find('.progress-bar'); $progressBar.css('width', progress + '%').text(fileName + ' ' + progress + '%'); }
function enableDragAndDropEffect() { $('#dropzone').on('dragover', function(e){ $('#dropzone').addClass('hover'); }).on('dragleave', function(e) { $('#dropzone').removeClass('hover'); }).on('drop', function() { $('#dropzone').removeClass('hover'); }); } </script> </body> </html>
|