This article describes in detail the code for downloading and modifying file names in js, which has some reference value, interested friends can refer to this article for details about the code for downloading files and modifying file names in js, which has some reference value. Interested friends can refer
Use js to download files, use tags, and add the download attribute.
var a = document.createElement("a");a.href = "XXX.com/audioStream/8a9dbae9d0859e48fc1f590fcf6d4ccc.mp3";a.download ="test.mp3";a.click();
However, if you want to rename the file, it seems that js cannot be implemented.
Therefore, the background implementation is considered. A java proxy request is used to obtain the file setting file name and return it to the front-end.
Public void downFiles (HttpServletResponse response, String url, String workInfoId, int type) {try {String prefix = type = 1? "Wav": "txt"; url = type = 1? Url: (url + "? TextInfoId = "+ workInfoId); HttpEntity entity = Request. get (url ). execute (). returnResponse (). getEntity (); byte [] bys = EntityUtils. toByteArray (entity); // get the work name Works = this. worksDao. findByWorkId (workInfoId); String name = (works! = Null & StringUtils. isNotBlank (works. getName ()))? Works. getName (): Long. toString (new Date (). getTime (); response. setHeader ("Content-Disposition", "attachment; filename =" + new String (name. getBytes ("UTF-8"), "ISO-8859-1") + ". "+ prefix); OutputStream out = response. getOutputStream (); out. write (bys); out. close ();} catch (Exception e) {e. printStackTrace ();}}