Skip to content

Instantly share code, notes, and snippets.

@dyp2000
Last active July 3, 2018 12:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dyp2000/63af070d0602e016a69421b8513ad9f1 to your computer and use it in GitHub Desktop.
Save dyp2000/63af070d0602e016a69421b8513ad9f1 to your computer and use it in GitHub Desktop.
ExtJS + PHP
# Client part
//
// !Columns
//
var xrep_Columns = new Ext.grid.ColumnModel({
defaults: {
sortable: false
},
columns: [{
id: 'xrep_id',
header: '№',
dataIndex: 'id',
width: 35,
resizable:false
},{
id: 'xrep_name',
header: 'Название отчета',
dataIndex: 'name',
width: 450,
editor: new Ext.form.TextField({allowBlank: false})
},{
id: 'xrep_file_name',
header: 'Имя файла',
dataIndex: 'file_name',
width: 450,
editor: new Ext.ux.form.FileUploadField({
allowBlank: false,
id: 'fileUploadButton',
buttonText: 'Шаблон',
listeners: {
'fileselected': function (fp, filename) {
var file = Ext.get('fileUploadButton-file').dom.files[0];
Ext.get('fileUploadButton').dom.value = file.name;
}
}
})
},{
id: 'xrep_description',
header: 'Описание',
dataIndex: 'description',
width: 40,
resizable:false,
editor: new Ext.form.TextField({allowBlank: true})
}
]
});
// Row Editor
var xreport_EditorRow = new Ext.ux.grid.RowEditor({
saveText: 'Сохранить',
cancelText: 'Отмена',
errorText: 'ОШИБКА',
commitChangesText : 'Вы должны сохранить или отменить изменения!',
errorSummary: false,
listeners: {
'canceledit': function (editor, flag) {
var delRec = editor.record;
if (!delRec) {
return false;
}
reportsTemplatesGrid.getStore().remove(delRec);
reportsTemplatesGrid.getStore().reload();
},
'afteredit': function() {
var file = Ext.get('fileUploadButton-file').dom.files[0];
var fileReader = new FileReader();
fileReader.onload = function(e) {
Ext.Ajax.request({
isUpload: true,
method: 'POST',
url: 'xreport-upload.php',
params: {
file: e.target.result,
fileName: file.name
},
success: function(resp, oper) {
Ext.Msg.show({
icon: Ext.Msg.INFO,
title: 'Загрузка файла',
msg: 'Файл успешно загружен на сервер',
buttons: Ext.Msg.OK
});
reportsTemplatesGrid.getStore().reload();
},
failure: function (resp, oper) {
Ext.Msg.show({
icon: Ext.Msg.ERROR,
title: 'Загрузка файла',
msg: 'Ошибка загрузки файла на сервер',
buttons: Ext.Msg.OK
});
reportsTemplatesGrid.getStore().reload();
}
});
};
fileReader.readAsDataURL(file);
}
}
});
// Server Part (PHP)
<?php
ini_set('default_charset','UTF-8');
$MaxFileSize = 2*1024*1024;
$UploadDir = "/var/www/fankom/tc1/templates/";
$fileBase64 = $_POST['file'];
$fileName = $_POST['fileName'];
$data = explode(',', $fileBase64);
$hFile = fopen($UploadDir.$fileName, 'wb');
if ($hFile != FALSE) {
$res = fwrite($hFile, base64_decode($data[1]));
if ($res != FALSE) {
fclose($hFile);
echo '{"success":"true"}';
} else {
echo '{"success":"false", "errmsg":"'.json_encode('Произошла ошибка во время передачи файла на сервер!').'"}';
}
} else {
echo '{"success":"false", "errmsg":"'.json_encode('Произошла ошибка во время передачи файла на сервер!').'"}';
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment