Code backend Handler.ashx
<%@ WebHandler Language = "C#" Class="Handler" %>
using System ;
using System.Web ;
gestionnaire de classe public : IHttpHandler {
public void ProcessRequest ( HttpContext context) {
string fileName = "web.config";//Le nom du fichier enregistré par le client
string filePath = context.Server.MapPath("web.config");//Path
/ /Télécharger les fichiers sous forme de flux de caractères
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(); 🎜> //Indiquer au navigateur de télécharger le fichier au lieu de l'ouvrir
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;
}
}
}
Code frontal :
< 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 = <br>document.body.appendChild(download_file.iframe); <br>// alert(download_file.iframe); <br> download_file.iframe.src = url; <br><br>download_file.iframe.style.display = "aucun"; 🎜></script>
aaaaa bbbbb ccccc