In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article is about how to use the spring framework ResponseEntity to download files. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Spring framework ResponseEntity implementation file download background code @ RequestMapping ("downLoad") public ResponseEntity downloadPromisePdf () {String fileName = "enterprise integrity commitment .pdf"; try {byte [] pdf = * *; / / byte file String dfileName = new String (fileName.getBytes ("gb2312"), "iso8859-1"); HttpHeaders headers = new HttpHeaders (); headers.setContentType (MediaType.APPLICATION_OCTET_STREAM); headers.setContentDispositionFormData ("attachment", dfileName) Return new ResponseEntity (pdf, headers, HttpStatus.OK);} catch (BuzEx e) {logger.error (e.getMessage ());} catch (Exception e) {} return null;}
Front-end direct _ window.location.href='/downLoad'
ResponseEntity uncompressed multi-file download uncompressed batch file download
The background ResponseEntity code still requests to download one at a time.
The front desk js sends out multiple requests instead.
In js, first use an array to store the file parameter information that needs to be downloaded, and then loop the array to execute the download method. The download method is first ajax to determine whether the file exists. If yes, dynamically create a tag to download the file in batch.
/ / Loop download method $.each (ids,function (iCommand ids) {downLoad (fileFunctionPathArray [I], fileNameInServerArray [I], fileOriginalNameArray [I], ids [I]) after getting the file array }) / / download method function downLoad (fileFunctionPath,fileNameInServer,fileOriginalName,ids) {$.ajax ({/ / check whether the file exists url: "/ ResourceManage/resourceDownloaduserLink/checkPermission", data: {sysuserid: localStorage.getItem ("id"), resourceid: ids} Success: function (data) {/ / if the file exists, create a dynamic a tag batch download file if (data.success) {/ / ResponseEntity download file url var url = ".. / filehandle/downLoad.do?filePlatPath=" + "resource" & fileFunctionPath= "+ fileFunctionPath +" & fileNameInServer= "+ fileNameInServer +" & fileOriginalName= "+ fileOriginalName Var fileName = fileNameInServer; downloadFile (url,fileName) / / dynamically create a tag batch download})} / / dynamically create a tag const downloadFile = (url, fileName ='') = > {let eleLink = document.createElement ('a'); eleLink.download = fileName EleLink.style.display = 'none'; eleLink.href = url; / / due to browser security policy, dynamically created elements must be added to the browser before click document.body.appendChild (eleLink) can be implemented; / / trigger click eleLink.click () / / then remove document.body.removeChild (eleLink);}
If you click to download, it will be downloaded in batch at the same time.
Thank you for reading! On "how to use the spring framework ResponseEntity to achieve file download" this article is shared here, I hope the above content can be of some help to you, so that you can learn more knowledge, if you think the article is good, you can share it out for more people to see it!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.