Example of js download and file name Modification

Source: Internet
Author: User
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 ();}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.