3 This file is part of Ext JS 4
5 Copyright (c) 2011 Sencha Inc
7 Contact: http://www.sencha.com/contact
9 GNU General Public License Usage
10 This file may be used under the terms of the GNU General Public License version 3.0 as published by the Free Software Foundation and appearing in the file LICENSE included in the packaging of this file. Please review the following information to ensure the GNU General Public License version 3.0 requirements will be met: http://www.gnu.org/copyleft/gpl.html.
12 If you are unsure which license is appropriate for your use, please contact the sales department at http://www.sencha.com/contact.
16 * @class Ext.form.field.File
17 * @extends Ext.form.field.Text
19 A file upload field which has custom styling and allows control over the button text and other
20 features of {@link Ext.form.field.Text text fields} like {@link Ext.form.field.Text#emptyText empty text}.
21 It uses a hidden file input element behind the scenes to allow user selection of a file and to
22 perform the actual upload during {@link Ext.form.Basic#submit form submit}.
24 Because there is no secure cross-browser way to programmatically set the value of a file input,
25 the standard Field `setValue` method is not implemented. The `{@link #getValue}` method will return
26 a value that is browser-dependent; some have just the file name, some have a full path, some use
28 {@img Ext.form.File/Ext.form.File.png Ext.form.File component}
31 Ext.create('Ext.form.Panel', {
32 title: 'Upload a Photo',
36 renderTo: Ext.getBody(),
45 buttonText: 'Select Photo...'
51 var form = this.up('form').getForm();
54 url: 'photo-upload.php',
55 waitMsg: 'Uploading your photo...',
56 success: function(fp, o) {
57 Ext.Msg.alert('Success', 'Your photo "' + o.result.file + '" has been uploaded.');
66 * @docauthor Jason Johnston <jason@sencha.com>
68 Ext.define("Ext.form.field.File", {
69 extend: 'Ext.form.field.Text',
70 alias: ['widget.filefield', 'widget.fileuploadfield'],
71 alternateClassName: ['Ext.form.FileUploadField', 'Ext.ux.form.FileUploadField', 'Ext.form.File'],
72 uses: ['Ext.button.Button', 'Ext.layout.component.field.File'],
75 * @cfg {String} buttonText The button text to display on the upload button (defaults to
76 * 'Browse...'). Note that if you supply a value for {@link #buttonConfig}, the buttonConfig.text
77 * value will be used instead if available.
79 buttonText: 'Browse...',
82 * @cfg {Boolean} buttonOnly True to display the file upload field as a button with no visible
83 * text field (defaults to false). If true, all inherited Text members will still be available.
88 * @cfg {Number} buttonMargin The number of pixels of space reserved between the button and the text field
89 * (defaults to 3). Note that this only applies if {@link #buttonOnly} = false.
94 * @cfg {Object} buttonConfig A standard {@link Ext.button.Button} config object.
99 * Fires when the underlying file input field's value has changed from the user
100 * selecting a new file from the system file selection dialog.
101 * @param {Ext.ux.form.FileUploadField} this
102 * @param {String} value The file value returned by the underlying file input field
106 * @property fileInputEl
107 * @type {Ext.core.Element}
108 * A reference to the invisible file input element created for this upload field. Only
109 * populated after this component is rendered.
114 * @type {Ext.button.Button}
115 * A reference to the trigger Button component created for this upload field. Only
116 * populated after this component is rendered.
120 * @cfg {String} fieldBodyCls
121 * An extra CSS class to be applied to the body content element in addition to {@link #fieldBodyCls}.
122 * Defaults to 'x-form-file-wrap' for file upload field.
124 fieldBodyCls: Ext.baseCSSPrefix + 'form-file-wrap',
129 componentLayout: 'filefield',
132 onRender: function() {
136 me.callParent(arguments);
139 me.createFileInput();
141 // we don't create the file/button til after onRender, the initial disable() is
142 // called in the onRender of the component.
147 inputEl = me.inputEl;
148 inputEl.dom.removeAttribute('name'); //name goes on the fileInput, not the text input
150 inputEl.setDisplayed(false);
156 * Creates the custom trigger Button component. The fileInput will be inserted into this.
158 createButton: function() {
160 me.button = Ext.widget('button', Ext.apply({
163 cls: Ext.baseCSSPrefix + 'form-file-btn',
164 preventDefault: false,
165 style: me.buttonOnly ? '' : 'margin-left:' + me.buttonMargin + 'px'
166 }, me.buttonConfig));
171 * Creates the file input element. It is inserted into the trigger button component, made
172 * invisible, and floated on top of the button's other content so that it will receive the
175 createFileInput : function() {
177 me.fileInputEl = me.button.el.createChild({
179 cls: Ext.baseCSSPrefix + 'form-file-input',
183 }).on('change', me.onFileChange, me);
187 * @private Event handler fired when the user selects a file.
189 onFileChange: function() {
190 this.lastValue = null; // force change event to get fired even if the user selects a file with the same name
191 Ext.form.field.File.superclass.setValue.call(this, this.fileInputEl.dom.value);
195 * Overridden to do nothing
198 setValue: Ext.emptyFn,
201 this.fileInputEl.remove();
202 this.createFileInput();
206 onDisable: function(){
211 disableItems: function(){
212 var file = this.fileInputEl,
213 button = this.button;
216 file.dom.disabled = true;
223 onEnable: function(){
226 me.fileInputEl.dom.disabled = false;
230 isFileUpload: function() {
234 extractFileInput: function() {
235 var fileInput = this.fileInputEl.dom;
240 onDestroy: function(){
241 Ext.destroyMembers(this, 'fileInputEl', 'button');