不废话,直接上代码。
前端代码:
生成申请单
import axios from 'axios';
export default {
data () {
return {
formValidate: {
orgName: '',
applyName: '',
applyPhone: '',
startDate: '',
endDate: '',
vmemo:''
},
ruleValidate: {
orgName: [
{ required: true, message: '项目名称不能为空!', trigger: 'blur' }
],
applyName: [
{ required: true, message: '申请人不能为空!', trigger: 'blur' }
],
applyPhone: [
{ required: true, message: '电话不能为空!', trigger: 'change' }
],
startDate: [
{ required: true, type: 'date', message: '请输入license有效期!', trigger: 'change' }
],
endDate: [
{ required: true, type: 'date', message: '请输入license有效期!', trigger: 'change' }
],
}
}
},
methods: {
handleSubmit (name) {
this.$refs[name].validate((valid) => {
if (valid) {
axios({
method: 'post',
url: this.$store.getters.requestNoteUrl,
data: this.formValidate,
responseType: 'blob'
}).then(res => {
this.download(res.data);
});
}
});
},
download (data) {
if (!data) {
return
}
let url = window.URL.createObjectURL(new Blob([data]))
let link = document.createElement('a');
link.style.display = 'none';
link.href = url;
link.setAttribute('download', this.formValidate.orgName+'('+ this.formValidate.applyName +')'+'-申请单.doc');
document.body.appendChild(link);
link.click();
}
}
}
后台:
/**
* 生成license申请单
*/
@RequestMapping(value = "/note", method = RequestMethod.POST)
public void requestNote(@RequestBody LicenseRequestNoteModel noteModel, HttpServletRequest req, HttpServletResponse resp) {
File file = null;
InputStream fin = null;
ServletOutputStream out = null;
try {
req.setCharacterEncoding("utf-8");
file = ExportDoc.createWord(noteModel, req, resp);
fin = new FileInputStream(file);
resp.setCharacterEnc