我使用弹簧靴2,百里香和bootsrap 4。
用户进行搜索后,我有一个链接可以将结果导出到excel。 所以我需要将所有参数传递给链接
<a class="dropdown-item btn btn-primary" id="searchExportExcel" href="#" role="button" onclick="updateExcelDownloadLink();">Excel</a>
JavaScript部分
function updateExcelDownloadLink() {
debugger;
var id = $("#searchSamplesForm input[name=id]").val();
var buildDate = "";
if ($("#searchSamplesBuildDatePicker").val() != "") {
buildDate = $("#searchSamplesBuildDatePicker").data('daterangepicker').startDate.format('YYYY-MM-DD');
}
var productTypesId = $("#searchSamplesForm select[name=productTypesId]").val();
var productsId = $("#searchSamplesForm select[name=productsId]").val();
var dimensionsId = $("#searchSamplesForm select[name=dimensionsId]").val();
var colorsId = $("#searchSamplesForm select[name=colorsId]").val();
var factoriesId = $("#searchSamplesForm select[name=factoriesId]").val();
var machinesId = $("#searchSamplesForm select[name=machinesId]").val();
var suppliersId = $("#searchSamplesForm select[name=suppliersId]").val();
var buildStartDate = "";
if ($("#searchSamplesBuildDatePicker").val() != "") {
buildStartDate = $("#searchSamplesBuildDatePicker").data('daterangepicker').startDate.format('YYYY-MM-DD');
}
var buildEndDate = "";
if ($("#searchSamplesBuildEndDatePicker").val() != "") {
buildEndDate = $("#searchSamplesBuildEndDatePicker").data('daterangepicker').startDate.format('YYYY-MM-DD');
}
var passing = $("#passing").prop('checked');
var retained = $("#retained").prop('checked');
var exportFileType = "excel";
var url = "/search/export";
var data = 'id=' + id +
'&buildDate=' + buildDate +
'&productTypesId=' + productTypesId +
'&productsId=' + productsId +
'&dimensionsId=' + dimensionsId +
'&colorsId=' + colorsId +
'&factoriesId=' + factoriesId +
'&machinesId=' + machinesId +
'&suppliersId=' + suppliersId +
'&buildStartDate=' + buildStartDate +
'&buildEndDate=' + buildEndDate +
'&passing=' + passing +
'&retained=' + retained +
'&exportFileType=' + exportFileType;
var link = document.getElementById("searchExportExcel");
window.open(
link.href,
'_blank'
);
link.innerHTML = "Excel";
link.setAttribute('href', url + "?" + data);
return false;
}
在服务器端
private ResponseEntity<byte[]> prepareExcelExport(byte[] content) throws IOException {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.parseMediaType("application/vnd.ms-excel"));
String filename = "exports.xls";
headers.setContentDispositionFormData(filename, filename);
headers.setCacheControl("must-revalidate, post-check=0, pre-check=0");
ResponseEntity<byte[]> response = new ResponseEntity<>(content, headers, HttpStatus.OK);
return response;
}
实际上,当我单击打开带有文件对话框的第二个选项卡的链接以保存文件时,我单击了保存。
第二个标签页关闭,并显示一个新文件对话框。
不明白为什么会出现两次文件对话框