Ext.ux.UploadDialog的使用经验

本文介绍如何使用Ext.ux.UploadDialog实现单文件上传功能,通过重写文件添加事件来限制每次仅能上传一个文件,并提供了完整的前端JavaScript代码及Struts2后台处理逻辑。

由于近段时间产品中需要加上上传组件,之前考虑过jQuery,但是现有项目是用ext完成的。所以使用了

Ext.ux.UploadDialog进行实现,遇到的第一个问题就是该Ext组件是批量上传组件,但是产品中只允许单个上传,通过对

源码的查看,发觉只要在添加文件时进行过滤就可以了。过滤代码如下:

 

 

//定义文件添加事件


    dialog.on('fileadd', function(){


    	var tb = this.grid_panel.getBottomToolbar();


    	if(this.grid_panel.getStore().getCount() > 1){


    		extWarn('每次只能上传一张背景图片!');


    		this.grid_panel.getSelectionModel().selectLastRow();


			var selections = this.grid_panel.getSelectionModel().getSelections();


			this.fsa.postEvent('remove-files', selections);    		


    	}


	});  

 具体API就不多说了。

 

 官方网站:http://www.max-bazhenov.com/dev/upload-dialog-2.0/index.php

 

 效果如下:

     上传截图

 

  完整的代码如下:

 

 1.前台代码:

 

 

/*
 异步上传背景图片
 */
function upload(code){
    var dialog = new Ext.ux.UploadDialog.Dialog({
        title: '上传背景图片(<a style="color:red">请注意:每次只能上传一张图片</a>)',
        url: 'mapConfigAction_uploadImage.action', 
        base_params: {
        	proCode: 'china'
        },
        post_var_name: 'uploadifyFiles',
        width: 450,
        height: 200,
        minWidth: 450,
        minHeight: 200,
        draggable: true,
        resizable: true,
        constraintoviewport: true,
        permitted_extensions: ['JPG', 'jpg', 'jpeg', 'JPEG', 'GIF', 'gif', 'png', 'PNG'],
        modal: true,
        reset_on_hide: false,
        allow_close_on_upload: false, //关闭上传窗口是否仍然上传文件 
        upload_autostart: false
    });
   
	dialog.on('uploadsuccess',function(dialog, filename){
		Ext.MessageBox.alert('成功提示','文件上传完成!');
		dialog.hide();
	});
	
	//定义上传完成
    dialog.on('uploadcomplete', function(){
		Ext.MessageBox.alert('成功提示','文件上传完成!');
		dialog.hide();
	}); 
	
	//定义文件添加事件
    dialog.on('fileadd', function(){
    	var tb = this.grid_panel.getBottomToolbar();
    	if(this.grid_panel.getStore().getCount() > 1){
    		extWarn('每次只能上传一张背景图片!');
    		this.grid_panel.getSelectionModel().selectLastRow();
			var selections = this.grid_panel.getSelectionModel().getSelections();
			this.fsa.postEvent('remove-files', selections);    		
    	}
	}); 
    //显示上传框
    dialog.show();  
}

 2.struts2后台代码:

 

	/**
	 * 上传图片保存到报表端.
	 * 
	 * @throws IOException
	 * @throws SQLException 
	 * 
	 */
	private void save2Report(String proCode) throws IOException, SQLException {
		String path = "";
		if (StringUtils.equals(CHINA, proCode)) {
			path = AQSConstant.REPORT_CHINA_MAP_IMAGE_PATH;
		} else {
			path = AQSConstant.REPORT_PROVINCE_IMAGE_PATH + this.proCode + ".jpg";
		}

		// 根据路径写入图片
		writeImage(path);
	}

	/**
	 * 根据传入图片路径写入图片.
	 * 
	 * @param path
	 *            图片路径
	 * @throws IOException
	 * @throws SQLException 
	 */
	private void writeImage(String path) throws IOException, SQLException {
		int len = 0;
		FileInputStream fis = new FileInputStream(getUploadifyFiles()[0]);
		FileOutputStream fos = FileUtils.openFileOutputStream(new File(path));
		byte[] buffer = new byte[fis.available()];
		while ((len = fis.read(buffer)) > 0) {
			fos.write(buffer, 0, len);
		}
		
		fos.flush();
		fos.close();
	}
 

 

 

 

 

 

 

Configuration. Most configuration options are inherited from Ext.Window (see ExtJs docs). The added ones are: url - the url where to post uploaded files. base_params - additional post params (default to {}). permitted_extensions - array of file extensions which are permitted to upload (default to []). reset_on_hide - whether to reset upload queue on dialog hide or not (default true). allow_close_on_upload - whether to allow hide/close dialog during upload process (default false). upload_autostart - whether to start upload automaticaly when file added or not (default false). post_var_name - uploaded data POST variable name (defaults to 'file'). Events. filetest - fires before file is added into the queue, parameters: dialog - reference to dialog filename - file name If handler returns false then file will not be queued. fileadd - fires when file is added into the queue, parameters: dialog - reference to dialog filename - file name fileremove - fires when file is removed from the queue, parameters: dialog - reference to dialog filename - file name record - file record resetqueue - fires when upload queue is resetted, parameters: dialog - reference to dialog beforefileuploadstart - fires when file as about to start uploading: dialog - reference to dialog filename - uploaded file name record - file record If handler returns false then file upload will be canceled. fileuploadstart - fires when file has started uploading: dialog - reference to dialog filename - uploaded file name record - file record uploadsuccess - fires when file is successfuly uploaded, parameters: dialog - reference to dialog filename - uploaded file name data - js-object builded from json-data returned from upload handler response. record - file record uploaderror - fires when file upload error occured, parameters: dialog - reference to dialog filename - uploaded file name data - js-object builded from json-data returned from upload handler response. record - file record uploadfailed - fires when file upload failed, parameters: dialog - reference to dialog filename - failed file name record - file record uploadcanceled - fires when file upload canceled, parameters: dialog - reference to dialog filename - failed file name record - file record uploadstart - fires when upload process starts, parameters: dialog - reference to dialog uploadstop - fires when upload process stops, parameters: dialog - reference to dialog uploadcomplete - fires when upload process complete (no files to upload left), parameters: dialog - reference to dialog Public methods Better go see the source. I18n. The class is ready for i18n, override the Ext.ux.UploadDialog.Dialog.prototype.i18n object with your language strings, or just pass i18n object in config. Server side handler. The files in the queue are posted one at a time, the file field name is 'file'. The handler should return json encoded object with following properties:
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值