JS下載文件|無(wú)刷新下載文件示例代碼
更新時(shí)間:2014年04月17日 11:31:05 作者:
JS下載文件的實(shí)現(xiàn)在網(wǎng)上可以找到很多教程,不過(guò)本文為大家介紹的是無(wú)刷新下載文件,貌似更酷一點(diǎn)是吧
后臺(tái)代碼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";//客戶(hù)端保存的文件名
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";
//通知瀏覽器下載文件而不是打開(kāi)
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;
}
}
}
前端代碼:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script src="jquery-1.7.2.min.js" type="text/javascript"></script>
<title></title>
<script>
function download_file(url)
{
if (typeof (download_file.iframe) == "undefined")
{
var iframe = document.createElement("iframe");
download_file.iframe = iframe;
document.body.appendChild(download_file.iframe);
}
// alert(download_file.iframe);
download_file.iframe.src = url;
download_file.iframe.style.display = "none";
}
</script>
</head>
<body>
<a href="javascript:void(0);" onclick="download_file('Handler.ashx')">aaaaa</a>
<a href="javascript:void(0);" onclick="download_file('Handler.ashx')">bbbbb</a>
<a href="javascript:void(0);" onclick="download_file('Handler.ashx')">ccccc</a>
</body>
</html>
復(fù)制代碼 代碼如下:
<%@ WebHandler Language="C#" Class="Handler" %>
using System;
using System.Web;
public class Handler : IHttpHandler {
public void ProcessRequest (HttpContext context) {
string fileName = "web.config";//客戶(hù)端保存的文件名
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";
//通知瀏覽器下載文件而不是打開(kāi)
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;
}
}
}
前端代碼:
復(fù)制代碼 代碼如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script src="jquery-1.7.2.min.js" type="text/javascript"></script>
<title></title>
<script>
function download_file(url)
{
if (typeof (download_file.iframe) == "undefined")
{
var iframe = document.createElement("iframe");
download_file.iframe = iframe;
document.body.appendChild(download_file.iframe);
}
// alert(download_file.iframe);
download_file.iframe.src = url;
download_file.iframe.style.display = "none";
}
</script>
</head>
<body>
<a href="javascript:void(0);" onclick="download_file('Handler.ashx')">aaaaa</a>
<a href="javascript:void(0);" onclick="download_file('Handler.ashx')">bbbbb</a>
<a href="javascript:void(0);" onclick="download_file('Handler.ashx')">ccccc</a>
</body>
</html>
相關(guān)文章
微信小程序云函數(shù)使用mysql數(shù)據(jù)庫(kù)過(guò)程詳解
這篇文章主要介紹了微信小程序云函數(shù)使用mysql數(shù)據(jù)庫(kù)過(guò)程詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-08-08談?wù)刯s中的prototype及prototype屬性解釋和常用方法
prototype是javascript中筆記難理解的一部分內(nèi)容,下面通過(guò)幾個(gè)關(guān)鍵知識(shí)點(diǎn)給大家講解js中的prototype,對(duì)js中的prototype相關(guān)知識(shí)感興趣的朋友一起學(xué)習(xí)吧2015-11-11基于zepto.js實(shí)現(xiàn)仿手機(jī)QQ空間的大圖查看組件ImageView.js詳解
這篇文章主要介紹了基于zepto.js實(shí)現(xiàn)仿手機(jī)QQ空間的大圖查看組件ImageView.js的源碼和使用方法,并附上一個(gè)使用ImageView.js的實(shí)例,這里分享給大家,有需要的小伙伴參考下。2015-03-03JavaScript實(shí)現(xiàn)動(dòng)態(tài)網(wǎng)頁(yè)時(shí)鐘
這篇文章主要介紹了JavaScript實(shí)現(xiàn)動(dòng)態(tài)網(wǎng)頁(yè)時(shí)鐘,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-06-06Bootstrap jquery.twbsPagination.js動(dòng)態(tài)頁(yè)碼分頁(yè)實(shí)例代碼
這篇文章主要介紹了Bootstrap jquery.twbsPagination.js動(dòng)態(tài)頁(yè)碼分頁(yè)實(shí)例代碼,需要的朋友可以參考下2017-02-02JavaScript判斷用戶(hù)名和密碼不能為空的實(shí)現(xiàn)代碼
下面小編就為大家?guī)?lái)一篇JavaScript判斷用戶(hù)名和密碼不能為空的實(shí)現(xiàn)代碼。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考,一起跟隨小編過(guò)來(lái)看看吧2016-05-05