Guest User

Untitled

a guest
Jun 20th, 2018
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.87 KB | None | 0 0
  1. (function($) {
  2. jQuery.fn.html5_upload = function(options) {
  3.  
  4. var available_events = ['onStart', 'onStartOne', 'onProgress', 'onFinishOne', 'onFinish', 'onError'];
  5. var options = jQuery.extend({
  6. onStart: function(event, total) {
  7. return true;
  8. },
  9. onStartOne: function(event, name, number, total) {
  10. return true;
  11. },
  12. onProgress: function(event, progress, name, number, total) {
  13. },
  14. onFinishOne: function(event, response, name, number, total) {
  15. },
  16. onFinish: function(event, total) {
  17. },
  18. onError: function(event, name, error) {
  19. },
  20. onBrowserIncompatible: function() {
  21. alert("Sorry, but your browser is incompatible with uploading files using HTML5 (at least, with current preferences.\n Please install the latest version of Firefox, Safari or Chrome");
  22. },
  23. autostart: true,
  24. autoclear: true,
  25. stopOnFirstError: false,
  26. sendBoundary: false,
  27. fieldName: 'user_file[]',//ignore if sendBoundary is false
  28. method: 'post',
  29.  
  30. STATUSES: {
  31. 'STARTED': 'started',
  32. 'PROGRESS': 'progress',
  33. 'LOADED': 'loaded',
  34. 'FINISHED': 'finished'
  35. },
  36.  
  37. setName: function(text) {},
  38. setStatus: function(text) {},
  39. setProgress: function(value) {},
  40.  
  41. genName: function(file, number, total) {
  42. return file + "(" + (number+1) + " of " + total + ")";
  43. },
  44. genStatus: function(progress, finished) {
  45. if (finished) {
  46. return options.STATUSES['FINISHED'];
  47. }
  48. if (progress == 0) {
  49. return options.STATUSES['STARTED'];
  50. }
  51. else if (progress == 1) {
  52. return options.STATUSES['LOADED'];
  53. }
  54. else {
  55. return options.STATUSES['PROGRESS'];
  56. }
  57. },
  58. genProgress: function(loaded, total) {
  59. return loaded / total;
  60. }
  61. }, options);
  62.  
  63. function upload() {
  64. var files = this.files;
  65. var total = files.length;
  66. var $this = $(this);
  67. if (!$this.triggerHandler('html5_upload.onStart', [total])) {
  68. return false;
  69. }
  70. this.disabled = true;
  71. var uploaded = 0;
  72. var xhr = this.html5_upload['xhr'];
  73. this.html5_upload['continue_after_abort'] = true;
  74. function upload_file(number) {
  75. if (number == total) {
  76. $this.trigger('html5_upload.onFinish', [total]);
  77. options.setStatus(options.genStatus(1, true));
  78. $this.attr("disabled", false);
  79. if (options.autoclear) {
  80. $this.val("");
  81. }
  82. return;
  83. }
  84. var file = files[number];
  85. if (!$this.triggerHandler('html5_upload.onStartOne', [file.fileName, number, total])) {
  86. return upload_file(number+1);
  87. }
  88. options.setStatus(options.genStatus(0));
  89. options.setName(options.genName(file.fileName, number, total));
  90. options.setProgress(options.genProgress(0, file.fileSize));
  91. xhr.upload['onprogress'] = function(rpe) {
  92. $this.trigger('html5_upload.onProgress', [rpe.loaded / rpe.total, file.fileName, number, total]);
  93. options.setStatus(options.genStatus(rpe.loaded / rpe.total));
  94. options.setProgress(options.genProgress(rpe.loaded, rpe.total));
  95. };
  96. xhr.onload = function(load) {
  97. $this.trigger('html5_upload.onFinishOne', [xhr.responseText, file.fileName, number, total]);
  98. options.setStatus(options.genStatus(1, true));
  99. options.setProgress(options.genProgress(file.fileSize, file.fileSize));
  100. upload_file(number+1);
  101. };
  102. xhr.onabort = function() {
  103. if ($this[0].html5_upload['continue_after_abort']) {
  104. upload_file(number+1);
  105. }
  106. else {
  107. $this.attr("disabled", false);
  108. if (options.autoclear) {
  109. $this.val("");
  110. }
  111. }
  112. };
  113. xhr.onerror = function(e) {
  114. $this.trigger('html5_upload.onError', [file.fileName, e]);
  115. if (!options.stopOnFirstError) {
  116. upload_file(number+1);
  117. }
  118. };
  119. xhr.open(options.method, typeof(options.url) == "function" ? options.url(number) : options.url, true);
  120. xhr.setRequestHeader("Cache-Control", "no-cache");
  121. xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
  122. xhr.setRequestHeader("X-File-Name", file.fileName);
  123. xhr.setRequestHeader("X-File-Size", file.fileSize);
  124.  
  125. if (!options.sendBoundary) {
  126. xhr.setRequestHeader("Content-Type", "multipart/form-data");
  127. xhr.send(file);
  128. }
  129. else {
  130. if (window.FormData) {//Many thanks to scottt.tw
  131. var f = new FormData();
  132. f.append(typeof(options.fieldName) == "function" ? options.fieldName() : options.fieldName, file);
  133. xhr.send(f);
  134. }
  135. else if (file.getAsBinary) {//Thanks to jm.schelcher
  136. var boundary = '------multipartformboundary' + (new Date).getTime();
  137. var dashdash = '--';
  138. var crlf = '\r\n';
  139.  
  140. /* Build RFC2388 string. */
  141. var builder = '';
  142.  
  143. builder += dashdash;
  144. builder += boundary;
  145. builder += crlf;
  146.  
  147. builder += 'Content-Disposition: form-data; name="'+(typeof(options.fieldName) == "function" ? options.fieldName() : options.fieldName)+'"';
  148. builder += '; filename="' + file.fileName + '"';
  149. builder += crlf;
  150.  
  151. builder += 'Content-Type: application/octet-stream';
  152. builder += crlf;
  153. builder += crlf;
  154.  
  155. /* Append binary data. */
  156. builder += file.getAsBinary();
  157. builder += crlf;
  158.  
  159. /* Write boundary. */
  160. builder += dashdash;
  161. builder += boundary;
  162. builder += dashdash;
  163. builder += crlf;
  164.  
  165. xhr.setRequestHeader('content-type', 'multipart/form-data; boundary=' + boundary);
  166. xhr.sendAsBinary(builder);
  167. }
  168. else {
  169. options.onBrowserIncompatible();
  170. }
  171. }
  172. }
  173. upload_file(0);
  174. return true;
  175. }
  176.  
  177. return this.each(function() {
  178. this.html5_upload = {
  179. xhr: new XMLHttpRequest(),
  180. continue_after_abort: true
  181. };
  182. if (options.autostart) {
  183. $(this).bind('change', upload);
  184. }
  185. for (event in available_events) {
  186. if (options[available_events[event]]) {
  187. $(this).bind("html5_upload."+available_events[event], options[available_events[event]]);
  188. }
  189. }
  190. $(this)
  191. .bind('html5_upload.start', upload)
  192. .bind('html5_upload.cancelOne', function() {
  193. this.html5_upload['xhr'].abort();
  194. })
  195. .bind('html5_upload.cancelAll', function() {
  196. this.html5_upload['continue_after_abort'] = false;
  197. this.html5_upload['xhr'].abort();
  198. })
  199. .bind('html5_upload.destroy', function() {
  200. this.html5_upload['continue_after_abort'] = false;
  201. this.xhr.abort();
  202. delete this.html5_upload;
  203. $(this).unbind('html5_upload.*').unbind('change', upload);
  204. });
  205. });
  206. };
  207. })(jQuery);
Add Comment
Please, Sign In to add comment