前端
const my_data = {
login_name: window.localStorage.getItem("login_name"),
file_name: 'export_student_list.xls'
};
axios({
url: '/admin/getFileByName',
method: 'POST',
data:JSON.stringify(my_data),
responseType: 'blob',
}).then((response) => {
const url = window.URL.createObjectURL(new Blob([response.data]));
const link = document.createElement('a');
link.href = url;
link.setAttribute('download', '学生名单.xls');
document.body.appendChild(link);
link.click();
});
后端
public void getFileByName() {
String json = getRawData();
JSONObject jo = new JSONObject(json);
String fileName = jo.getString("file_name");
fileName = AdminConfig.File_Path+fileName;
if(new File(fileName).exists()) {
System.out.println("文件存在!");
renderFile(new File(fileName));
return;
}
}