| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196 |
- /**
- * 多文件上传下载
- */
- Ext.define('erp.view.core.form.FileField', {
- extend: 'Ext.form.FieldSet',
- alias: 'widget.mfilefield',
- minHeight: 22,
- collapsible: true,
- title: '<img src="' + basePath + 'resource/images/icon/clip.png" width=20 height=20/>附件',
- style: 'background:#f1f1f1;',
- margin: '2 2 2 2',
- autoUpdate:true,//添加属性是否自动更新
- filesize: 0,//附件总大小
- initComponent: function() {
- this.columnWidth = 1;//强制占一行
- this.cls = '';
- this.callParent(arguments);
- this.items.items[0].name = this.name;
- },
- layout:'column',
- items: [{
- xtype: 'hidden',//该隐藏字段的值(附件在FilePath表的ID,用;隔开)将被保存到数据库
- value: '',
- fieldLabel: '附件'
- },{
- xtype: 'form',
- columnWidth: 1,
- frame: false,
- border: false,
- minHeight: 22,
- bodyStyle: 'background:#f1f1f1;',
- layout: 'hbox',
- items: [{
- xtype: 'filefield',
- name: 'file',
- buttonText: '浏览<font color=blue size=1>(≤100M)</font>...',
- buttonOnly: true,
- hideLabel: true,
- listeners: {
- change: function(field){
- if(field.value != null){
- field.ownerCt.ownerCt.upload(field.ownerCt, field);
- }
- }
- }
- }]
- }],
- setValue: function(value){
- this.value = value;
- if(Ext.isEmpty(value))
- this.down('hidden').setValue('');
- else if(this.items && this.items.items.length==2){
- this.download(value);
- }
- },
- listeners : {
- afterrender: function(f){
- var form = f.ownerCt;
- if(f.value != null && f.value.toString().trim() != ''){
- f.download(f.value);
- }
- if(form.keyField){
- Ext.getCmp(form.keyField).on('change', function(){
- Ext.defer(function(){
- Ext.each(f.items.items, function(item, index){
- if(index > 1){
- item.destroy();
- }
- });
- if(f.value != null && f.value.toString().trim() != ''){
- f.download(f.value);
- }
- }, 50);
- });
- }
- f.setReadOnly(f.readOnly);
- }
- },
- /**
- * 上传附件
- */
- upload: function(form, field){
- var me = this;
- var filename = '';
- if(contains(field.value, "\\", true)){
- filename = field.value.substring(field.value.lastIndexOf('\\') + 1);
- } else {
- filename = field.value.substring(field.value.lastIndexOf('/') + 1);
- }
- form.getForm().submit({
- url: basePath + 'common/upload.action?em_code=' + em_code,
- waitMsg: "正在上传:" + filename,
- success: function(fp, o){
- if(o.result.error){
- showError(o.result.error);
- } else {
- me.fireEvent('afterupload', me, filename);
- Ext.Msg.alert("恭喜", filename + " 上传成功!");
- me.filesize += o.result.size;
- me.setTitle('<img src="' + basePath + 'resource/images/icon/clip.png" width=20 height=20/>附件' +
- '(总大小:' + Ext.util.Format.fileSize(me.filesize) + ")");
- var name = filename + " (" + Ext.util.Format.fileSize(o.result.size) + ")";
- var w = Math.min((me.getStrLength(name) + 10) / 200, .8);
- var field = Ext.create('erp.view.core.trigger.TrashField', {
- fileName: filename,
- value: name,
- columnWidth: w,
- readOnly: false,
- editable: false,
- filepath: o.result.filepath,
- filesize: o.result.size,
- realpath: o.result.path,
- fieldStyle: 'background:#E0EEEE;'
- });
- me.add(field);
- var val = me.down('hidden').value + o.result.filepath + ';';
- me.down('hidden').setValue(val);
- // 单据已经存在的情况下,直接修改attach字段
- var form = me.ownerCt, kf;
- if(form && form.keyField && (kf = Ext.getCmp(form.keyField)) != null
- && kf.getValue() != null && kf.getValue() != 0 && me.autoUpdate) {
- field.updateAttachField(val, '添加附件');
- }
- }
- }
- });
- },
- /**
- * 根据id读取对应PATH
- * @param id{String} fp_id
- */
- download: function(id){
- var me = this;
- var files = new Array();
- Ext.Ajax.request({
- url : basePath + 'common/getFilePaths.action',
- async: false,
- params: {
- id: id,
- itemId:me.id
- },
- method : 'post',
- callback : function(options,success,response){
- var res = new Ext.decode(response.responseText);
- if(res.exception || res.exceptionInfo){
- showError(res.exceptionInfo);
- return;
- }
- files = res.files != null ? res.files : [];
- }
- });
- Ext.each(files, function(f){
- var path = f.fp_path;
- var fileName = f.fp_name;
- if(!fileName) {
- if(contains(path, '\\', true)){
- fileName = path.substring(path.lastIndexOf('\\') + 1);
- } else {
- fileName = path.substring(path.lastIndexOf('/') + 1);
- }
- }
- me.filesize += f.fp_size;
- me.setTitle('<img src="' + basePath + 'resource/images/icon/clip.png" width=20 height=20/>附件' +
- '(总大小:' + Ext.util.Format.fileSize(me.filesize) + ")");
- var name = fileName + " (" + Ext.util.Format.fileSize(f.fp_size) + ")";
- var w = Math.min((me.getStrLength(name) + 10) / 200, .8);
- me.addItem(Ext.create('erp.view.core.trigger.TrashField', {
- fileName: fileName,
- value: name,
- columnWidth: w,
- readOnly: false,
- editable: false,
- filepath: f.fp_id,
- filesize: f.fp_size,
- realpath: path,
- fieldStyle: 'background:#E0EEEE;'
- }));
- me.down('hidden').setValue(me.down('hidden').value + f.fp_id + ';');
- me.down('hidden').originalValue = me.down('hidden').value;
- });
- },
- addItem: function(item){
- this.add(item);
- },
- setReadOnly: function(bool){
- /*this.down('filefield').setDisabled(bool);*/
- },
- setFieldStyle: function(str) {
- },
- 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;
- }
- });
|