后端实现前端使用Vue2.0 后端使用SpringBoot2 后端实现信息的导出并触发前端浏览器下载功能
将data写入输出流,并response设置参数触发浏览器下载
public class ExportTxt {
public static void export(HttpServletResponse response, TxtData data) throws Exception {
String filename = data.getFileName() + "-" + (new SimpleDateFormat("yyyyMMddHHmmss").format(new Date())) + ".txt";
filename = URLEncoder.encode(filename, "utf-8");
response.setHeader("content-Type", "text/plain");
// 下载文件的默认名称
response.setHeader("Content-Disposition", "attachment;filename=" + filename);
response.addHeader("filename", filename);
response.addHeader("returncode","0000");
exportTxt(data, response.getOutputStream());
}
public static void exportTxt(TxtData data, OutputStream out) throws Exception {
List
此外还可以自定义封装数据结构
@Data
public class TxtData {
// 错误行数-错误信息
private List
前端实现
this.action(this.form).then(res => {
this.$waiting.close()
if (window.navigator && window.navigator.msSaveBlob) {//ie浏览器
window.navigator.msSaveBlob(res.data, decodeURIComponent(res.filename));
} else if (window.URL && window.URL.createObjectURL) {//其他浏览器
let url = window.URL.createObjectURL(res.data)
let a = document.createElement('a')
a.href = url
a.download = decodeURIComponent(res.filename)
document.body.appendChild(a)
a.click()
document.body.removeChild(a)
} else {
this.$alert('您的浏览器不支持下载此文件', '下载文件失败', { type: 'error' })
}
}).catch(err => {
this.$waiting.close()
this.$alert(err, '导出数据失败', { type: 'error' })
})



