Record the detailed process of using blob objects to receive java background file streams and download them into xlsx format. The key part of the code is as follows.
First, set the parameters in the response in the java background:
public void exportExcel(HttpServletResponse response, String fileName, String sheetName, List<String> titleRow, List<List<String>> dataRows) { OutputStream out = null; try { // Set the mime type of the browser parsing file. If it is already set in js, you can not set it here // ("application/-excel;charset=gbk"); // Set this item, when downloading Excel files in IE browser, you can pop up the file to display the download window ("Content-Disposition", "attachment;filename=" + (fileName, "UTF-8")); // Allow browsers to access FileName in header ("Access-Control-Expose-Headers", "FileName"); // Set FileName to convert to prevent Chinese garbled code ("FileName", (fileName, "UTF-8")); out = (); (out, sheetName, titleRow, dataRows); (); } catch (Exception e) { if ((out)) { try { (); } catch (IOException e1) { ("Export failed", e); } } throw (("500", "Export failed")); } }
At this time, you can see the response header parameters of the export interface in the debugging panel of the browser as follows:
access-control-allow-credentials: true
access-control-allow-methods: GET,POST,PUT,DELETE,OPTIONS
access-control-allow-origin: :8081
access-control-expose-headers: FileName
connection: close
content-type: application/-excel;charset=gbk
date: Sun, 29 Mar 2020 10:59:54 GMT
filename: %E4%B8%BB%E6%92%AD%E5%88%97%E8%A1%
Next we get the file stream in the front-end code:
handleExport = () => { (`The interface request path to download the file`, {}, { params: { Parameter name1: Parameter value1, Parameter name2: Parameter value2 }, // Set the responseType object format to blob responseType: "blob" }).then(res => { // Create a download link const url = (new Blob([], // Set the mime type of the file, the corresponding mime type here corresponds to .xlsx format {type: 'application/'})); const link = ('a'); = url; // Get the server named file name from the header const fileName = decodeURI(['filename']); ('download', fileName); (link); (); }); };
At this point, you can happily download xlsx format files~
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.