FileField.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /**
  2. * 多文件上传下载
  3. */
  4. Ext.define('erp.view.core.form.FileField', {
  5. extend: 'Ext.form.FieldSet',
  6. alias: 'widget.mfilefield',
  7. minHeight: 22,
  8. collapsible: true,
  9. // title: '<img src="' + basePath + 'resource/images/icon/clip.png" width=20 height=20/>附件',
  10. style: 'background:#f1f1f1;',
  11. // margin: '2 2 2 2',
  12. filesize: 0,//附件总大小
  13. multi: true,
  14. initComponent: function() {
  15. this.columnWidth = 1;//强制占一行
  16. this.cls = '';
  17. this.title = this.title || '附件';
  18. this._title = this.title;
  19. this.callParent(arguments);
  20. this.items.items[0].name = this.name;
  21. },
  22. layout:'column',
  23. items: [{
  24. xtype: 'hidden',//该隐藏字段的值(附件在FilePath表的ID,用;隔开)将被保存到数据库
  25. value: '',
  26. fieldLabel: '附件'
  27. },{
  28. xtype: 'form',
  29. columnWidth: 1,
  30. frame: false,
  31. border: false,
  32. minHeight: 22,
  33. bodyStyle: 'background:#f1f1f1;padding:2px;',
  34. layout: 'hbox',
  35. items: [{
  36. xtype: 'filefield',
  37. name: 'file',
  38. buttonText: '浏览<font color=blue size=1>(≤100M)</font>...',
  39. buttonOnly: true,
  40. hideLabel: true,
  41. listeners: {
  42. change: function(field){
  43. if(field.value != null){
  44. field.ownerCt.ownerCt.upload(field.ownerCt, field);
  45. }
  46. }
  47. }
  48. }]
  49. }],
  50. setValue: function(value){
  51. this.value = value;
  52. },
  53. listeners : {
  54. afterrender: function(f){
  55. var form = f.ownerCt;
  56. if(f.value != null && f.value.toString().trim() != ''){
  57. f.download(f.value);
  58. }
  59. if(form.keyField){
  60. var keyField = form.down('field[name=' + form.keyField + ']');
  61. if(keyField) {
  62. keyField.on('change', function(f, nv, ov){
  63. if(ov){
  64. Ext.each(f.items.items, function(item, index){
  65. if(index > 1){
  66. item.destroy();
  67. }
  68. });
  69. if(f.value != null && f.value.toString().trim() != ''){
  70. f.download(f.value);
  71. }
  72. }
  73. });
  74. }
  75. }
  76. f.setReadOnly(f.readOnly);
  77. }
  78. },
  79. /**
  80. * 上传附件
  81. */
  82. upload: function(form, field){
  83. var me = this;
  84. var filename = '';
  85. if(contains(field.value, "\\", true)){
  86. filename = field.value.substring(field.value.lastIndexOf('\\') + 1);
  87. } else {
  88. filename = field.value.substring(field.value.lastIndexOf('/') + 1);
  89. }
  90. form.getForm().submit({
  91. url: basePath + 'common/upload.action?em_code=' + em_code,
  92. waitMsg: "正在上传:" + filename,
  93. success: function(fp, o){
  94. if(o.result.error){
  95. showError(o.result.error);
  96. } else {
  97. Ext.Msg.alert("恭喜", filename + " 上传成功!");
  98. me.filesize += o.result.size;
  99. me.setTitle('<img src="' + basePath + 'resource/images/icon/clip.png" width=20 height=20/>' + me._title +
  100. '(总大小:' + Ext.util.Format.fileSize(me.filesize) + ")");
  101. var name = filename + " (" + Ext.util.Format.fileSize(o.result.size) + ")";
  102. var w = Math.min((me.getStrLength(name) + 10) / 200, .8);
  103. var field = Ext.create('erp.view.core.trigger.TrashField', {
  104. fileName: filename,
  105. value: name,
  106. columnWidth: w,
  107. readOnly: false,
  108. editable: false,
  109. filepath: o.result.filepath,
  110. filesize: o.result.size,
  111. realpath: o.result.path,
  112. fieldStyle: 'background:#E0EEEE;'
  113. });
  114. if(!me.multi)
  115. me.clearAll();
  116. me.add(field);
  117. var val = me.down('hidden').value + o.result.filepath + ';';
  118. me.down('hidden').setValue(val);
  119. // 单据已经存在的情况下,直接修改attach字段
  120. var form = me.ownerCt, kf;
  121. if(form && form.keyField && (kf = form.down('field[name=' + form.keyField + ']')) != null
  122. && kf.getValue() != null && kf.getValue() != 0) {
  123. field.updateAttachField(val, '添加附件');
  124. }
  125. }
  126. }
  127. });
  128. },
  129. /**
  130. * 根据id读取对应PATH
  131. * @param id{String} fp_id
  132. */
  133. download: function(id){
  134. var me = this;
  135. var files = new Array();
  136. Ext.Ajax.request({
  137. url : basePath + 'common/getFilePaths.action',
  138. async: false,
  139. params: {
  140. id: id
  141. },
  142. method : 'post',
  143. callback : function(options,success,response){
  144. var res = new Ext.decode(response.responseText);
  145. if(res.exception || res.exceptionInfo){
  146. showError(res.exceptionInfo);
  147. return;
  148. }
  149. files = res.files != null ? res.files : [];
  150. }
  151. });
  152. Ext.each(files, function(f){
  153. var path = f.fp_path;
  154. var fileName = f.fp_name;
  155. if(!fileName) {
  156. if(contains(path, '\\', true)){
  157. fileName = path.substring(path.lastIndexOf('\\') + 1);
  158. } else {
  159. fileName = path.substring(path.lastIndexOf('/') + 1);
  160. }
  161. }
  162. me.filesize += f.fp_size;
  163. me.setTitle('<img src="' + basePath + 'resource/images/icon/clip.png" width=20 height=20/>' + me._title +
  164. '(总大小:' + Ext.util.Format.fileSize(me.filesize) + ")");
  165. var name = fileName + " (" + Ext.util.Format.fileSize(f.fp_size) + ")";
  166. var w = Math.min((me.getStrLength(name) + 10) / 200, .8);
  167. me.addItem(Ext.create('erp.view.core.trigger.TrashField', {
  168. fileName: fileName,
  169. value: name,
  170. columnWidth: w,
  171. readOnly: false,
  172. editable: false,
  173. filepath: f.fp_id,
  174. filesize: f.fp_size,
  175. realpath: path,
  176. fieldStyle: 'background:#E0EEEE;'
  177. }));
  178. me.down('hidden').setValue(me.down('hidden').value + f.fp_id + ';');
  179. me.down('hidden').originalValue = me.down('hidden').value;
  180. });
  181. },
  182. addItem: function(item){
  183. this.add(item);
  184. },
  185. setReadOnly: function(bool){
  186. //取消附件上传限制 改为记录日志
  187. /* this.down('filefield').setDisabled(bool);
  188. if(this.items && this.items.items.length>2){
  189. Ext.Array.each(this.items.items,function(item,index){
  190. if(index>1){
  191. item.resetField(bool);
  192. }
  193. });
  194. }*/
  195. /*this.down('filefield').button.setDisabled(bool);*/
  196. },
  197. setFieldStyle: function(str) {
  198. },
  199. getStrLength: function(str) {
  200. for (var len = str.length, c = 0, i = 0; i < len; i++)
  201. str.charCodeAt(i) < 27 || str.charCodeAt(i) > 126 ? c += 2 : c++;
  202. return c;
  203. },
  204. clearAll: function() {
  205. var me = this, items = me.query('trashfield');
  206. Ext.Array.each(items, function(item){
  207. me.remove(item);
  208. });
  209. me.down('hidden').setValue('');
  210. me.filesize = 0;
  211. }
  212. });