JS下載文件|無刷新下載文件示例代碼
更新時間:2014年04月17日 11:31:05 作者:
JS下載文件的實現(xiàn)在網(wǎng)上可以找到很多教程,不過本文為大家介紹的是無刷新下載文件,貌似更酷一點是吧
后臺代碼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";//客戶端保存的文件名
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));
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";//客戶端保存的文件名
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));
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ù)庫過程詳解
這篇文章主要介紹了微信小程序云函數(shù)使用mysql數(shù)據(jù)庫過程詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-08-08談?wù)刯s中的prototype及prototype屬性解釋和常用方法
prototype是javascript中筆記難理解的一部分內(nèi)容,下面通過幾個關(guān)鍵知識點給大家講解js中的prototype,對js中的prototype相關(guān)知識感興趣的朋友一起學(xué)習(xí)吧2015-11-11基于zepto.js實現(xiàn)仿手機(jī)QQ空間的大圖查看組件ImageView.js詳解
這篇文章主要介紹了基于zepto.js實現(xiàn)仿手機(jī)QQ空間的大圖查看組件ImageView.js的源碼和使用方法,并附上一個使用ImageView.js的實例,這里分享給大家,有需要的小伙伴參考下。2015-03-03JavaScript實現(xiàn)動態(tài)網(wǎng)頁時鐘
這篇文章主要介紹了JavaScript實現(xiàn)動態(tài)網(wǎng)頁時鐘,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-06-06Bootstrap jquery.twbsPagination.js動態(tài)頁碼分頁實例代碼
這篇文章主要介紹了Bootstrap jquery.twbsPagination.js動態(tài)頁碼分頁實例代碼,需要的朋友可以參考下2017-02-02JavaScript判斷用戶名和密碼不能為空的實現(xiàn)代碼
下面小編就為大家?guī)硪黄狫avaScript判斷用戶名和密碼不能為空的實現(xiàn)代碼。小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考,一起跟隨小編過來看看吧2016-05-05