您的位置:首页 > 产品设计 > UI/UE

使用easyUI中rowData和ajax实现从服务器下载文件

2018-01-18 11:19 387 查看
从服务器下载文件,个人总结主要的实现思路有:1、界面通过表单方式提交,服务器接受表单后通过读写流方式直接将保存在服务器端的文件下载下来。2、通过url直接传参读写流方式直接将保存在服务器端的文件下载下来。这两种思路都可以实现。而在easyUI框架中由于对url方式整合不当,通过url方式引来其他的错误。在这里使用以下方式实现:ajaxpost方式提交--->通过数据库查找到对应保存在服务器文件的id并保存于session中--->获取session中文件信息(文件名称、路径)--->通过文件读写流处理文件--->释放sessionA、在js中创建一个表单
function toDownLoad(rowData) { $.messager.confirm('<fmt:message key="statTaskSet.title.download" />', '<fmt:message key="statTaskSet.text.info" />', function(r) { if(r){ $.easyuix.ajax({ "url":"toExport", "type":"POST", "data":{ id:rowData.id }, normal:function(data){ var form=$("<form>");//定义一个form表单 form.attr("style","display:none"); form.attr("target",""); form.attr("method","post"); form.attr("action","exportFile"); $("body").append(form);//将表单放置在web中 form.submit();//表单提交 $('#data_list').datagrid('reload'); } }) $("#data_list").datagrid('reload'); } }); }
B、接受ajaxpost请求
@RequestMapping(value = "/toExport", method = RequestMethod.POST) @ResponseBody public Result toExport(@RequestParam("id")Long id) { return statTaskSetService.exportFile(id); }
C、文件信息保存在session中
public Result exportFile(Long id) { Result result = new Result(); StatTaskSet bean = statTaskSetMapper.findById(id); BusinessException.ASSERT.notNull(bean, "statTaskSet.error.notexist"); String tempPath = SysParamHolder.getValue(ParamValConstant.fileSavePath); BusinessException.ASSERT.notNull(tempPath, "statTaskSet.error.filepath"); MapObject mapSession = new MapObject(); mapSession.put("fileName", bean.getFileName()); mapSession.put("filePath", tempPath); mapSession.put("idkeys", id); WebUtil.sessionPut("taskFileMap", mapSession); return result;
}
D、请求获取session处理文件并释放session
@RequestMapping(value = "/exportFile", method = RequestMethod.POST, produces = "text/plain") @ResponseBody public void exportFile() { MapObject map = (MapObject) sessionGet("taskFileMap"); String path = (String) map.get("fileName"); String name = (String) map.get("filePath"); FileDownloadUtil.downloadByFile(path, name); sessionPut("taskFileMap", null);//釋放session }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: