Backend 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 files as character streams
System.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";
//Notify the browser to download the file instead of opening it
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:
< head>
<script> <br>function download_file(url) <br>{ <br><br>if (typeof (download_file.iframe) == "undefined") <br>{ <br>var iframe = document .createElement("iframe"); <br>download_file.iframe = iframe; <br>document.body.appendChild(download_file.iframe); <br>} <br>// alert(download_file.iframe); <br> download_file.iframe.src = url; <br><br>download_file.iframe.style.display = "none"; <br><br><br><br>} <br></script>
aaaaa bbbbb ccccc