Upload.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /**
  2. * 上传文件按钮
  3. */
  4. Ext.define('erp.view.core.button.Upload',{
  5. extend: 'Ext.Button',
  6. alias: 'widget.erpUploadButton',
  7. iconCls: 'x-button-icon-add',
  8. cls: 'x-btn-gray',
  9. text: $I18N.common.button.erpUploadButton,
  10. style: {
  11. marginLeft: '10px'
  12. },
  13. width: 80,
  14. listeners: {
  15. afterrender: function(){
  16. var form = Ext.getCmp('form');
  17. form.add({
  18. xtype: 'hidden',
  19. name: 'files',
  20. fieldLabel: '附件',
  21. value: '',
  22. id: 'files'
  23. });
  24. }
  25. },
  26. handler: function(upload){
  27. var win = new Ext.window.Window({
  28. title: '上传附件',
  29. id : 'win',
  30. height: "100%",
  31. width: "80%",
  32. maximizable : true,
  33. buttonAlign : 'center',
  34. items: [{
  35. xtype: 'form',
  36. id: 'uploadform',
  37. layout: 'column',
  38. bodyStyle: {background: '#f1f1f1'},
  39. fieldDefaults: {
  40. labelAlign : "right",
  41. columnWidth: .6
  42. },
  43. items: [{
  44. xtype: 'filefield',
  45. fieldLabel: '附件',
  46. id: 'attach',
  47. name: 'file',
  48. msgTarget: 'side',
  49. allowBlank: false,
  50. buttonText: '浏览...',
  51. listeners: {
  52. afterrender: function(field){
  53. field.attachcount = 0;
  54. upload.files = new Array();
  55. },
  56. change: function(field){
  57. if(field.value != null){
  58. field.button.disable(true);
  59. var container = Ext.create('Ext.form.FieldContainer', {
  60. layout: 'hbox',
  61. fieldLabel: "附件" + (field.attachcount + 1),
  62. items: [{
  63. xtype: 'textfield',
  64. id: 'attach' + field.attachcount,
  65. flex: 1
  66. }, {
  67. xtype: 'button',
  68. text: '上传',
  69. id: 'upload' + field.attachcount,
  70. handler: function(btn){
  71. var form = btn.ownerCt.ownerCt;
  72. var f = Ext.getCmp(btn.id.replace('upload', 'attach'));
  73. if(f.value != null && f.value != ''){
  74. //field.value = f.value;
  75. form.getForm().submit({
  76. url: basePath + 'common/upload.action?em_code=' + em_code,
  77. waitMsg: "正在上传:" + f.value,
  78. success: function(fp, o){
  79. if(o.result.error){
  80. showError(o.result.error);
  81. } else {
  82. Ext.Msg.alert("恭喜", f.value + " 上传成功!");
  83. btn.setText("上传成功(" + Ext.util.Format.fileSize(o.result.size) + ")");
  84. btn.disable(true);
  85. field.button.setDisabled(false);
  86. Ext.getCmp('files').setValue(Ext.getCmp('files').value + ',' + o.result.filepath);
  87. upload.files[Number(btn.id.replace('upload', ''))] = o.result.filepath;
  88. }
  89. }
  90. });
  91. }
  92. },
  93. flex: 1
  94. }, {
  95. xtype: 'button',
  96. text: '删除',
  97. id: 'delete' + field.attachcount,
  98. handler: function(btn){
  99. var f = Ext.getCmp(btn.id.replace('delete', 'attach'));
  100. if(f.value != null && f.value != ''){
  101. Ext.getCmp('files').setValue(Ext.getCmp('files').value.replace(upload.files[Number(btn.id.replace('delete', ''))], ''));
  102. upload.files[Number(btn.id.replace('delete', ''))] = '';
  103. }
  104. btn.ownerCt.destroy(true);
  105. field.attachcount--;
  106. },
  107. flex: 1
  108. }]
  109. });
  110. if(contains(field.value, "\\", true)){
  111. Ext.getCmp('attach' + field.attachcount).setValue(field.value.substring(field.value.lastIndexOf('\\') + 1));
  112. } else {
  113. Ext.getCmp('attach' + field.attachcount).setValue(field.value.substring(field.value.lastIndexOf('/') + 1));
  114. }
  115. Ext.getCmp('uploadform').insert(1, container);
  116. field.attachcount++;
  117. field.button.setText("继续...");
  118. }
  119. }
  120. }
  121. },{
  122. xtype: 'displayfield',
  123. id: 'attachs',
  124. cls: 'mail-attach',
  125. height: 'auto'
  126. }]
  127. }],
  128. buttons: [{
  129. text: $I18N.common.button.erpCloseButton,
  130. iconCls: 'x-button-icon-close',
  131. cls: 'x-btn-gray',
  132. handler: function(){
  133. Ext.getCmp('win').close();
  134. }
  135. }]
  136. });
  137. win.show();
  138. },
  139. initComponent : function(){
  140. this.callParent(arguments);
  141. }
  142. });