後台程式碼Handler.ashx
using System;
using System.Web;
public class Handler : IHttpHandler {
public void ProcessRequest (HttpHContext ) {
string fileName = "web.config";//客戶端儲存的檔案名稱
string filePath = context.Server.MapPath("web.config");//路徑
//以字元流的形式下載檔案
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";
////通知瀏覽器下載檔案而不是開啟
context.Response.AddHeader("Content-Disposition", "attachment; filename=" HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
contextcode. Response.BinaryWrite(bytes);
context.Response.Flush();
context.Response.End();
}
public bool IsReusable {
}
public bool IsReusable {
} return false;
}
}
}
代碼如下:
<script> <BR>function download_file( url) <br>{ <br><BR>if (typeof (download_file.iframe) == "undefined") <BR>{ <BR>var iframe = document.createElement("iframe"); <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>🎜>🎜 = url; >download_file.iframe.style.display = "none"; <br><br><br><BR>} <BR></script>
aaaaa
bbbbb ccccc 身體>