Ext.define('treps.controller.Drop.FileDrop', { extend: 'Ext.app.Controller', views: [ 'Steps.SourceSelection.FileDropZone' ], refs: [ { ref: 'dropZone', selector: 'filedropzone' } ], init: function() { var me = this; this.control({ 'filedropzone': { afterrender: me.onInitDropZone, scope: me } }); }, onInitDropZone: function() { var me = this; var dropZone = this.getDropZone(); var documentDropTargetEl = dropZone.getEl().dom; if (typeof(File) === 'undefined' || typeof(FileReader) === 'undefined') { dropZone.update("Drag & Drop Feature not supported by your browser"); return; } documentDropTargetEl.addEventListener('dragenter', function (evt) { dropZone.getEl().highlight(); evt.stopPropagation(); evt.preventDefault(); }, false); documentDropTargetEl.addEventListener('dragover', function (evt) { evt.stopPropagation(); evt.preventDefault(); }, false); documentDropTargetEl.addEventListener('drop', function (evt) { var files = evt.target.files || evt.dataTransfer.files; evt.stopPropagation(); evt.preventDefault(); if (!files || files.length == 0) { treps.Messages.showError("Error to retrieve the dragged file."); return false; } if (files.length > 1) { treps.Messages.showError("Please select only one file."); return false; } var file = files[0]; if (file.size > treps.Constants.DROP_MAX_FILESIZE) { treps.Messages.showError("File too large (limitation to "+treps.Constants.DROP_MAX_FILESIZE+" octets)"); return false; } var filenameBase = file.name.substring(0,file.name.lastIndexOf('.')); treps.model.Session.set('in_filenameBase',filenameBase); var reader = new FileReader(); reader.onload = function (event) { me.application.fireEvent('receivesource','data', event.target.result); }; try { reader.readAsDataURL(file); } catch (err) { treps.Messages.showError(err.message); } }, false); } });