|
@@ -0,0 +1,312 @@
|
|
|
+
|
|
|
+ * 多文件上传下载
|
|
|
+ */
|
|
|
+Ext.define('Ext.ux.form.field.MFileField', {
|
|
|
+ extend: 'Ext.form.FieldSet',
|
|
|
+ alias: 'widget.mfilefield',
|
|
|
+
|
|
|
+ defaultBindProperty: 'value',
|
|
|
+
|
|
|
+ isFormField:true,
|
|
|
+
|
|
|
+ columnWidth: 1,
|
|
|
+ cls: 'multi-file-field',
|
|
|
+ minHeight: 22,
|
|
|
+ collapsible: true,
|
|
|
+
|
|
|
+ layout:'column',
|
|
|
+
|
|
|
+ files: [],
|
|
|
+
|
|
|
+ initComponent: function() {
|
|
|
+ var me = this;
|
|
|
+ this.title = this.fieldLabel;
|
|
|
+
|
|
|
+ if(this.value || this.defalutValue) {
|
|
|
+ me.getFilesInfo(this.value || this.defalutValue).then(function(res) {
|
|
|
+ var filesInfo = res.data;
|
|
|
+ me.addFiles(filesInfo);
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ Ext.apply(me, {
|
|
|
+ items: [{
|
|
|
+ xtype: 'hidden',
|
|
|
+ name: 'fileIds'
|
|
|
+ }, {
|
|
|
+ xtype: 'filefield',
|
|
|
+ name: 'file',
|
|
|
+ buttonText: '浏览文件(≤20MB)',
|
|
|
+ buttonOnly: true,
|
|
|
+ hideLabel: true,
|
|
|
+ columnWidth: 1,
|
|
|
+ float: 'left',
|
|
|
+ buttonConfig:{
|
|
|
+ cls:'x-filefield-button',
|
|
|
+ style: {
|
|
|
+ float: 'left',
|
|
|
+ margin: '0 0 5px 0'
|
|
|
+ }
|
|
|
+ },
|
|
|
+ createFileInput : function() {
|
|
|
+ this.fileInputEl = this.button.el.createChild({
|
|
|
+ name: this.getName(),
|
|
|
+ cls: Ext.baseCSSPrefix + 'form-file-input',
|
|
|
+ tag: 'input',
|
|
|
+ type: 'file',
|
|
|
+ multiple:'multiple',
|
|
|
+ size: 1
|
|
|
+ }).on('change', this.onFileChange, this);
|
|
|
+ },
|
|
|
+ listeners: {
|
|
|
+ change: function(field){
|
|
|
+ if(field.value){
|
|
|
+ me.upload(field);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }, {
|
|
|
+ xtype: 'form',
|
|
|
+ layout: 'column',
|
|
|
+ cls: 'form-file-tags',
|
|
|
+ columnWidth: 1,
|
|
|
+ bodyPadding:'0 0 5px 5px',
|
|
|
+ items: [],
|
|
|
+ }],
|
|
|
+ listeners: {
|
|
|
+ change: function() {
|
|
|
+ console.log('field value change', arguments);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ this.callParent(arguments);
|
|
|
+ },
|
|
|
+
|
|
|
+ setValue: function(v){
|
|
|
+ var me = this,
|
|
|
+ items = me.items.items,
|
|
|
+ files = me.files,
|
|
|
+ newValue = '',
|
|
|
+ filesInfo;
|
|
|
+
|
|
|
+ me.getFilesInfo(v).then(function(res) {
|
|
|
+ var filesInfo = res.data;
|
|
|
+ me.addFiles(filesInfo);
|
|
|
+ });
|
|
|
+ },
|
|
|
+
|
|
|
+ getValue: function (){
|
|
|
+ return this.items.items[0].value;
|
|
|
+ },
|
|
|
+
|
|
|
+ getValueField: function () {
|
|
|
+ return this.items.items[0];
|
|
|
+ },
|
|
|
+
|
|
|
+ getFileForm: function() {
|
|
|
+ return this.items.items[2];
|
|
|
+ },
|
|
|
+
|
|
|
+ isValid: function () {
|
|
|
+ return this.getValueField().isValid();
|
|
|
+ },
|
|
|
+
|
|
|
+ isDirty: function () {
|
|
|
+ return this.getValueField().isDirty();
|
|
|
+ },
|
|
|
+
|
|
|
+ setReadOnly: function (val) {
|
|
|
+ var me = this,
|
|
|
+ items = me.items.items,
|
|
|
+ fileIdsField = items[0],
|
|
|
+ fileField = items[1];
|
|
|
+
|
|
|
+ fileIdsField.setReadOnly(val);
|
|
|
+ fileField.button.setDisabled(val);
|
|
|
+ },
|
|
|
+
|
|
|
+ resetOriginalValue: function() {
|
|
|
+ this.getValueField().resetOriginalValue();
|
|
|
+ },
|
|
|
+
|
|
|
+ addFiles: function(fileInfos) {
|
|
|
+ var me = this,
|
|
|
+ items = me.items.items,
|
|
|
+ value = me.getValue(),
|
|
|
+ id,
|
|
|
+ oldValue = value,
|
|
|
+ newValue = value;
|
|
|
+
|
|
|
+ Ext.Array.forEach(fileInfos, function(fileInfo) {
|
|
|
+ if(!me.contains(fileInfo)) {
|
|
|
+ newValue = (newValue || '') + fileInfo.id + ';';
|
|
|
+ me.files.push(fileInfo);
|
|
|
+ me.insertFileTag(fileInfo);
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ items[0].setValue(newValue);
|
|
|
+ this.publishState('value', newValue);
|
|
|
+ this.fireEvent('change', this, newValue, oldValue);
|
|
|
+ },
|
|
|
+
|
|
|
+ addFile: function(fileInfo) {
|
|
|
+ var me = this,
|
|
|
+ items = me.items.items,
|
|
|
+ value = me.getValue(),
|
|
|
+ oldValue = value,
|
|
|
+ newValue = value;
|
|
|
+
|
|
|
+ if(!me.contains(fileInfo)) {
|
|
|
+ newValue = (newValue || '') + fileInfo.id + ';';
|
|
|
+ items[0].setValue(newValue);
|
|
|
+ this.files.push(fileInfo);
|
|
|
+ this.insertFileTag(fileInfo);
|
|
|
+
|
|
|
+ this.publishState('value', newValue);
|
|
|
+ this.fireEvent('change', this, newValue, oldValue);
|
|
|
+ }
|
|
|
+ },
|
|
|
+
|
|
|
+ insertFileTag: function(fileInfo) {
|
|
|
+ var me = this,
|
|
|
+ fileForm = me.getFileForm(),
|
|
|
+ id = fileInfo.id,
|
|
|
+ name = fileInfo.name,
|
|
|
+ path = fileInfo.fullName,
|
|
|
+ size = fileInfo.size,
|
|
|
+ fileText, fieldWidth, newFileTag;
|
|
|
+
|
|
|
+ if(!id) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ fileText = name + " (" + Ext.util.Format.fileSize(size) + ")";
|
|
|
+ fieldWidth = Math.min((me.getStrLength(fileText) + 25) / 200, .8);
|
|
|
+
|
|
|
+ newFileTag = Ext.create('Ext.ux.form.field.FileTag', {
|
|
|
+ fileName: name,
|
|
|
+ value: name,
|
|
|
+ columnWidth: fieldWidth,
|
|
|
+ readOnly: false,
|
|
|
+ editable: false,
|
|
|
+ filepath: path,
|
|
|
+ filesize: size,
|
|
|
+ _id: id,
|
|
|
+ fieldStyle: 'background:#E0EEEE;'
|
|
|
+ });
|
|
|
+ fileForm.add(newFileTag);
|
|
|
+ },
|
|
|
+
|
|
|
+
|
|
|
+ * 上传附件
|
|
|
+ */
|
|
|
+ upload: function(fileField){
|
|
|
+ var me = this;
|
|
|
+ var fileEl = fileField.fileInputEl.dom;
|
|
|
+ var fd = new FormData();
|
|
|
+ fd.append('file', fileEl.files[0]);
|
|
|
+ fd.append('folderId', 0);
|
|
|
+ me.setLoading(true);
|
|
|
+ Ext.Ajax.request({
|
|
|
+ url: '/api/file/upload',
|
|
|
+ cors: true,
|
|
|
+ useDefaultXhrHeader: false,
|
|
|
+ method: 'post',
|
|
|
+ rawData: fd,
|
|
|
+ headers: {
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ "Content-Type":null
|
|
|
+ },
|
|
|
+ success: function (response, opts) {
|
|
|
+ me.setLoading(false);
|
|
|
+ var res = Ext.decode(response.responseText);
|
|
|
+ if(res.success){
|
|
|
+ console.log('success');
|
|
|
+ var data = res.data;
|
|
|
+ me.addFile(data);
|
|
|
+ }else{
|
|
|
+ console.error('failure');
|
|
|
+ }
|
|
|
+ },
|
|
|
+ failure: function (response, opts) {
|
|
|
+ me.setLoading(false);
|
|
|
+ console.error('failure');
|
|
|
+ }
|
|
|
+ });
|
|
|
+ },
|
|
|
+
|
|
|
+ getFilesInfo: function(value){
|
|
|
+ var me = this;
|
|
|
+
|
|
|
+ me.setLoading(true);
|
|
|
+ return new Ext.Promise(function (resolve, reject) {
|
|
|
+ Ext.Ajax.request({
|
|
|
+ url : '/api/file/info',
|
|
|
+ params: {
|
|
|
+ ids: value
|
|
|
+ },
|
|
|
+ method : 'GET',
|
|
|
+ success: function (response, opts) {
|
|
|
+ me.setLoading(false);
|
|
|
+ var res = Ext.decode(response.responseText);
|
|
|
+ if(res.success){
|
|
|
+ resolve(res);
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ }else{
|
|
|
+ reject(res);
|
|
|
+ }
|
|
|
+ },
|
|
|
+ failure: function (response, opts) {
|
|
|
+ me.setLoading(false);
|
|
|
+ reject(response);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ })
|
|
|
+ },
|
|
|
+
|
|
|
+ contains: function(fileInfo) {
|
|
|
+ if(typeof fileInfo === 'string') {
|
|
|
+ fileInfo = { id: fileInfo };
|
|
|
+ }
|
|
|
+ return !!Ext.Array.findBy(this.files, function(f) {
|
|
|
+ return f.id === fileInfo.id;
|
|
|
+ });
|
|
|
+ },
|
|
|
+
|
|
|
+ clearAll: function() {
|
|
|
+ this.getFileForm().removeAll();
|
|
|
+ this.setValue('');
|
|
|
+ this.files.length = 0;
|
|
|
+ },
|
|
|
+
|
|
|
+ getStrLength: function(str) {
|
|
|
+ for (var len = str.length, c = 0, i = 0; i < len; i++)
|
|
|
+ str.charCodeAt(i) < 27 || str.charCodeAt(i) > 126 ? c += 2 : c++;
|
|
|
+ return c;
|
|
|
+ },
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+});
|