4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5 <title>The source code</title>
6 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
7 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
8 <style type="text/css">
9 .highlight { display: block; background-color: #ddd; }
11 <script type="text/javascript">
12 function highlight() {
13 document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
17 <body onload="prettyPrint(); highlight();">
18 <pre class="prettyprint lang-js"><span id='Ext-form-field-File'>/**
19 </span> * @docauthor Jason Johnston <jason@sencha.com>
21 * A file upload field which has custom styling and allows control over the button text and other
22 * features of {@link Ext.form.field.Text text fields} like {@link Ext.form.field.Text#emptyText empty text}.
23 * It uses a hidden file input element behind the scenes to allow user selection of a file and to
24 * perform the actual upload during {@link Ext.form.Basic#submit form submit}.
26 * Because there is no secure cross-browser way to programmatically set the value of a file input,
27 * the standard Field `setValue` method is not implemented. The `{@link #getValue}` method will return
28 * a value that is browser-dependent; some have just the file name, some have a full path, some use
31 * **IMPORTANT:** File uploads are not performed using normal 'Ajax' techniques; see the description for
32 * {@link Ext.form.Basic#hasUpload} for details.
37 * Ext.create('Ext.form.Panel', {
38 * title: 'Upload a Photo',
42 * renderTo: Ext.getBody(),
46 * fieldLabel: 'Photo',
51 * buttonText: 'Select Photo...'
56 * handler: function() {
57 * var form = this.up('form').getForm();
60 * url: 'photo-upload.php',
61 * waitMsg: 'Uploading your photo...',
62 * success: function(fp, o) {
63 * Ext.Msg.alert('Success', 'Your photo "' + o.result.file + '" has been uploaded.');
71 Ext.define("Ext.form.field.File", {
72 extend: 'Ext.form.field.Text',
73 alias: ['widget.filefield', 'widget.fileuploadfield'],
74 alternateClassName: ['Ext.form.FileUploadField', 'Ext.ux.form.FileUploadField', 'Ext.form.File'],
75 uses: ['Ext.button.Button', 'Ext.layout.component.field.File'],
77 <span id='Ext-form-field-File-cfg-buttonText'> /**
78 </span> * @cfg {String} buttonText
79 * The button text to display on the upload button. Note that if you supply a value for
80 * {@link #buttonConfig}, the buttonConfig.text value will be used instead if available.
82 buttonText: 'Browse...',
84 <span id='Ext-form-field-File-cfg-buttonOnly'> /**
85 </span> * @cfg {Boolean} buttonOnly
86 * True to display the file upload field as a button with no visible text field. If true, all
87 * inherited Text members will still be available.
91 <span id='Ext-form-field-File-cfg-buttonMargin'> /**
92 </span> * @cfg {Number} buttonMargin
93 * The number of pixels of space reserved between the button and the text field. Note that this only
94 * applies if {@link #buttonOnly} = false.
98 <span id='Ext-form-field-File-cfg-buttonConfig'> /**
99 </span> * @cfg {Object} buttonConfig
100 * A standard {@link Ext.button.Button} config object.
103 <span id='Ext-form-field-File-event-change'> /**
104 </span> * @event change
105 * Fires when the underlying file input field's value has changed from the user selecting a new file from the system
106 * file selection dialog.
107 * @param {Ext.ux.form.FileUploadField} this
108 * @param {String} value The file value returned by the underlying file input field
111 <span id='Ext-form-field-File-property-fileInputEl'> /**
112 </span> * @property {Ext.Element} fileInputEl
113 * A reference to the invisible file input element created for this upload field. Only populated after this
114 * component is rendered.
117 <span id='Ext-form-field-File-property-button'> /**
118 </span> * @property {Ext.button.Button} button
119 * A reference to the trigger Button component created for this upload field. Only populated after this component is
123 <span id='Ext-form-field-File-cfg-fieldBodyCls'> /**
124 </span> * @cfg {String} [fieldBodyCls='x-form-file-wrap']
125 * An extra CSS class to be applied to the body content element in addition to {@link #fieldBodyCls}.
127 fieldBodyCls: Ext.baseCSSPrefix + 'form-file-wrap',
129 <span id='Ext-form-field-File-cfg-readOnly'> /**
130 </span> * @cfg {Boolean} readOnly
131 * Unlike with other form fields, the readOnly config defaults to true in File field.
136 componentLayout: 'filefield',
139 onRender: function() {
143 me.callParent(arguments);
146 me.createFileInput();
148 // we don't create the file/button til after onRender, the initial disable() is
149 // called in the onRender of the component.
154 inputEl = me.inputEl;
155 inputEl.dom.removeAttribute('name'); //name goes on the fileInput, not the text input
157 inputEl.setDisplayed(false);
161 <span id='Ext-form-field-File-method-createButton'> /**
163 * Creates the custom trigger Button component. The fileInput will be inserted into this.
165 createButton: function() {
167 me.button = Ext.widget('button', Ext.apply({
171 cls: Ext.baseCSSPrefix + 'form-file-btn',
172 preventDefault: false,
173 style: me.buttonOnly ? '' : 'margin-left:' + me.buttonMargin + 'px'
174 }, me.buttonConfig));
177 <span id='Ext-form-field-File-method-createFileInput'> /**
179 * Creates the file input element. It is inserted into the trigger button component, made
180 * invisible, and floated on top of the button's other content so that it will receive the
183 createFileInput : function() {
185 me.fileInputEl = me.button.el.createChild({
187 cls: Ext.baseCSSPrefix + 'form-file-input',
191 }).on('change', me.onFileChange, me);
194 <span id='Ext-form-field-File-method-onFileChange'> /**
195 </span> * @private Event handler fired when the user selects a file.
197 onFileChange: function() {
198 this.lastValue = null; // force change event to get fired even if the user selects a file with the same name
199 Ext.form.field.File.superclass.setValue.call(this, this.fileInputEl.dom.value);
202 <span id='Ext-form-field-File-property-setValue'> /**
203 </span> * Overridden to do nothing
206 setValue: Ext.emptyFn,
211 me.fileInputEl.remove();
212 me.createFileInput();
213 me.inputEl.dom.value = '';
218 onDisable: function(){
223 disableItems: function(){
224 var file = this.fileInputEl,
225 button = this.button;
228 file.dom.disabled = true;
235 onEnable: function(){
238 me.fileInputEl.dom.disabled = false;
242 isFileUpload: function() {
246 extractFileInput: function() {
247 var fileInput = this.fileInputEl.dom;
252 onDestroy: function(){
253 Ext.destroyMembers(this, 'fileInputEl', 'button');