在 html 文件中,我有一个包含员工联系方式的表格。 该表有不同的过滤器,还有一个下载文件按钮。
当按钮被按下时,函数被调用
function ParseDocument() {
var table = document.getElementById('allContact');
var contactDTOList = new Array();
var i =0;
for (var r = 2, n = table.rows.length; r < n; r++) {
if (table.rows[r].style.display !== 'none') {
var s = table.rows[r].cells[0].innerHTML;
var bits = s.split(/\D/);
var date = new Date(bits[0], --bits[1], bits[2], bits[3], bits[4], bits[5]);
var contactDTO = {
dateOfBirth:date.getTime(),
firstName:table.rows[r].cells[1].innerHTML,
lastName:table.rows[r].cells[2].innerHTML,
email:table.rows[r].cells[3].innerHTML
};
contactDTOList.push(contactDTO);
i++;
}
}
$.ajax({
type: 'POST',
url: "/rest/bot5",
data: JSON.stringify(contactDTOList),
contentType: "application/json;charset=UTF-8",
async: true,
success: function () {
}
});
}
接下来,所有通过过滤器的contacts都落入controller java
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
@RestController
@RequestMapping(value = "/test")
public class BotRestController {
@PostMapping(value = "/bot5", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public FileOutputStream postOrderingTask(@RequestBody List<Contact> contactList) throws IOException {
String[] columns = {"First Name", "Last Name", "Email", "Date Of Birth"};
List<Contact> contacts = new ArrayList<Contact>();
contacts.add(new Contact("Sylvain", "Saurel", "sylvain.saurel@gmail.com", "2018-09-25 16:28:47"));
contacts.add(new Contact("Albert", "Dupond", "sylvain.saurel@gmail.com", "2018-09-25 17:28:47"));
contacts.add(new Contact("Pierre", "Dupont", "sylvain.saurel@gmail.com", "2018-09-25 18:28:47"));
contacts.add(new Contact("Mariano", "Diaz", "sylvain.saurel@gmail.com", "2018-09-25 19:28:47"));
Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("Contacts");
Font headerFont = workbook.createFont();
headerFont.setBold(true);
headerFont.setFontHeightInPoints((short) 14);
headerFont.setColor(IndexedColors.RED.getIndex());
CellStyle headerCellStyle = workbook.createCellStyle();
headerCellStyle.setFont(headerFont);
// Create a Row
Row headerRow = sheet.createRow(0);
for (int i = 0; i < columns.length; i++) {
Cell cell = headerRow.createCell(i);
cell.setCellValue(columns[i]);
cell.setCellStyle(headerCellStyle);
}
// Create Other rows and cells with contacts data
int rowNum = 1;
for (Contact contact : contacts) {
Row row = sheet.createRow(rowNum++);
row.createCell(0).setCellValue(contact.firstName);
row.createCell(1).setCellValue(contact.lastName);
row.createCell(2).setCellValue(contact.email);
row.createCell(3).setCellValue(contact.dateOfBirth);
}
// Resize all columns to fit the content size
for (int i = 0; i < columns.length; i++) {
sheet.autoSizeColumn(i);
}
// Write the output to a file
FileOutputStream fileOut = new FileOutputStream("contacts.xlsx");
workbook.write(fileOut);
fileOut.close();
return fileOut;
}
}
单击 html 中的按钮时如何下载包含联系人的文件?
最佳答案
您已将所有详细信息保存在 contacts.xlsx 中。现在响应从部署目录发送文件路径。 说 path =/projectname/contacts.xlsx 在成功函数中使用 window.open(ip:port://path);
在 ajax 调用中 success: function () 应该是 success: function (data) 在 data 变量中你会得到服务器的响应
关于javascript - 如何从 Java Controller 下载文件到 javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52495935/