FileField.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. autoUpdate:true,//添加属性是否自动更新
  13. filesize: 0,//附件总大小
  14. initComponent: function() {
  15. this.columnWidth = 1;//强制占一行
  16. this.cls = '';
  17. this.callParent(arguments);
  18. this.items.items[0].name = this.name;
  19. },
  20. layout:'column',
  21. items: [{
  22. xtype: 'hidden',//该隐藏字段的值(附件在FilePath表的ID,用;隔开)将被保存到数据库
  23. value: '',
  24. fieldLabel: '附件'
  25. },{
  26. xtype: 'form',
  27. columnWidth: 1,
  28. frame: false,
  29. border: false,
  30. minHeight: 22,
  31. bodyStyle: 'background:#f1f1f1;',
  32. layout: 'hbox',
  33. items: [{
  34. xtype: 'filefield',
  35. name: 'file',
  36. buttonText: '浏览<font color=blue size=1>(≤100M)</font>...',
  37. buttonOnly: true,
  38. hideLabel: true,
  39. listeners: {
  40. change: function(field){
  41. if(field.value != null){
  42. field.ownerCt.ownerCt.upload(field.ownerCt, field);
  43. }
  44. }
  45. }
  46. }]
  47. }],
  48. setValue: function(value){
  49. this.value = value;
  50. if(Ext.isEmpty(value))
  51. this.down('hidden').setValue('');
  52. else if(this.items && this.items.items.length==2){
  53. this.download(value);
  54. }
  55. },
  56. listeners : {
  57. afterrender: function(f){
  58. var form = f.ownerCt;
  59. if(f.value != null && f.value.toString().trim() != ''){
  60. f.download(f.value);
  61. }
  62. if(form.keyField){
  63. Ext.getCmp(form.keyField).on('change', function(){
  64. Ext.defer(function(){
  65. Ext.each(f.items.items, function(item, index){
  66. if(index > 1){
  67. item.destroy();
  68. }
  69. });
  70. if(f.value != null && f.value.toString().trim() != ''){
  71. f.download(f.value);
  72. }
  73. }, 50);
  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. me.fireEvent('afterupload', me, filename);
  98. Ext.Msg.alert("恭喜", filename + " 上传成功!");
  99. me.filesize += o.result.size;
  100. me.setTitle('<img src="' + basePath + 'resource/images/icon/clip.png" width=20 height=20/>附件' +
  101. '(总大小:' + Ext.util.Format.fileSize(me.filesize) + ")");
  102. var name = filename + " (" + Ext.util.Format.fileSize(o.result.size) + ")";
  103. var w = Math.min((me.getStrLength(name) + 10) / 200, .8);
  104. var field = Ext.create('erp.view.core.trigger.TrashField', {
  105. fileName: filename,
  106. value: name,
  107. columnWidth: w,
  108. readOnly: false,
  109. editable: false,
  110. filepath: o.result.filepath,
  111. filesize: o.result.size,
  112. realpath: o.result.path,
  113. fieldStyle: 'background:#E0EEEE;'
  114. });
  115. me.add(field);
  116. var val = me.down('hidden').value + o.result.filepath + ';';
  117. me.down('hidden').setValue(val);
  118. // 单据已经存在的情况下,直接修改attach字段
  119. var form = me.ownerCt, kf;
  120. if(form && form.keyField && (kf = Ext.getCmp(form.keyField)) != null
  121. && kf.getValue() != null && kf.getValue() != 0 && me.autoUpdate) {
  122. field.updateAttachField(val, '添加附件');
  123. }
  124. }
  125. }
  126. });
  127. },
  128. /**
  129. * 根据id读取对应PATH
  130. * @param id{String} fp_id
  131. */
  132. download: function(id){
  133. var me = this;
  134. var files = new Array();
  135. Ext.Ajax.request({
  136. url : basePath + 'common/getFilePaths.action',
  137. async: false,
  138. params: {
  139. id: id,
  140. itemId:me.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/>附件' +
  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. /*this.down('filefield').setDisabled(bool);*/
  187. },
  188. setFieldStyle: function(str) {
  189. },
  190. getStrLength: function(str) {
  191. for (var len = str.length, c = 0, i = 0; i < len; i++)
  192. str.charCodeAt(i) < 27 || str.charCodeAt(i) > 126 ? c += 2 : c++;
  193. return c;
  194. }
  195. });