ayand04

Multifile uploader

May 26th, 2016
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.95 KB | None | 0 0
  1. HTML
  2.  
  3. <!DOCTYPE html>
  4. <html>
  5.  
  6. <head>
  7. <meta charset="utf-8"/>
  8. <title>File Uploader</title>
  9.  
  10. <!-- Google web fonts -->
  11. <link href="http://fonts.googleapis.com/css?family=PT+Sans+Narrow:400,700" rel='stylesheet' />
  12.  
  13. <!-- The main CSS file -->
  14. <link href="assets/css/style.css" rel="stylesheet" />
  15. </head>
  16.  
  17. <body>
  18.  
  19. <form id="upload" method="post" action="upload.php" enctype="multipart/form-data">
  20. <div id="drop">
  21. Drop Here
  22.  
  23. <a>Browse</a>
  24. <input type="file" name="upl[]" multiple />
  25. </div>
  26.  
  27. <ul>
  28. <!-- The file uploads will be shown here -->
  29. </ul>
  30.  
  31. </form>
  32.  
  33. <!-- JavaScript Includes -->
  34. <script src="assets/js/jquery-1.11.3.min.js"></script>
  35. <script src="assets/js/jquery.knob.js"></script>
  36.  
  37. <!-- jQuery File Upload Dependencies -->
  38. <script src="assets/js/jquery.ui.widget.js"></script>
  39. <script src="assets/js/jquery.iframe-transport.js"></script>
  40. <script src="assets/js/jquery.fileupload.js"></script>
  41.  
  42. <!-- Our main JS file -->
  43. <script src="assets/js/script.js"></script>
  44.  
  45.  
  46. </body>
  47. </html>
  48.  
  49. script.js
  50.  
  51. $(function(){
  52.  
  53. var ul = $('#upload ul');
  54. var myfiles = [];
  55. $('#drop a').click(function(){
  56. // Simulate a click on the file input button
  57. // to show the file browser dialog
  58. $(this).parent().find('input').click();
  59. });
  60.  
  61. // Initialize the jQuery File Upload plugin
  62. $('#upload').fileupload({
  63.  
  64. // This element will accept file drag/drop uploading
  65. dropZone: $('#drop'),
  66.  
  67. // This function is called when a file is added to the queue;
  68. // either via the browse button, or via drag/drop:
  69. add: function (e, data) {
  70.  
  71. var tpl = $('<li class="working"><input type="text" value="0" data-width="48" data-height="48"'+
  72. ' data-fgColor="#0788a5" data-readOnly="1" data-bgColor="#3e4043" /><p></p><span></span></li>');
  73.  
  74. // Append the file name and file size
  75. tpl.find('p').text(data.files[0].name)
  76. .append('<i>' + formatFileSize(data.files[0].size) + '</i>');
  77.  
  78. // Add the HTML to the UL element
  79. data.context = tpl.appendTo(ul);
  80.  
  81. // Initialize the knob plugin
  82. tpl.find('input').knob();
  83.  
  84. // Listen for clicks on the cancel icon
  85. tpl.find('span').click(function(){
  86.  
  87. if(tpl.hasClass('working')){
  88. jqXHR.abort();
  89. }
  90.  
  91. tpl.fadeOut(function(){
  92. tpl.remove();
  93. });
  94.  
  95. });
  96.  
  97. // Automatically upload the file once it is added to the queue
  98. var jqXHR = data.submit();
  99. },
  100.  
  101. progress: function(e, data){
  102.  
  103. // Calculate the completion percentage of the upload
  104. var progress = parseInt(data.loaded / data.total * 100, 10);
  105.  
  106. // Update the hidden input field and trigger a change
  107. // so that the jQuery knob plugin knows to update the dial
  108. data.context.find('input').val(progress).change();
  109.  
  110. if(progress == 100){
  111. data.context.removeClass('working');
  112. }
  113. },
  114.  
  115. fail:function(e, data){
  116. // Something has gone wrong!
  117. data.context.addClass('error');
  118. }
  119.  
  120. });
  121.  
  122.  
  123. // Prevent the default action when a file is dropped on the window
  124. $(document).on('drop dragover', function (e) {
  125. e.preventDefault();
  126. });
  127.  
  128. // Helper function that formats the file sizes
  129. function formatFileSize(bytes) {
  130. if (typeof bytes !== 'number') {
  131. return '';
  132. }
  133.  
  134. if (bytes >= 1000000000) {
  135. return (bytes / 1000000000).toFixed(2) + ' GB';
  136. }
  137.  
  138. if (bytes >= 1000000) {
  139. return (bytes / 1000000).toFixed(2) + ' MB';
  140. }
  141.  
  142. return (bytes / 1000).toFixed(2) + ' KB';
  143. }
  144.  
  145. });
Add Comment
Please, Sign In to add comment