Front-end interface to call back two ways to download the word document

 

 

                      1 While traditional ajax to submit to the background, but the returned data is parsed into json, html, text strings, etc., can not respond to the browser to download. Even if the use of simulation download bob, when the amount of data is not easy

          Ado: the Code (here Layui listening to submit, traditional html submit a more convenient)

// monitor submitted 
form. ON ( 'Submit (formDemo)', function (Data) {
Layer. MSG ( the JSON. The stringify (Data. Field));
// this first embodiment directly opened link, a parameter to the suffix Find the background, you can download
// window.open (+ data.field.id, '_blank' '/ Jump / download the above mentioned id =?');


// second way analog form submission this way, the value of dynamic access pass back into the try { var queryForm = $ ( '#llll'); var exportForm = $ ( "<form Action = '/ Jump / downloads' Method = 'POST'> </ form>") . queryForm Find ( "INPUT" ). each ( function () { var name = $ ( the this). attr ( "name");var value =






$(this).val();
exportForm.append("<input type='hidden' name='" + name + "' value='" + value + "'/>")
});

queryForm.find("select").each(function() {
var name = $(this).attr("name");
var value = $(this).val();
exportForm.append("<input type='hidden' name='" + name + "' value='" + value + "'/>")
});

$(document.body).append(exportForm);
exportForm. Submit ();
} the catch (E) {
Console. log (E);
} the finally {
exportForm. Remove ();
}


// ajax third form is submitted. 3, does not correspond to the browser to download in this way may submit a Map to go back but the return is not easy to download files, proficient native js partner can try, too much trouble, again I do not incompetence
// $ .ajax ({
// of the type: "GET",
// url: "/ Jump / downloads ",
// Data: the JSON.stringify (data.field),
// contentType:" file application / JSON; charset = UTF-. 8 ",
// Success: function (Data) {
//
//}
//} ); return false; // ajax when submitting Layui prevent pages automatically refresh });




 @ApiOperation (value = "Export word document", Notes = "{ \" ID \ " : \" . 11 \ " }") 
@RequestMapping (value = . "/ Downloads", Method = RequestMethod the POST)
public void downloads (the HttpServletRequest Request, the HttpServletResponse Response) throws IOException {

// corresponding to the front end of the third submission @RequestBody Map map a first embodiment submitted over a distal support the Map
the Map <String, Object> = userMapMeS new new the HashMap <String, Object> ();
/ / String fileName = as map.get ( "name") toString ();.
// userMapMeS.put ( "name", as map.get ( "name") toString ().);
// userMapMeS.put ( "ID" , map.get ( "id") toString ()).;
// userMapMeS.put("age", map.get("age").toString());
// userMapMeS.put("phone", map.get("phone").toString());
// userMapMeS.put("password", map.get("password").toString());
// userMapMeS.put("email", map.get("email").toString());
// userMapMeS.put("times", map.get("times").toString());
// userMapMeS.put("address", map.get("address").toString());
// userMapMeS.put("sex", map.get("sex").toString());
// userMapMeS.put("work", map.get("work").toString());
// userMapMeS.put("hobby", map.get("hobby").toString());
//对应第一种前端提交方式


// String id=request.getParameter("id");
// Map map=new HashMap();
// map.put("id",id);
// User user= userService.selectUser(map);
// String fileName=user.getName();
// Map<String, Object> userMapMeS = new HashMap<String, Object>();
// userMapMeS.put("name", user.getName());
// userMapMeS.put("id", user.getId());
// userMapMeS.put("age", user.getAge());
// userMapMeS.put("phone", user.getPhone());
// userMapMeS.put("password", user.getPassword());
// userMapMeS.put("email", user.getEmail());
// userMapMeS.put("times", user.getTimes());
// userMapMeS.put("address", user.getAddress());
// userMapMeS.put("sex", user.getSex());
// userMapMeS.put("work", user.getWork());
// userMapMeS.put("hobby", user.getHobby());UserMapMeS.put ("name") toString ();.String fileName = request.getParameter (

// corresponding to a second distal end analog form submission

"name", request.getParameter("name").toString());
userMapMeS.put("id", request.getParameter("id").toString());
userMapMeS.put("age", request.getParameter("age").toString());
userMapMeS.put("phone", request.getParameter("phone").toString());
userMapMeS.put("password", request.getParameter("password").toString());
userMapMeS.put("email", request.getParameter("email").toString());
userMapMeS.put("times", request.getParameter("times").toString());
userMapMeS.put("address", request.getParameter("address").toString());
userMapMeS.put("sex", request.getParameter("sex").toString());
userMapMeS.put("work", request.getParameter("work").toString());
userMapMeS.put("hobby", request.getParameter("hobby").toString());
String endCodeFileName = new String(fileName.getBytes("utf-8"), "ISO8859-1");
response.reset();//清除空白行纺织报错
response.setHeader("Access-Control-Allow-Origin", "*");//所有域都可以跨
response.setHeader("Content-Type", "application/octet-stream;charset=UTF-8");//二进制 流文件
response.setHeader("Content-Disposition", "attachment;filename=" + endCodeFileName+".doc"); // download file name and
response.setHeader ( "Connection", "use Close"); // close the connection request header
// Set the file is opened in the browser or download
response.setContentType ( "the Application / the X- -download ");


WordUtil wUtil = new new WordUtil ();
Long now = the System. with currentTimeMillis ();
wUtil.createDoc (userMapMeS, response.getOutputStream ());
. = End Long the System with currentTimeMillis ();
Long ddd = End-now ;
. the System Out.println ( "ddd" + ddd);

}
 In the next article tools

Guess you like

Origin www.cnblogs.com/wangbiaohistory/p/11761855.html