Introduction
I have a “filefield” or “fileuploadfield” in ExtJS, and I’d like to read directly its content directly into the browser, when user selects a file.
It is useful when you’d like to display an image for example, as soon as the user selects it.
How To Proceed
First of all, you need to declare the “filefield” with a listener on “change” event. For example:
{ xtype: 'filefield' ,fieldLabel: "File: " ,labelWidth: 70 ,anchor: '100%' ,buttonOnly: true // hide text field below button ,buttonText: "Select a picture" ,listeners: { 'change': 'onMediaChange' } }
and in the Controller, the handler:
onMediaChange: function(widget, value, eOpts ) { var files = event.target.files self = this; // the controller if (!files || files.length == 0) return; // make sure we got something // Let's read content as file var reader = new FileReader(); reader.onload = function (e) { // image content is in e.target.result // we can then put it into img.src, for example self.lookupReference('myImg').setSrc(e.target.result); }; reader.readAsDataURL(files[0]); }
and that’s it!
You must be logged in to post a comment.