ExtJS:文件上传实例

页面导航:首页 > 网络编程 > JavaScript > ExtJS:文件上传实例

ExtJS:文件上传实例

来源: 作者: 时间:2016-02-04 09:15 【

ExtJS:文件上传实例var ext_dateFormat = Y-m-d H:i:s ;var dateFormat = yyyy-MM-dd HH:mm:ss ;var date = new Date();Ext onReady(function() {var fifp =Ext create( Ext form Pa

ExtJS:文件上传实例

var ext_dateFormat = 'Y-m-d H:i:s';
var dateFormat = 'yyyy-MM-dd HH:mm:ss';
var date = new Date();
Ext.onReady(function() {
	var fifp =Ext.create('Ext.form.Panel', {
        renderTo: 'fi-form',
        width: 500,
        frame: true,
        title: '文件上传',
        bodyPadding: '10 10 0',

        defaults: {
            anchor: '100%',
            allowBlank: false,
            msgTarget: 'side',
            labelWidth: 80
        },
        items: [{
            xtype: 'textfield',
            fieldLabel: '样品编号',
            id:'finfo',
            name:'finfo'
        },{
        	 xtype: 'container',
             layout: 'hbox',
             items: [{
            	 xtype: 'textfield',
                 fieldLabel: '当前经度',
                 id:'flongitude',
                 name:'flongitude',
                 msgTarget: 'side',
                 allowBlank: false,
                 labelWidth: 80
             }, {
            	 xtype: 'textfield',
                 fieldLabel: '当前纬度',
                 id:'flatitude',
                 name:'flatitude',
                 msgTarget: 'side',
                 allowBlank: false,
                 labelWidth: 80
             }]
        },{
			xtype : 'textfield',
			fieldLabel : '上传时间',
			id : 'ftime',
			name : 'ftime',
			// yyyy-MM-dd HH:mm:ss
			value : Ext.Date.format(new Date(date.getFullYear(),date.getMonth(),date.getDate(),
					date.getHours(),date.getMinutes(),date.getSeconds()), ext_dateFormat),
			listeners : {
				'focus' : function() {
					WdatePicker({
						dateFmt : dateFormat
					});
				}
			}
        },{
            xtype: 'filefield',
            id: 'fiupload',
            emptyText: '请点击右边按钮选择文件!',
            fieldLabel: '选择文件',
            name: 'fiupload',
            buttonText: '浏览文件',
            buttonConfig: {
                iconCls: 'upload-icon'
            }
        }],

        buttons: [{
            text: '保存文件',
            handler: function(){
                var fiform = this.up('form').getForm();
                if(fiform.isValid()){
                    fiform.submit({
                    	type : 'ajax',  
                        url: 'files/addData.action',
                        method : "POST",
                        waitMsg: ' 正在上传,请稍候...',
                        success: function(form, action) {
                        	Ext.Msg.alert('Success','文件上传成功!');
                        },
                    	failure:function(form, action)
                    	{
                    	Ext.Msg.alert("Failure","文件上传失败");
                    	}
                    });
                }
            }
        },{
            text: '重新上传',
            handler: function() {
                this.up('form').getForm().reset();
            }
        }]
    });
	
});

后台处理核心类方法:

	private static final int BUFFER_SIZE = 16 * 1024;

	public String addData() throws Exception {
		Timestamp ts = new Timestamp(System.currentTimeMillis());
		SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmssSSS");// 设置日期格式
		ts = Timestamp.valueOf(this.ftime);
		System.out.println(ts);
		String nowtime = df.format(new Date());
		System.out.println("uploadFileName = " + this.fiuploadFileName);
		System.out.println("uploadContentType = " + this.fiuploadContentType);
		System.out.println(nowtime);
		// upload -- wapps 下面的文件夹,用来存放图片
		String toSrc = ServletActionContext.getServletContext().getRealPath(
				"upload")
				+ "/" + nowtime + getFileExp(this.fiuploadFileName); // 使用時間戳作為文件名
		String toFilename = nowtime + getFileExp(this.fiuploadFileName);
		String toSrcPath = "./upload/" + toFilename;
		String toinfo = this.finfo;
		Double tolongitude = Double.parseDouble(this.flongitude);
		Double tolatitude = Double.parseDouble(this.flatitude);
		System.out.println("原文件名 : " + this.fiuploadFileName);
		System.out.println("文件描述 : " + toinfo);
		System.out.println("存放路径: " + toSrcPath);
		System.out.println("存放文件名: " + toFilename);
		System.out.println("当前经度 : " + tolongitude);
		System.out.println("当前维度 : " + tolatitude);
		File toFile = new File(toSrc);
		writeFile(this.fiupload, toFile);
		Files files = new Files(ts, toFilename, toSrcPath, toinfo, tolatitude,
				tolongitude);
		String result = filesService.addData(files);
		System.out.println(result);
		success = true;
		return SUCCESS;
	}

	private static void writeFile(File src, File dst) {
		System.out.println(" == 文件写入 == ");
		try {
			InputStream in = null;
			OutputStream out = null;
			try {

				in = new BufferedInputStream(new FileInputStream(src),
						BUFFER_SIZE);
				out = new BufferedOutputStream(new FileOutputStream(dst),
						BUFFER_SIZE);
				byte[] buffer = new byte[BUFFER_SIZE];
				while (in.read(buffer) > 0) {
					out.write(buffer);
				}
			} finally {
				if (null != in) {
					in.close();
				}
				if (null != out) {
					out.close();
				}
			}
		} catch (Exception e) {

			e.printStackTrace();
		}
		System.out.println(" == 写入成功! == ");
	}


Tags:

文章评论

最 近 更 新
热 点 排 行
Js与CSS工具
代码转换工具

<