Guest User

Untitled

a guest
Nov 17th, 2018
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.73 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <title>HTML5 File Drag & Drop API</title>
  6. <style>
  7. /*
  8. Styles for HTML5 File Drag & Drop demonstration
  9. Featured on SitePoint.com
  10. Developed by Craig Buckler (@craigbuckler) of OptimalWorks.net
  11. */
  12. body
  13. {
  14. font-family: "Segoe UI", Tahoma, Helvetica, freesans, sans-serif;
  15. font-size: 90%;
  16. margin: 10px;
  17. color: #333;
  18. background-color: #fff;
  19. }
  20.  
  21. h1, h2
  22. {
  23. font-size: 1.5em;
  24. font-weight: normal;
  25. }
  26.  
  27. h2
  28. {
  29. font-size: 1.3em;
  30. }
  31.  
  32. legend
  33. {
  34. font-weight: bold;
  35. color: #333;
  36. }
  37.  
  38. #filedrag
  39. {
  40. display: none;
  41. font-weight: bold;
  42. text-align: center;
  43. padding: 1em 0;
  44. margin: 1em 0;
  45. color: #555;
  46. border: 2px dashed #555;
  47. border-radius: 7px;
  48. cursor: default;
  49. }
  50.  
  51. #filedrag.hover
  52. {
  53. color: #f00;
  54. border-color: #f00;
  55. border-style: solid;
  56. box-shadow: inset 0 3px 4px #888;
  57. }
  58.  
  59. img
  60. {
  61. max-width: 100%;
  62. }
  63.  
  64. pre
  65. {
  66. width: 95%;
  67. height: 8em;
  68. font-family: monospace;
  69. font-size: 0.9em;
  70. padding: 1px 2px;
  71. margin: 0 0 1em auto;
  72. border: 1px inset #666;
  73. background-color: #eee;
  74. overflow: auto;
  75. }
  76.  
  77. #messages
  78. {
  79. padding: 0 10px;
  80. margin: 1em 0;
  81. border: 1px solid #999;
  82. }
  83.  
  84. #progress p
  85. {
  86. display: block;
  87. width: 240px;
  88. padding: 2px 5px;
  89. margin: 2px 0;
  90. border: 1px inset #446;
  91. border-radius: 5px;
  92. background: #eee url("progress.png") 100% 0 repeat-y;
  93. }
  94.  
  95. #progress p.success
  96. {
  97. background: #0c0 none 0 0 no-repeat;
  98. }
  99.  
  100. #progress p.failed
  101. {
  102. background: #c00 none 0 0 no-repeat;
  103. }
  104. </style>
  105. </head>
  106. <body>
  107. <form id="upload" action="index.html" method="POST" enctype="multipart/form-data">
  108.  
  109. <fieldset>
  110. <legend>HTML File Upload</legend>
  111.  
  112. <input type="hidden" id="MAX_FILE_SIZE" name="MAX_FILE_SIZE" value="300000" />
  113.  
  114. <div>
  115. <label for="fileselect">Files to upload:</label>
  116. <input type="file" id="fileselect" name="fileselect[]" multiple="multiple" />
  117. <div id="filedrag">or drop files here</div>
  118. </div>
  119.  
  120. <div id="submitbutton">
  121. <button type="submit">Upload Files</button>
  122. </div>
  123.  
  124. </fieldset>
  125.  
  126. </form>
  127.  
  128. <div id="messages">
  129. <p>Status Messages</p>
  130. </div>
  131. <script>
  132. /*
  133. filedrag.js - HTML5 File Drag & Drop demonstration
  134. Featured on SitePoint.com
  135. Developed by Craig Buckler (@craigbuckler) of OptimalWorks.net
  136. */
  137. (function() {
  138.  
  139. // getElementById
  140. function $id(id) {
  141. return document.getElementById(id);
  142. }
  143.  
  144.  
  145. // output information
  146. function Output(msg) {
  147. var m = $id("messages");
  148. m.innerHTML = msg + m.innerHTML;
  149. }
  150.  
  151.  
  152. // file drag hover
  153. function FileDragHover(e) {
  154. e.stopPropagation();
  155. e.preventDefault();
  156. e.target.className = (e.type == "dragover" ? "hover" : "");
  157. }
  158.  
  159.  
  160. // file selection
  161. function FileSelectHandler(e) {
  162.  
  163. // cancel event and hover styling
  164. FileDragHover(e);
  165.  
  166. // fetch FileList object
  167. var files = e.target.files || e.dataTransfer.files;
  168.  
  169. // process all File objects
  170. for (var i = 0, f; f = files[i]; i++) {
  171. ParseFile(f);
  172. }
  173.  
  174. }
  175.  
  176.  
  177. // output file information
  178. function ParseFile(file) {
  179.  
  180. Output(
  181. "<p>File information: <strong>" + file.name +
  182. "</strong> type: <strong>" + file.type +
  183. "</strong> size: <strong>" + file.size +
  184. "</strong> bytes</p>"
  185. );
  186.  
  187. }
  188.  
  189.  
  190. // initialize
  191. function Init() {
  192.  
  193. var fileselect = $id("fileselect"),
  194. filedrag = $id("filedrag"),
  195. submitbutton = $id("submitbutton");
  196.  
  197. // file select
  198. fileselect.addEventListener("change", FileSelectHandler, false);
  199.  
  200. // is XHR2 available?
  201. var xhr = new XMLHttpRequest();
  202. if (xhr.upload) {
  203.  
  204. // file drop
  205. filedrag.addEventListener("dragover", FileDragHover, false);
  206. filedrag.addEventListener("dragleave", FileDragHover, false);
  207. filedrag.addEventListener("drop", FileSelectHandler, false);
  208. filedrag.style.display = "block";
  209.  
  210. // remove submit button
  211. submitbutton.style.display = "none";
  212. }
  213.  
  214. }
  215.  
  216. // call initialization file
  217. if (window.File && window.FileList && window.FileReader) {
  218. Init();
  219. }
  220.  
  221.  
  222. })();
  223. </script>
  224. </body>
  225. </html>
Add Comment
Please, Sign In to add comment