You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

attachments.js 30 kB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770
  1. /* Redmine - project management software
  2. Copyright (C) 2006-2013 Jean-Philippe Lang */
  3. function postUpMsg(attachmentId) {
  4. $.ajax({
  5. url: '/attachments/renderTag',
  6. type: "GET",
  7. data: {
  8. attachmentId: attachmentId
  9. }
  10. })
  11. }
  12. function reload(fileSpan) {
  13. fileSpan.remove();
  14. $('#upload_file_count').html("<%= l(:label_no_file_uploaded)%>");
  15. $old_file = $("#_file");
  16. $new_file = $old_file.clone(true);
  17. $old_file.replaceWith($new_file);
  18. $new_file.show();
  19. }
  20. function addFile_board(inputEl, file, eagerUpload, id,btnId) {
  21. var attachments_frame = '#attachments_fields' + id;
  22. if ($(attachments_frame).children().length < 30) {
  23. deleteallfiles = $(inputEl).data('deleteAllFiles');
  24. var attachmentId = addFile.nextAttachmentId++;
  25. var fileSpan = $('<span>', {
  26. 'id': 'attachments_' + attachmentId,
  27. 'class': 'attachment'
  28. });
  29. fileSpan.append(
  30. $('<input>', {
  31. 'type': 'text',
  32. 'class': 'upload_filename readonly',
  33. 'name': 'attachments[' + attachmentId + '][filename]',
  34. 'readonly': 'readonly'
  35. }).val(file.name),
  36. // $('<input>', {
  37. // 'type': 'text',
  38. // 'class': 'description',
  39. // 'name': 'attachments[' + attachmentId + '][description]',
  40. // 'maxlength': 254,
  41. // 'placeholder': $(inputEl).data('descriptionPlaceholder')
  42. // }).toggle(!eagerUpload),
  43. // $('<span >' + $(inputEl).data('fieldIsPublic') + ':</span>').attr({
  44. // 'class': 'ispublic-label'
  45. // }),
  46. // $('<input>', {
  47. // 'type': 'checkbox',
  48. // 'class': 'is_public_checkbox',
  49. // 'value': 1,
  50. // 'name': 'attachments[' + attachmentId + '][is_public_checkbox]',
  51. // checked: 'checked'
  52. // }).toggle(!eagerUpload),
  53. $('<a>&nbsp</a>').attr({
  54. 'href': "#",
  55. 'class': 'remove-upload',
  56. 'data-containerid': id
  57. }).click(function() {
  58. if (confirm($(inputEl).data('areYouSure'))) {
  59. removeFile();
  60. if (!eagerUpload) {
  61. (function(e) {
  62. reload(e);
  63. })(fileSpan);
  64. }
  65. }
  66. }).toggle(!eagerUpload),
  67. $('<div>', {
  68. 'class': 'div_attachments',
  69. 'name': 'div_' + 'attachments_' + attachmentId
  70. })
  71. ).appendTo(attachments_frame);
  72. if (eagerUpload) {
  73. ajaxUpload(file, attachmentId, fileSpan, inputEl,btnId);
  74. }
  75. return attachmentId;
  76. }
  77. return null;
  78. }
  79. function addFile(inputEl, file, eagerUpload,btnId) {
  80. var attachments_frame = '#attachments_fields';
  81. if (true) {
  82. deleteallfiles = $(inputEl).data('deleteAllFiles');
  83. var attachmentId = addFile.nextAttachmentId++;
  84. var fileSpan = $('<span>', {
  85. 'id': 'attachments_' + attachmentId,
  86. 'class': 'attachment'
  87. });
  88. fileSpan.append(
  89. $('<input>', {
  90. 'type': 'text',
  91. 'class': 'upload_filename readonly',
  92. 'name': 'attachments[' + attachmentId + '][filename]',
  93. 'readonly': 'readonly'
  94. }).val(file.name),
  95. // $('<input>', {
  96. // 'type': 'text',
  97. // 'class': 'description',
  98. // 'name': 'attachments[' + attachmentId + '][description]',
  99. // 'maxlength': 254,
  100. // 'placeholder': $(inputEl).data('descriptionPlaceholder')
  101. // }).toggle(!eagerUpload),
  102. // $('<span >' + $(inputEl).data('fieldIsPublic') + ':</span>').attr({
  103. // 'class': 'ispublic-label'
  104. // }),
  105. // $('<input>', {
  106. // 'type': 'checkbox',
  107. // 'class': 'is_public_checkbox',
  108. // 'value': 1,
  109. // 'name': 'attachments[' + attachmentId + '][is_public_checkbox]',
  110. // checked: 'checked'
  111. // }).toggle(!eagerUpload),
  112. $('<a>&nbsp</a>').attr({
  113. 'href': "#",
  114. 'class': 'remove-upload'
  115. }).click(function() {
  116. if (confirm($(inputEl).data('areYouSure'))) {
  117. removeFile();
  118. if (!eagerUpload) {
  119. (function(e) {
  120. reload(e);
  121. })(fileSpan);
  122. }
  123. }
  124. }).toggle(!eagerUpload),
  125. $('<div>', {
  126. 'class': 'div_attachments',
  127. 'name': 'div_' + 'attachments_' + attachmentId
  128. })
  129. ).appendTo('#attachments_fields');
  130. if (eagerUpload) {
  131. ajaxUpload(file, attachmentId, fileSpan, inputEl,btnId);
  132. }
  133. return attachmentId;
  134. }
  135. return null;
  136. }
  137. addFile.nextAttachmentId = 1;
  138. function addReviseFile(inputEl, file, eagerUpload,btnId) {
  139. var attachments_frame = '#attachments_fields';
  140. if ($(attachments_frame).children().length < 30) {
  141. deleteallfiles = $(inputEl).data('deleteAllFiles');
  142. var attachmentId = addFile.nextAttachmentId++;
  143. var fileSpan = $('<span>', {
  144. 'id': 'attachments_' + attachmentId,
  145. 'class': 'attachment'
  146. });
  147. fileSpan.append(
  148. $('<input>', {
  149. 'type': 'text',
  150. 'class': 'upload_filename readonly',
  151. 'name': 'attachments[' + attachmentId + '][filename]',
  152. 'readonly': 'readonly'
  153. }).val(file.name),
  154. // $('<input>', {
  155. // 'type': 'text',
  156. // 'class': 'description',
  157. // 'name': 'attachments[' + attachmentId + '][description]',
  158. // 'maxlength': 254,
  159. // 'placeholder': $(inputEl).data('descriptionPlaceholder')
  160. // }).toggle(!eagerUpload),
  161. // $('<span >' + $(inputEl).data('fieldIsPublic') + ':</span>').attr({
  162. // 'class': 'ispublic-label'
  163. // }),
  164. // $('<input>', {
  165. // 'type': 'checkbox',
  166. // 'class': 'is_public_checkbox',
  167. // 'value': 1,
  168. // 'name': 'attachments[' + attachmentId + '][is_public_checkbox]',
  169. // checked: 'checked'
  170. // }).toggle(!eagerUpload),
  171. $('<a>&nbsp</a>').attr({
  172. 'href': "#",
  173. 'class': 'remove-upload'
  174. }).click(function() {
  175. if (confirm($(inputEl).data('areYouSure'))) {
  176. removeReviseFile();
  177. if (!eagerUpload) {
  178. (function(e) {
  179. reload(e);
  180. })(fileSpan);
  181. }
  182. }
  183. }).toggle(!eagerUpload),
  184. $('<div>', {
  185. 'class': 'div_attachments',
  186. 'name': 'div_' + 'attachments_' + attachmentId
  187. })
  188. ).appendTo('#attachments_fields');
  189. $("#uploadReviseBox").addClass('disable_link');
  190. $("#choose_revise_attach").attr("onclick","return false;");
  191. if (eagerUpload) {
  192. ajaxUpload(file, attachmentId, fileSpan, inputEl,btnId);
  193. }
  194. return attachmentId;
  195. }
  196. return null;
  197. }
  198. function ajaxUpload(file, attachmentId, fileSpan, inputEl,btnId) {
  199. //上传开始调用函数
  200. function onLoadstart(e) {
  201. fileSpan.removeClass('ajax-waiting');
  202. fileSpan.addClass('ajax-loading');
  203. $('input:submit', $(this).parents('form')).attr('disabled', 'disabled');
  204. }
  205. //更改progressbar的值
  206. function onProgress(e) {
  207. if (e.lengthComputable) {
  208. this.progressbar('value', e.loaded * 100 / e.total);
  209. }
  210. }
  211. function actualUpload(file, attachmentId, fileSpan, inputEl,btnId) {
  212. ajaxUpload.uploading++;
  213. uploadBlob(file, $(inputEl).data('upload-path'), attachmentId, {
  214. loadstartEventHandler: onLoadstart.bind(progressSpan),
  215. progressEventHandler: onProgress.bind(progressSpan)
  216. })
  217. .done(function(result) {
  218. progressSpan.progressbar('value', 100).remove(); //上传完成就去掉该文件名后的进度条
  219. fileSpan.find('input.description, a').css('display', 'inline-block');
  220. fileSpan.find('input.is_public_checkbox, a').css('display', 'inline-block');
  221. })
  222. .fail(function(result) {
  223. progressSpan.text("网络错误");
  224. if ($("#network_issue")) { //在上传处都要给出一个network_issue的div用来显示错误
  225. $("#network_issue").show();
  226. }
  227. }).always(function() {
  228. ajaxUpload.uploading--;
  229. fileSpan.removeClass('ajax-loading');
  230. var containerid = $(inputEl).data('containerid');//多个上传控件需要的容器id
  231. if(containerid == undefined ){
  232. var count = 1;//同步上传参数为1,所以不需要去获取正在上传的文件就知道是1
  233. $('#upload_file_count').html("<span id=\"count\">" + count + "</span>" + $(inputEl).data('lebelFileUploding'));
  234. }
  235. else{
  236. var count = 1;
  237. $('#upload_file_count' + containerid).html("<span id=\"count\">" + count + "</span>" + $(inputEl).data('lebelFileUploding'));
  238. }
  239. var form = fileSpan.parents('form');
  240. if (form.queue('upload').length == 0 && ajaxUpload.uploading == 0) { //所有文件上传完毕,更改状态
  241. $('input:submit', form).removeAttr('disabled');
  242. $('#' + btnId).removeAttr("disabled");
  243. $('#' + btnId).val('确定')
  244. $('#' + btnId).css('background-color', '#269ac9')
  245. if(containerid == undefined ){
  246. var count = $('#attachments_fields>span').length;
  247. $('#upload_file_count').html("<span id=\"count\">" + count + "</span>" + $(inputEl).data('fileCount'));
  248. }else{
  249. var count = $('#attachments_fields' + containerid + '>span').length;
  250. $('#upload_file_count'+containerid).html("<span id=\"count\">" + count + "</span>" + $(inputEl).data('fileCount'));
  251. }
  252. }
  253. form.dequeue('upload');//上传完成一个就退出一个
  254. });
  255. //gcm files count and add delete_all link
  256. //modify by yutao 2015-5-14 ��1��ҳ����ڶ���ϴ��ؼ�ʱ�˿�������bug �ʸ�֮ start
  257. var containerid = $(inputEl).data('containerid');
  258. if (containerid == undefined) {
  259. var count = $('#attachments_fields>span').length;
  260. $('#'+btnId).attr("disabled",true);
  261. $('#'+btnId).val('提交中');
  262. $('#'+btnId).css('background-color','#c1c1c1')
  263. $('#upload_file_count').html("<span id=\"count\">1</span>" + $(inputEl).data('lebelFileUploding'));//目前肯定是穿一个文件
  264. if (count >= 1) {
  265. var add_attachs = $('.add_attachment');
  266. var delete_all = $('.remove_all');
  267. if (delete_all.length < 1) {
  268. add_attachs.append($("<a>&nbsp</a>").attr({
  269. "href": "javascript:void(0)",
  270. 'class': 'remove_all',
  271. "onclick": "removeAll()"
  272. }));
  273. }
  274. }
  275. } else {
  276. var count = $('#attachments_fields' + containerid + '>span').length;
  277. $('#'+btnId).attr("disabled",true);
  278. $('#'+btnId).val('提交中');
  279. $('#'+btnId).css('background-color','#c1c1c1')
  280. $('#upload_file_count' + containerid).html("<span id=\"count\">1</span>" + $(inputEl).data('lebelFileUploding'));//目前肯定是穿一个文件
  281. if (count >= 1) {
  282. var add_attachs = $('.add_attachment').filter(function(index) {
  283. return $(this).data('containerid') == containerid;
  284. });
  285. var delete_all = $('.remove_all').filter(function(index) {
  286. return $(this).data('containerid') == containerid;
  287. });
  288. if (delete_all.length < 1) {
  289. add_attachs.append($("<a>&nbsp</a>").attr({
  290. "href": "javascript:void(0)",
  291. 'class': 'remove_all',
  292. 'data-containerid': containerid,
  293. "onclick": "removeAll('" + containerid + "')"
  294. }));
  295. }
  296. }
  297. //modify by yutao 2015-5-14 ��1��ҳ����ڶ���ϴ��ؼ�ʱ�˿�������bug �ʸ�֮ end
  298. }
  299. //gcm
  300. }
  301. //在插入文件名的后边插入div
  302. var progressSpan = $('<div>').insertAfter(fileSpan.find('input.upload_filename'));
  303. progressSpan.progressbar(); //将div处理成progress bar
  304. fileSpan.addClass('ajax-waiting'); //添加文件正在上传的状态
  305. var containerid = $(inputEl).data('containerid');//多个上传控件需要的容器id
  306. if(containerid == undefined ){ //选择了多少个文件也要显示,因为有时候文件过大,一时半会传不完,但还是显示文件没有选择
  307. var count = $('#attachments_fields>span').length;
  308. $('#upload_file_count').html("<span id=\"count\">" + count + "</span>" + '个文件被选择');
  309. }else{
  310. var count = $('#attachments_fields' + containerid + '>span').length;
  311. $('#upload_file_count'+containerid).html("<span id=\"count\">" + count + "</span>" + '个文件被选择');
  312. }
  313. //最大的同步上传参数
  314. var maxSyncUpload = 1 //$(inputEl).data('max-concurrent-uploads');
  315. //如果没有指定同步上传参数 或者参数不对,或者需要上传的文件已经小于同步上传的个数,那么直接上传
  316. if (maxSyncUpload == null || maxSyncUpload <= 0 || ajaxUpload.uploading < maxSyncUpload)
  317. actualUpload(file, attachmentId, fileSpan, inputEl,btnId);
  318. else //否则就进行队列上传
  319. $(inputEl).parents('form').queue('upload', actualUpload.bind(this, file, attachmentId, fileSpan, inputEl,btnId));
  320. }
  321. ajaxUpload.uploading = 0;
  322. function removeFile() {
  323. $(this).parent('span').remove();
  324. return false;
  325. }
  326. function removeReviseFile() {
  327. $(this).parent('span').remove();
  328. $("#uploadReviseBox").removeClass('disable_link');
  329. $("#choose_revise_attach").attr("onclick","_file.click();");
  330. return false;
  331. }
  332. //gcm delete all file
  333. //modify by yutao 2015-5-14 ��1��ҳ����ڶ���ϴ��ؼ�ʱ�˿�������bug �ʸ�֮ start
  334. function removeAll(containerid) {
  335. if (confirm(deleteallfiles)) {
  336. if (containerid == undefined) {
  337. $(".remove-upload").removeAttr("data-confirm");
  338. $(".remove-upload").click();
  339. } else {
  340. var arr = $(".remove-upload").filter(function() {
  341. return $(this).data('containerid') == containerid;
  342. });
  343. arr.removeAttr("data-confirm");
  344. arr.click();
  345. }
  346. }
  347. // return false;
  348. }
  349. //modify by yutao 2015-5-14 ��1��ҳ����ڶ���ϴ��ؼ�ʱ�˿�������bug �ʸ�֮ end
  350. //gcm
  351. function uploadBlob(blob, uploadUrl, attachmentId, options) {
  352. var actualOptions = $.extend({
  353. loadstartEventHandler: $.noop,
  354. progressEventHandler: $.noop
  355. }, options);
  356. if (uploadUrl.indexOf('?') > 0) {
  357. uploadUrl = uploadUrl + '&attachment_id=' + attachmentId;
  358. } else {
  359. uploadUrl = uploadUrl + '?attachment_id=' + attachmentId;
  360. }
  361. if (blob instanceof window.File) {
  362. uploadUrl += '&filename=' + encodeURIComponent(blob.name);
  363. }
  364. return $.ajax(uploadUrl, {
  365. type: 'POST',
  366. contentType: 'application/octet-stream',
  367. beforeSend: function(jqXhr) {
  368. jqXhr.setRequestHeader('Accept', 'application/js');
  369. },
  370. xhr: function() {
  371. var xhr = $.ajaxSettings.xhr();
  372. xhr.upload.onloadstart = actualOptions.loadstartEventHandler;
  373. xhr.upload.onprogress = actualOptions.progressEventHandler;
  374. return xhr;
  375. },
  376. data: blob,
  377. cache: false,
  378. processData: false
  379. });
  380. }
  381. function addReviseInputFiles(inputEl,btnId) {
  382. // var clearedFileInput = $(inputEl).clone().val('');
  383. if (inputEl.files) {
  384. uploadAndAttachReviseFiles(inputEl.files, inputEl,btnId);
  385. // $(inputEl).remove();
  386. } else {
  387. // browser not supporting the file API, upload on form submission
  388. var attachmentId;
  389. var aFilename = inputEl.value.split(/\/|\\/);
  390. var count = $('#attachments_fields>span').length;
  391. attachmentId = addReviseFile(inputEl, {
  392. name: aFilename[aFilename.length - 1]
  393. }, false);
  394. if (attachmentId) {
  395. $(inputEl).attr({
  396. name: 'attachments[' + attachmentId + '][file]'
  397. }).hide();
  398. if (count <= 0) count = 1;
  399. }
  400. }
  401. //clearedFileInput.insertAfter('#attachments_fields');
  402. }
  403. function addInputFiles(inputEl,btnId) {
  404. // var clearedFileInput = $(inputEl).clone().val('');
  405. if (inputEl.files) {
  406. // if(inputEl.files.length >= 5){
  407. // alert('一次选择的文件不能超过5个')
  408. // return;
  409. // }
  410. // upload files using ajax
  411. uploadAndAttachFiles(inputEl.files, inputEl,btnId);
  412. // $(inputEl).remove();
  413. } else {
  414. // browser not supporting the file API, upload on form submission
  415. var attachmentId;
  416. var aFilename = inputEl.value.split(/\/|\\/);
  417. var count = $('#attachments_fields>span').length;
  418. attachmentId = addFile(inputEl, {
  419. name: aFilename[aFilename.length - 1]
  420. }, false);
  421. if (attachmentId) {
  422. $(inputEl).attr({
  423. name: 'attachments[' + attachmentId + '][file]'
  424. }).hide();
  425. if (count <= 0) count = 1;
  426. $('#upload_file_count').html("<span id=\"count\">" + count + "</span>" + $(inputEl).data('fileCount'));
  427. }
  428. }
  429. //clearedFileInput.insertAfter('#attachments_fields');
  430. }
  431. function addInputFiles_board(inputEl, id,btnId) {
  432. // var clearedFileInput = $(inputEl).clone().val('');
  433. if (inputEl.files) {
  434. // if(inputEl.files.length >= 5){
  435. // alert('一次选择的文件不能超过5个')
  436. // return;
  437. // }
  438. // upload files using ajax
  439. uploadAndAttachFiles_board(inputEl.files, inputEl, id,btnId);
  440. // $(inputEl).remove();
  441. } else {
  442. // browser not supporting the file API, upload on form submission
  443. var attachmentId;
  444. var aFilename = inputEl.value.split(/\/|\\/);
  445. attachmentId = addFile_board(inputEl, {
  446. name: aFilename[aFilename.length - 1]
  447. }, false, id);
  448. if (attachmentId) {
  449. $(inputEl).attr({
  450. name: 'attachments[' + attachmentId + '][file]'
  451. }).hide();
  452. $('#upload_file_count').html("<span id=\"count\">" + count + "</span>" + $(inputEl).data('fileCount'));
  453. }
  454. }
  455. //clearedFileInput.insertAfter('#attachments_fields');
  456. }
  457. function uploadAndAttachReviseFiles(files, inputEl,btnId) {
  458. var maxFileSize = $(inputEl).data('max-file-size');
  459. var maxFileSizeExceeded = $(inputEl).data('max-file-size-message');
  460. var sizeExceeded = false;
  461. $.each(files, function() {
  462. if (this.size && maxFileSize != null && this.size > parseInt(maxFileSize)) {
  463. sizeExceeded = true;
  464. }
  465. });
  466. if (sizeExceeded) {
  467. window.alert(maxFileSizeExceeded);
  468. } else {
  469. $.each(files, function() {
  470. addReviseFile(inputEl, this, true,btnId);
  471. });
  472. }
  473. }
  474. function uploadAndAttachFiles(files, inputEl,btnId) {
  475. var maxFileSize = $(inputEl).data('max-file-size');
  476. var maxFileSizeExceeded = $(inputEl).data('max-file-size-message');
  477. var sizeExceeded = false;
  478. $.each(files, function() {
  479. if (this.size && maxFileSize != null && this.size > parseInt(maxFileSize)) {
  480. sizeExceeded = true;
  481. }
  482. });
  483. if (sizeExceeded) {
  484. window.alert(maxFileSizeExceeded);
  485. } else {
  486. $.each(files, function() {
  487. addFile(inputEl, this, true,btnId);
  488. });
  489. }
  490. }
  491. function uploadAndAttachFiles_board(files, inputEl, id,btnId) {
  492. var maxFileSize = $(inputEl).data('max-file-size');
  493. var maxFileSizeExceeded = $(inputEl).data('max-file-size-message');
  494. var sizeExceeded = false;
  495. $.each(files, function() {
  496. if (this.size && maxFileSize != null && this.size > parseInt(maxFileSize)) {
  497. sizeExceeded = true;
  498. }
  499. });
  500. if (sizeExceeded) {
  501. window.alert(maxFileSizeExceeded);
  502. } else {
  503. $.each(files, function() {
  504. addFile_board(inputEl, this, true, id,btnId);
  505. });
  506. }
  507. }
  508. function handleFileDropEvent(e) {
  509. $(this).removeClass('fileover');
  510. blockEventPropagation(e);
  511. if ($.inArray('Files', e.dataTransfer.types) > -1) {
  512. uploadAndAttachFiles(e.dataTransfer.files, $('input:file.file_selector'));
  513. }
  514. }
  515. function dragOverHandler(e) {
  516. $(this).addClass('fileover');
  517. blockEventPropagation(e);
  518. }
  519. function dragOutHandler(e) {
  520. $(this).removeClass('fileover');
  521. blockEventPropagation(e);
  522. }
  523. function setupFileDrop() {
  524. if (window.File && window.FileList && window.ProgressEvent && window.FormData) {
  525. $.event.fixHooks.drop = {
  526. props: ['dataTransfer']
  527. };
  528. $('form div.box').has('input:file').each(function() {
  529. $(this).on({
  530. dragover: dragOverHandler,
  531. dragleave: dragOutHandler,
  532. drop: handleFileDropEvent
  533. });
  534. });
  535. }
  536. }
  537. $(document).ready(setupFileDrop);
  538. $(function() {
  539. $(".file_selector").each(function() {
  540. deleteallfiles = $(this).data('deleteAllFiles');
  541. var containerid = $(this).data('containerid');
  542. if (containerid == undefined) containerid = '';
  543. var count = $('#attachments_fields' + containerid + '>span').length;
  544. if (count >= 1) {
  545. $('#upload_file_count' + containerid).html("<span id=\"count\">" + count + "</span>" + $(this).data('fileCount'));
  546. var add_attachs = $('.add_attachment').filter(function(index) {
  547. return $(this).data('containerid') == containerid;
  548. });
  549. var delete_all = $('.remove_all').filter(function(index) {
  550. return $(this).data('containerid') == containerid;
  551. });
  552. if (delete_all.length < 1) {
  553. add_attachs.append($("<a>&nbsp</a>").attr({
  554. "href": "javascript:void(0)",
  555. 'class': 'remove_all',
  556. 'data-containerid': containerid,
  557. "onclick": "removeAll('" + containerid + "')"
  558. }));
  559. }
  560. }
  561. });
  562. });
  563. //课程课件
  564. function addInputFilesCourseSource(inputEl) {
  565. checkBox = arguments[1] == 'public' ? false : true;
  566. btnId = arguments[2];
  567. // var clearedFileInput = $(inputEl).clone().val('');
  568. if (inputEl.files) {
  569. // if(inputEl.files.length >= 5){
  570. // alert('一次选择的文件不能超过5个')
  571. // return;
  572. // }
  573. // upload files using ajax
  574. uploadAndAttachFilesCourseSource(inputEl.files, inputEl,checkBox,btnId);
  575. // $(inputEl).remove();
  576. } else {
  577. // browser not supporting the file API, upload on form submission
  578. var attachmentId;
  579. var aFilename = inputEl.value.split(/\/|\\/);
  580. var count = $('#attachments_fields>span').length;
  581. attachmentId = addFile(inputEl, {
  582. name: aFilename[aFilename.length - 1]
  583. }, false);
  584. if (attachmentId) {
  585. $(inputEl).attr({
  586. name: 'attachments[' + attachmentId + '][file]'
  587. }).hide();
  588. if (count <= 0) count = 1;
  589. $('#upload_file_count').html("<span id=\"count\">" + count + "</span>" + $(inputEl).data('fileCount'));
  590. }
  591. }
  592. //clearedFileInput.insertAfter('#attachments_fields');
  593. }
  594. function uploadAndAttachFilesCourseSource(files, inputEl,checkBox,btnId) {
  595. var maxFileSize = $(inputEl).data('max-file-size');
  596. var maxFileSizeExceeded = $(inputEl).data('max-file-size-message');
  597. var sizeExceeded = false;
  598. $.each(files, function() {
  599. if (this.size && maxFileSize != null && this.size > parseInt(maxFileSize)) {
  600. sizeExceeded = true;
  601. }
  602. });
  603. if (sizeExceeded) {
  604. window.alert(maxFileSizeExceeded);
  605. } else {
  606. $.each(files, function() {
  607. addFileCourseSource(inputEl, this, true,checkBox,btnId);
  608. });
  609. }
  610. }
  611. function addFileCourseSource(inputEl, file, eagerUpload,checkBox,btnId) {
  612. var attachments_frame = '#attachments_fields';
  613. if ($(attachments_frame).children().length < 30) {
  614. deleteallfiles = $(inputEl).data('deleteAllFiles');
  615. var attachmentId = addFile.nextAttachmentId++;
  616. var fileSpan = $('<span>', {
  617. 'id': 'attachments_' + attachmentId,
  618. 'class': 'attachment'
  619. });
  620. //alert(checkBox);
  621. if(checkBox){
  622. fileSpan.append(
  623. $('<input>', {
  624. 'type': 'text',
  625. 'class': 'upload_filename readonly',
  626. 'name': 'attachments[' + attachmentId + '][filename]',
  627. 'readonly': 'readonly'
  628. }).val(file.name),
  629. // $('<input>', {
  630. // 'type': 'text',
  631. // 'class': 'description',
  632. // 'name': 'attachments[' + attachmentId + '][description]',
  633. // 'maxlength': 254,
  634. // 'placeholder': $(inputEl).data('descriptionPlaceholder')
  635. // }).toggle(!eagerUpload),
  636. $('<div>', {
  637. 'class': 'div_attachments',
  638. 'name': 'div_' + 'attachments_' + attachmentId
  639. })
  640. ).appendTo('#attachments_fields');
  641. }else {
  642. fileSpan.append(
  643. $('<input>', {
  644. 'type': 'text',
  645. 'class': 'upload_filename readonly',
  646. 'name': 'attachments[' + attachmentId + '][filename]',
  647. 'readonly': 'readonly'
  648. }).val(file.name),
  649. // $('<input>', {
  650. // 'type': 'text',
  651. // 'class': 'description',
  652. // 'name': 'attachments[' + attachmentId + '][description]',
  653. // 'maxlength': 254,
  654. // 'placeholder': $(inputEl).data('descriptionPlaceholder')
  655. // }).toggle(!eagerUpload),
  656. // $('<span >' + $(inputEl).data('fieldIsPublic') + ':</span>').attr({
  657. // 'class': 'ispublic-label'
  658. // }),
  659. // $('<input>', {
  660. // 'type': 'checkbox',
  661. // 'class': 'is_public_checkbox',
  662. // 'value': 1,
  663. // 'name': 'attachments[' + attachmentId + '][is_public_checkbox]',
  664. // checked: 'checked'
  665. // }).toggle(!eagerUpload),
  666. $('<a>&nbsp</a>').attr({
  667. 'href': "#",
  668. 'class': 'remove-upload'
  669. }).click(function () {
  670. if (confirm($(inputEl).data('areYouSure'))) {
  671. removeFile();
  672. if (!eagerUpload) {
  673. (function (e) {
  674. reload(e);
  675. })(fileSpan);
  676. }
  677. }
  678. }).toggle(!eagerUpload),
  679. $('<div>', {
  680. 'class': 'div_attachments',
  681. 'name': 'div_' + 'attachments_' + attachmentId
  682. })
  683. ).appendTo('#attachments_fields');
  684. }
  685. if (eagerUpload) {
  686. ajaxUpload(file, attachmentId, fileSpan, inputEl,btnId);
  687. }
  688. return attachmentId;
  689. }
  690. return null;
  691. }
  692. addFileCourseSource.nextAttachmentId = 1;

软件技术创新是当今国家整体创新能力的重要基础和发展引擎。近年来,大众参与的软件创新与创业活动已经成为网络时代软件开发和应用的新形态,正在快速改变着全球软件创新模式和软件产业格局。 ...