Skip to content

Commit

Permalink
Backoffice. Cetera.window.Upload added progress indicator
Browse files Browse the repository at this point in the history
  • Loading branch information
nicodim99 committed May 1, 2017
1 parent 1f9ce2b commit 07b2117
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 24 deletions.
7 changes: 6 additions & 1 deletion cms/app/fileselect/Panel.js
Original file line number Diff line number Diff line change
Expand Up @@ -662,10 +662,15 @@ Ext.define('Cetera.fileselect.Panel', {
},
failure: function(resp) {
if (files_reload && path == me.path) me.reloadFiles();
progress.updateProgress( 0, prefix + ' - ' + _('Ошибка! (Превышен размер файла?)') );

setTimeout(function(){
me.centerPanel.remove(progress, true);
},5000);

var msg = _('Ошибка!');
var o = Ext.decode(resp.responseText);
if (o.message) msg += ' '+o.message;
progress.updateProgress( 0, prefix + ' - ' + msg );
},
uploadProgress: function(e) {
progress.updateProgress( e.loaded/e.total, prefix + ' - ' + parseInt(e.loaded*100/e.total)+'%' );
Expand Down
59 changes: 38 additions & 21 deletions cms/app/window/Upload.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
Ext.define('Cetera.window.Upload', {
extend: 'Ext.Window',

requires: 'Cetera.Ajax',

layout:'fit',
width:500,
Expand All @@ -10,15 +12,15 @@ Ext.define('Cetera.window.Upload', {

initComponent : function(){
if (this.showPath) {
this.height = 140;
this.height = 150;
this.pathField = new Ext.form.TextField({
fieldLabel: Config.Lang.uploadCat,
name: 'path',
value: this.path,
allowBlank: false
});
} else {
this.height = 110;
this.height = 120;
this.pathField = new Ext.form.Hidden({
name: 'path',
value: this.path
Expand All @@ -33,6 +35,8 @@ Ext.define('Cetera.window.Upload', {
allowBlank: false,
buttonText: Config.Lang.chooseFile
});

this.progress = Ext.create('Ext.ProgressBar');

this.uploadForm = new Ext.FormPanel({
fileUpload: true,
Expand All @@ -47,7 +51,7 @@ Ext.define('Cetera.window.Upload', {
anchor: '0',
allowBlank: false
},
items: [this.pathField,this.fileField]
items: [this.pathField,this.fileField, this.progress]
});

this.buttons = [{
Expand All @@ -74,24 +78,37 @@ Ext.define('Cetera.window.Upload', {
this.pathField.setValue(this.pathField.getValue()+'/');
if (this.pathField.getValue().substr(0,1) != '/')
this.pathField.setValue('/'+this.pathField.getValue());
var cmp = this;
this.uploadForm.getForm().submit({
url: '/cms/include/action_files.php',
params: {
action: 'upload',
showPath: this.showPath
},
waitMsg: Config.Lang.wait,
success: function(fp, o){
cmp.hide();
cmp.fireEvent('successUpload', {file: o.result.file, path: cmp.pathField.getValue()});
cmp.fileField.reset();
},
failure: function(fp, o){
var obj = Ext.decode(o.response.responseText);
if (obj.message) Ext.MessageBox.alert(Config.Lang.error, obj.message);
}
});

var me = this;
var formData = new FormData();
var file = this.fileField.fileInputEl.dom.files[0];

formData.append('file', file);
var prefix = _('Загрузка')+': '+file.name;

Cetera.Ajax.request({
url: 'include/action_files.php?action=upload&path='+this.pathField.getValue(),
method: 'POST',
rawData: formData,
ignoreHeaders: true,
success: function(resp) {
var o = Ext.decode(resp.responseText);
me.progress.updateProgress(0,' ');
me.hide();
me.fireEvent('successUpload', {file: o.file, path: o.path});
me.fileField.reset();
},
failure: function(resp) {
var msg = _('Ошибка!');
var o = Ext.decode(resp.responseText);
if (o.message) msg += ' '+o.message;
me.progress.updateProgress( 0, prefix + ' - ' + msg );
},
uploadProgress: function(e) {
me.progress.updateProgress( e.loaded/e.total, prefix + ' - ' + parseInt(e.loaded*100/e.total)+'%' );
},
scope: me
});
}
},

Expand Down
12 changes: 10 additions & 2 deletions cms/include/action_files.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,15 @@ function check_file($file, $readonly = false) {
}

if ($_REQUEST['action'] == 'upload') {

$_SERVER['HTTP_X_REQUESTED_WITH'] = 'xmlhttprequest';

if ($_FILES['file']['error']) {
if (!isset($_FILES['file'])) {

throw new \Exception( $translator->_('Нет файла') );

}
elseif ($_FILES['file']['error']) {
switch ($_FILES['file']['error']) {
case UPLOAD_ERR_INI_SIZE:
case UPLOAD_ERR_FORM_SIZE:
Expand All @@ -161,7 +168,8 @@ function check_file($file, $readonly = false) {
$res['success'] = false;
$res['message'] = $msg;

} else {
}
else {

$path = trim($_REQUEST['path'],'/').'/';

Expand Down

0 comments on commit 07b2117

Please sign in to comment.