首先vue:标签:vue,zip,url,res,request,en,blob,method From: https://blog.51cto.com/u_12528551/5900324
data中
model: {},
方法
handleSubmit(e) {
e.preventDefault()
this.form.validateFields((err, values) => {
if (!err) {
let formData = Object.assign(this.model, values);
console.log('formData:', formData);
httpAction("下载地址", formData, 'post',{timeout:120000}).then((res) => {
//将二进制流转化为链接
let blob = new Blob([res], {type: 'application/zip'})
let url = window.URL.createObjectURL(blob)
const link = document.createElement('a')
link.href = url
link.download = values.projectName
link.click()
URL.revokeObjectURL(url)
})
}
})
},
export function httpAction(url,parameter,method,timeout=9000) {
return axios({
url: url,
method: method ,
data: parameter, //参数会在请求体中
timeout: timeout,
responseType: 'blob' //这个必须要写,不写的话,下载下的zip文件无法打开
})
}
后台使用:@RequestBody来接收
public ResponseEntity<byte[]> download(@RequestBody Generator generator, @Context HttpServletResponse response) {
String fileName = generator.getProjectName();
export function downzipAction(url,parameter,method) {
return axios({
url: url,
params: parameter, //参数会在url中
method: method,
responseType: 'blob'
})
}后台使用:@Context来接收
public ResponseEntity<byte[]> download(@Context HttpServletRequest request, @Context HttpServletResponse response) {
Map<String, String> param = MapParserUtil.getAllRequestParam(request);
Generator generator = JsonUtil.jsonToObject(JsonUtil.objectToJson(param),Generator.class);
public static Map<String, String> getAllRequestParam(HttpServletRequest request) {
Map<String, String> res = new TreeMap();
Enumeration<?> temp = request.getParameterNames();
if (null != temp) {
while(true) {
String en;
do {
if (!temp.hasMoreElements()) {
return res;
}
en = (String)temp.nextElement();
String value = request.getParameter(en);
res.put(en, value);
} while(null != res.get(en) && !"".equals(res.get(en)));
res.remove(en);
}
} else {
return res;
}
}