Use hidden form to download files, solve url download, limit url length due to environmental problems, can not meet all needs!

1. For some environments, export and download directly with wiondow.href=url. For some business requirements, such as employee files and other fields, when there are many fields, selecting all to export will cause an exception. Due to the limitation of Nginx forwarding length,

If the operation and maintenance do not want to change the environment, they can only modify the program daringly, that is, from the original get method to POST, the ajax method cannot download the file, because the file stream will be returned directly to the callback function, so here we take the hide form download

The background interface is modified to POST acceptance method:

    @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);
    }

The foreground uses the form form to submit data:

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

			var form = $("<form>"); //Define a form  
			form.attr('style', 'display:none'); //Add query parameters to the 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);

  One thing to note here: don't rush to set the form encoding method, first try to see if the background can accept the parameters you need to submit, I am here because I set the encoding parameters actively for the first time, and I have been reporting 415 errors, and then the second time I did not Set it up and succeed directly! (Damn it)

All the required parameters can be set to hide the input input box, there is no problem with how many! The value is no matter what value (array, character, list, map, etc.)

Finally submit the form:

	$('body').append(form); //Place the form in the web   
			form.append(input1); //Submit the query parameter control to the form
			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();  

  At this time, the limitation of url length is solved, no matter how many parameters you need to pass, you can download it correctly!

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325251032&siteId=291194637