file-upload.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. Ext.require([
  2. 'Ext.form.field.File',
  3. 'Ext.form.Panel',
  4. 'Ext.window.MessageBox'
  5. ]);
  6. Ext.onReady(function() {
  7. // Class which shows invisible file input field.
  8. if (window.location.href.indexOf('debug') !== -1) {
  9. Ext.getBody().addCls('x-debug');
  10. }
  11. var msg = function(title, msg) {
  12. Ext.Msg.show({
  13. title: title,
  14. msg: msg,
  15. minWidth: 200,
  16. modal: true,
  17. icon: Ext.Msg.INFO,
  18. buttons: Ext.Msg.OK
  19. });
  20. };
  21. var fibasic = Ext.create('Ext.form.field.File', {
  22. renderTo: 'fi-basic',
  23. width: 400,
  24. hideLabel: true
  25. });
  26. Ext.create('Ext.button.Button', {
  27. text: 'Get File Path',
  28. renderTo: 'fi-basic-btn',
  29. handler: function(){
  30. var v = fibasic.getValue();
  31. msg('Selected File', v && v != '' ? v : 'None');
  32. }
  33. });
  34. Ext.create('Ext.form.field.File', {
  35. renderTo: 'fi-button',
  36. buttonOnly: true,
  37. hideLabel: true,
  38. listeners: {
  39. 'change': function(fb, v){
  40. var el = Ext.get('fi-button-msg');
  41. el.update('<b>Selected:</b> '+v);
  42. if(!el.isVisible()){
  43. el.slideIn('t', {
  44. duration: 200,
  45. easing: 'easeIn',
  46. listeners: {
  47. afteranimate: function() {
  48. el.highlight();
  49. el.setWidth(null);
  50. }
  51. }
  52. });
  53. }else{
  54. el.highlight();
  55. }
  56. }
  57. }
  58. });
  59. Ext.create('Ext.form.Panel', {
  60. renderTo: 'fi-form',
  61. width: 500,
  62. frame: true,
  63. title: 'File Upload Form',
  64. bodyPadding: '10 10 0',
  65. defaults: {
  66. anchor: '100%',
  67. allowBlank: false,
  68. msgTarget: 'side',
  69. labelWidth: 50
  70. },
  71. items: [{
  72. xtype: 'textfield',
  73. fieldLabel: 'Name'
  74. },{
  75. xtype: 'filefield',
  76. id: 'form-file',
  77. emptyText: 'Select an image',
  78. fieldLabel: 'Photo',
  79. name: 'photo-path',
  80. buttonText: '',
  81. buttonConfig: {
  82. iconCls: 'upload-icon'
  83. }
  84. }],
  85. buttons: [{
  86. text: 'Save',
  87. handler: function(){
  88. var form = this.up('form').getForm();
  89. if(form.isValid()){
  90. form.submit({
  91. url: 'file-upload.php',
  92. waitMsg: 'Uploading your photo...',
  93. success: function(fp, o) {
  94. msg('Success', 'Processed file "' + o.result.file + '" on the server');
  95. }
  96. });
  97. }
  98. }
  99. },{
  100. text: 'Reset',
  101. handler: function() {
  102. this.up('form').getForm().reset();
  103. }
  104. }]
  105. });
  106. });