zzxUpload.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. var ZZXUpload = function(form, url, complete,noprocess) {
  2. if (url === null || url === '') {
  3. alert('url 不能为空!');
  4. return false;
  5. }
  6. var _self = this;
  7. var ZZXUpload, formData;
  8. _self.formData = $(form).length > 0 ? new FormData($(form)[0]) : new FormData();
  9. _self.url = url;
  10. _self.put = function(key, val) {
  11. _self.formData.append(key, val);
  12. }
  13. _self.upload = function(files) {
  14. uploadFiles(files);
  15. /*
  16. * _self.files = files; _self.files.length > 0 ?
  17. * uploadFile(_self.files.shift()) : '';
  18. */
  19. }
  20. var processFiles = function(files) {
  21. var file, _i, _len;
  22. for (_i = 0, _len = files.length; _i < _len; _i++) {
  23. file = files[_i];
  24. file.processing = true;
  25. file.status = Dropzone.UPLOADING;
  26. $(file.previewTemplate).addClass("dz-processing");
  27. }
  28. return uploadFiles(files);
  29. }
  30. var uploadFiles = function(files) {
  31. var file, formData, handleError, headerName, headerValue, headers, i, input, inputName, inputType, key, method, option, progressObj, response, updateProgress, url, value, xhr, _i, _j, _k, _l, _len, _len1, _len2, _len3, _m, _ref, _ref1, _ref2, _ref3, _ref4, _ref5;
  32. xhr = new XMLHttpRequest();
  33. for (_i = 0, _len = files.length; _i < _len; _i++) {
  34. file = files[_i];
  35. file.xhr = xhr;
  36. }
  37. xhr.open('post', _self.url, true);
  38. response = null;
  39. handleError = (function(_this) {
  40. return function() {
  41. var _j, _len1, _results;
  42. _results = [];
  43. for (_j = 0, _len1 = files.length; _j < _len1; _j++) {
  44. file = files[_j];
  45. }
  46. return _results;
  47. };
  48. })(this);
  49. updateProgress = (function(_this) {
  50. return function(e) {
  51. var allFilesFinished, progress, _j, _k, _l, _len1, _len2, _len3, _results;
  52. if (e != null) {
  53. progress = 100 * e.loaded / e.total;
  54. for (_j = 0, _len1 = files.length; _j < _len1; _j++) {
  55. file = files[_j];
  56. file.upload = {
  57. progress : progress,
  58. total : e.total,
  59. bytesSent : e.loaded
  60. };
  61. }
  62. } else {
  63. allFilesFinished = true;
  64. progress = 100;
  65. for (_k = 0, _len2 = files.length; _k < _len2; _k++) {
  66. file = files[_k];
  67. if (!(file.upload.progress === 100 && file.upload.bytesSent === file.upload.total)) {
  68. allFilesFinished = false;
  69. }
  70. file.upload.progress = progress;
  71. file.upload.bytesSent = file.upload.total;
  72. }
  73. if (allFilesFinished) {
  74. return;
  75. }
  76. }
  77. _results = [];
  78. for (_l = 0, _len3 = files.length; _l < _len3; _l++) {
  79. file = files[_l];
  80. _results.push(uploadprogress(file, progress,
  81. file.upload.bytesSent));
  82. }
  83. return _results;
  84. };
  85. })(this);
  86. xhr.onload = (function(_this) {
  87. return function(e) {
  88. var _ref;
  89. if(files.length > 0){
  90. if (files[0].status === Dropzone.CANCELED) {
  91. return;
  92. }
  93. }
  94. if (xhr.readyState !== 4) {
  95. return;
  96. }
  97. response = xhr.responseText;
  98. if (xhr.getResponseHeader("content-type")
  99. && ~xhr.getResponseHeader("content-type").indexOf(
  100. "application/json")) {
  101. try {
  102. response = JSON.parse(response);
  103. } catch (_error) {
  104. e = _error;
  105. response = "Invalid JSON response from server.";
  106. }
  107. }
  108. if(!noprocess){
  109. updateProgress();
  110. }
  111. typeof complete == 'function' ? complete(xhr.status,xhr.responseText) : '';
  112. if (!((200 <= (_ref = xhr.status) && _ref < 300))) {
  113. return handleError();
  114. } else {
  115. return true;
  116. }
  117. };
  118. })(this);
  119. xhr.onerror = (function(_this) {
  120. return function() {
  121. if (files[0].status === Dropzone.CANCELED) {
  122. return;
  123. }
  124. return handleError();
  125. };
  126. })(this);
  127. progressObj = (_ref = xhr.upload) != null ? _ref : xhr;
  128. progressObj.onprogress = updateProgress;
  129. headers = {
  130. "Accept" : "application/json",
  131. "Cache-Control" : "no-cache",
  132. "X-Requested-With" : "XMLHttpRequest"
  133. };
  134. for (headerName in headers) {
  135. headerValue = headers[headerName];
  136. xhr.setRequestHeader(headerName, headerValue);
  137. }
  138. formData = _self.formData;
  139. if(files.length > 0){
  140. for (i = _m = 0, _ref5 = files.length - 1; 0 <= _ref5 ? _m <= _ref5 : _m >= _ref5; i = 0 <= _ref5 ? ++_m : --_m) {
  141. var paramName = files[i].paramName == null ? files[i].name : files[i].paramName;
  142. paramName = paramName + '_' + i;
  143. formData.append(paramName, files[i]);
  144. }
  145. }
  146. return xhr.send(formData);
  147. };
  148. var uploadprogress = function(file, progress, bytesSent) {
  149. var node, _i, _len, _ref, _results;
  150. if (file.previewElement) {
  151. _ref = $(file.previewElement).find('[data-dz-uploadprogress]');
  152. _results = [];
  153. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  154. node = _ref[_i];
  155. if (node.nodeName === 'PROGRESS') {
  156. _results.push(node.value = progress);
  157. } else {
  158. _results.push(node.style.width = "" + progress + "%");
  159. }
  160. }
  161. return _results;
  162. }
  163. }
  164. var uploadFile = function(file) {
  165. var formData, handleError, input, inputElement, inputName, progressObj, xhr, _i, _len, _ref, _ref1, _this = this;
  166. xhr = new XMLHttpRequest();
  167. formData = new FormData();
  168. formData.append(file.name, file);
  169. xhr.open("POST", _self.url, true);
  170. handleError = function() {
  171. return _this.errorProcessing(file, xhr.responseText
  172. || ("Server responded with " + xhr.status + " code."));
  173. };
  174. xhr.onload = function(e) {
  175. var response;
  176. if (xhr.status !== 200) {
  177. return handleError();
  178. } else {
  179. var $preview = $(file.previewTemplate).find(
  180. ".progress-bar-success");
  181. $(file.previewTemplate).find(".progress-bar-success").css({
  182. width : "" + 100 + "%"
  183. });
  184. response = xhr.responseText;
  185. if (~xhr.getResponseHeader("content-type").indexOf(
  186. "application/json")) {
  187. response = $.parseJSON(response);
  188. }
  189. $(file.previewTemplate).addClass("success");
  190. previewTemplate: "<div class=\"preview file-preview\">\n <div class=\"details\">\n <div class=\"filename\"><span></span></div>\n </div>\n <div class=\"progress\"><span class=\"upload\"></span></div>\n <div class=\"success-mark\"><span>✔</span></div>\n <div class=\"error-mark\"><span>✘</span></div>\n <div class=\"error-message\"><span></span></div>\n</div>"
  191. _self.files.length > 0 ? uploadFile(_self.files.shift()) : '';
  192. return true;
  193. }
  194. };
  195. xhr.onerror = function() {
  196. return handleError();
  197. };
  198. progressObj = (_ref1 = xhr.upload) != null ? _ref1 : xhr;
  199. progressObj.onprogress = function(e) {
  200. var progress = Math.min(100, (e.loaded / e.total) * 100);
  201. var $preview = $(file.previewTemplate)
  202. .find(".progress-bar-success").css({
  203. width : "" + Math.max(0, progress + "%")
  204. });
  205. return true;
  206. };
  207. xhr.setRequestHeader("Accept", "application/json");
  208. xhr.setRequestHeader("Cache-Control", "no-cache");
  209. xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
  210. xhr.setRequestHeader("X-File-Name", file.name);
  211. return xhr.send(formData);
  212. };
  213. };
  214. /*document.write("<script src='assets/js/jquery.colorbox-min.js'></script>");
  215. $(function($) {
  216. var $overflow = '';
  217. var colorbox_params = {
  218. rel: 'colorbox',
  219. reposition:true,
  220. scalePhotos:true,
  221. scrolling:false,
  222. previous:'<i class="ace-icon fa fa-arrow-left"></i>',
  223. next:'<i class="ace-icon fa fa-arrow-right"></i>',
  224. close:'&times;',
  225. current:'{current} of {total}',
  226. maxWidth:'100%',
  227. maxHeight:'100%',
  228. onOpen:function(){
  229. $overflow = document.body.style.overflow;
  230. document.body.style.overflow = 'hidden';
  231. },
  232. onClosed:function(){
  233. document.body.style.overflow = $overflow;
  234. },
  235. onComplete:function() {
  236. $.colorbox.resize();
  237. }
  238. };
  239. $('.ace-thumbnails [data-rel="colorbox"]').colorbox(colorbox_params);
  240. //let's add a custom loading icon
  241. $("#cboxLoadingGraphic").html("<i class='ace-icon fa fa-spinner orange'></i>");
  242. });*/
  243. function delFile(fileid){
  244. if(fileid!=undefined && fileid!="" && confirm('您确定要删除吗?')){
  245. $("#"+fileid).remove();
  246. var ids = $("#delFileIds").val();
  247. if(ids==undefined || ids==""){
  248. ids=fileid;
  249. $("#delFileIds").val(ids);
  250. }else{
  251. var tmp=ids+","+fileid;
  252. $("#delFileIds").val(tmp);
  253. }
  254. }
  255. }