使用隐藏form表单下载文件,解决url方式下载,由于环境问题而限制url长度,满足不了所有的需求!


一 对于某些环境导出是直接用wiondow.href=url直接导出下载,有些业务需求,如员工档案等字段比较多的时候,全选导出就会引发异常,由于Nginx转发长度限制的问题,

如果运维不愿意改变环境,只能硬着头皮修改程序了,即由原来的get方式改为POST,ajax方式是不能下载文件的,因为会把文件流直接返回到回调函数中,所以这里采取隐藏form表单下载

后台接口修改为POST接受方式:

    @SuppressWarnings("rawtypes")
    @RequestMapping(value = "/common", method = RequestMethod.POST)
    public void export(@RequestParam Map map, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
        File file = exportServlce.export(map);
        downLoad(httpServletResponse, httpServletRequest, file);
    }

前台使用form表单提交数据:

// window.location = gyz.tools.addPer(url);
			let url = '/minierp/sys/export/common'

			var form = $("<form>");   //定义一个form表单  
			form.attr('style', 'display:none');   //在form表单中添加查询参数  
			form.attr('target', '');  
			form.attr('method', 'post');  
			form.attr('action', url);  
		
			var input1 = $('<input>');  
			input1.attr('type', 'hidden');  
			input1.attr('name', 'exportTitle');  
			input1.attr('value', me.exportTitle);

  这里注意一点:先不要着急设置表单编码方式,先试试看后台能不能接受到你需要提交的参数,我再这里就是因为第一次主动设置了编码参数,一直报415错误,后来第二次不设置,直接一次成功!(活见鬼)

需要的参数就全部设置为隐藏input输入框即可,有多少都没问题!,value也无论是什么值(数组,字符,list,map等)

最后提交表单即可:

	$('body').append(form);  //将表单放置在web中   
			form.append(input1);   //将查询参数控件提交到表单上
			form.append(input2);
			form.append(input3);
			form.append(input4);
			form.append(input5);
			form.append(input6);
			if (me.appType != undefined) {
				var input7 = $('<input>');  
				input7.attr('type', 'hidden');  
				input7.attr('name', 'appType');  
				input7.attr('value', me.appType);
				form.append(input7);
			 }
			form.submit();  

  这个时候,就解决了url长度的限制,无论你需要传的参数有多少个都可以正确下载!


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM