Guest User

Untitled

a guest
Jan 20th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.36 KB | None | 0 0
  1. $("#drop1, #drop2, #drop3").dropzone();
  2.  
  3. $(".drop_zone").dropzone();
  4.  
  5. (function ($) {
  6.  
  7. $.fn.dropzone = function (options) {
  8. var opts = {};
  9. var uploadFiles = function (files) {
  10. $.fn.dropzone.newFilesDropped(); for (var i = 0; i < files.length; i++) {
  11. var file = filesi?;
  12. // create a new xhr object var xhr = new XMLHttpRequest(); var upload = xhr.upload; upload.fileIndex = i; upload.fileObj = file; upload.downloadStartTime = new Date().getTime(); upload.currentStart = upload.downloadStartTime; upload.currentProgress = 0; upload.startData = 0;
  13. // add listeners upload.addEventListener("progress", progress, false);
  14. xhr.onload = function (event) {
  15. load(event, xhr);
  16. };
  17. xhr.open(opts.method, opts.url); xhr.setRequestHeader("Cache-Control", "no-cache"); xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest"); xhr.setRequestHeader("X-File-Name", file.fileName); xhr.setRequestHeader("X-File-Size", file.fileSize); xhr.setRequestHeader("Content-Type", "multipart/form-data"); xhr.send(file);
  18. $.fn.dropzone.uploadStarted(i, file);
  19. }
  20. };
  21. var drop = function (event) {
  22. var dt = event.dataTransfer; var files = dt.files;
  23. event.preventDefault(); uploadFiles(files);
  24. return false;
  25. };
  26. var log = function (logMsg) {
  27. if (opts.printLogs) {
  28. // console && console.log(logMsg);
  29. }
  30. };
  31. // invoked when the input field has changed and new files have been dropped // or selected var change = function (event) {
  32. event.preventDefault();
  33. // get all files ... var files = this.files;
  34. // ... and upload them uploadFiles(files);
  35. };
  36. var progress = function (event) {
  37. if (event.lengthComputable) {
  38. var percentage = Math.round((event.loaded 100) / event.total); if (this.currentProgress != percentage) {
  39. // log(this.fileIndex + " --> " + percentage + "%");
  40. this.currentProgress = percentage; $.fn.dropzone.fileUploadProgressUpdated(this.fileIndex, this.fileObj, this.currentProgress);
  41. var elapsed = new Date().getTime(); var diffTime = elapsed - this.currentStart; if (diffTime >= opts.uploadRateRefreshTime) {
  42. var diffData = event.loaded - this.startData; var speed = diffData / diffTime; // in KB/sec
  43. $.fn.dropzone.fileUploadSpeedUpdated(this.fileIndex, this.fileObj, speed);
  44. this.startData = event.loaded; this.currentStart = elapsed;
  45. }
  46. }
  47. }
  48. };
  49. var load = function (event, xhr) {
  50. var now = new Date().getTime(); var timeDiff = now - this.downloadStartTime; if (opts.onLoadComplete) {
  51. opts.onLoadComplete(xhr.responseText);
  52. } $.fn.dropzone.uploadFinished(this.fileIndex, this.fileObj, timeDiff); log("finished loading of file " + this.fileIndex);
  53. };
  54. var dragenter = function (event) {
  55. event.stopPropagation(); event.preventDefault(); return false;
  56. };
  57. var dragover = function (event) {
  58. event.stopPropagation(); event.preventDefault(); return false;
  59. };
  60. // Extend our default options with those provided. opts = $.extend({}, $.fn.dropzone.defaults, options);
  61. var id = this.attr("id"); var dropzone = document.getElementById(id);
  62. log("adding dnd-file-upload functionalities to element with id: " + id);
  63. // hack for safari on windows: due to not supported drop/dragenter/dragover events we have to create a invisible <input type="file" /> tag instead if ($.client.browser == "Safari" && $.client.os == "Windows") {
  64. var fileInput = $("<input>"); fileInput.attr({
  65. type: "file"
  66. }); fileInput.bind("change", change); fileInput.css({
  67. 'opacity': '0', 'width': '100%', 'height': '100%'
  68. }); fileInput.attr("multiple", "multiple"); fileInput.click(function () {
  69. return false;
  70. }); this.append(fileInput);
  71. } else {
  72. dropzone.addEventListener("drop", drop, true); var jQueryDropzone = $("#" + id); jQueryDropzone.bind("dragenter", dragenter); jQueryDropzone.bind("dragover", dragover);
  73. }
  74. return this;
  75. };
  76. $.fn.dropzone.defaults = {
  77. url: "", method: "POST", numConcurrentUploads: 3, printLogs: false, // update upload speed every second uploadRateRefreshTime: 1000
  78. };
  79. // invoked when new files are dropped $.fn.dropzone.newFilesDropped = function () { };
  80. // invoked when the upload for given file has been started $.fn.dropzone.uploadStarted = function (fileIndex, file) { };
  81. // invoked when the upload for given file has been finished $.fn.dropzone.uploadFinished = function (fileIndex, file, time) { };
  82. // invoked when the progress for given file has changed $.fn.dropzone.fileUploadProgressUpdated = function (fileIndex, file,
  83. newProgress) {
  84. };
  85. // invoked when the upload speed of given file has changed $.fn.dropzone.fileUploadSpeedUpdated = function (fileIndex, file,
  86. KBperSecond) {
  87. };
  88. })(jQuery);
Add Comment
Please, Sign In to add comment