DWR + Ext的文件上传与下载

前台

用到的Ext组件

{
    xtype: 'fileuploadfield',
    width:300,
    flex: 1,
    emptyText: '请选择文件',
    fieldLabel: '请选择文件',
    allowBlank: false,
    id: 'import',
    fileTypes:['xlsx','xls'],
    name: 'importFile',
    buttonText: '浏览',
    vtype:'fileType'
         }

 

  此组件似乎没有校验文件后缀名的功能,需要自己校验,此处用xtype,如果用form的url提交,会显示自带的进度条,如果是用dwr异步提交,则不会显示了。

dwr异步提交的关键代码:

var fileValue=dwr.util.getValue("import-file");
vlanAdapter.uploadFiles(fileValue,function(data){
                     alert(data);
	   });

 后台:

Adapter中方法的定义:

 

public String uploadFiles(FileTransfer file){
         Workbook workbook=new HSSFWorkbook(file.getInputStream());
}

 下载部分:

后台adapter方法:

	public FileTransfer downLoadTemplate(){
		try {
//			BufferedInputStream in = new BufferedInputStream(new FileInputStream("C:/Users/tanghui/Desktop/模版.xlsx"));
//			String filePath=realPath+"service/template/模板.xlsx";
			String filePath = SystemRoot.getRootPathAsWebApp()+"service"+File.separator+"template"+File.separator+"模版.xls";
			filePath = filePath.replaceAll("/", File.separator+File.separator);
			File file = new File(filePath);
			BufferedInputStream in = new BufferedInputStream(new FileInputStream(file));
			ByteArrayOutputStream out = new ByteArrayOutputStream();  
			byte[] temp = new byte[1024];  
			int size = 0;    
			while ((size = in.read(temp)) != -1)  
			{  
				out.write(temp, 0, size);  
			} 
			String  filename="模版.xls";
			return new FileTransfer(new String( filename.getBytes("GBK"),"iso8859-1" ), "application/vnd.ms-excel", out.toByteArray());  
		} catch (FileNotFoundException e){
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

 前台js

vlanAdapter.downLoadVlan(deviceName,ponName,cardId,{callback :function(data){
								dwr.engine.openInDownload(data)
							},async:false});

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值