JS file download | no need to download new files

Source: Internet
Author: User

Background code Handler. ashx

<% @ WebHandler Language = "C #" Class = "Handler" %> using System; using System. web; public class Handler: IHttpHandler {public void ProcessRequest (HttpContext context) {string fileName = "web. config "; // the file name saved by the client string filePath = context. server. mapPath ("web. config "); // path // download the file System in the form of a response stream. IO. fileStream fs = new System. IO. fileStream (filePath, System. IO. fileMode. open); byte [] bytes = new byte [(int) fs. length]; fs. read (bytes, 0, bytes. length); fs. close (); context. response. contentType = "application/octet-stream"; // notifies the browser to download the file instead of opening the context. response. addHeader ("Content-Disposition", "attachment; filename =" + HttpUtility. urlEncode (fileName, System. text. encoding. UTF8); context. response. binaryWrite (bytes); context. response. flush (); context. response. end () ;}public bool IsReusable {get {return false ;}}}

Front-end code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">



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.