JS下載文件|無刷新下載文件示例代碼
更新時(shí)間:2014年04月17日 11:31:05 作者:
JS下載文件的實(shí)現(xiàn)在網(wǎng)上可以找到很多教程,不過本文為大家介紹的是無刷新下載文件,貌似更酷一點(diǎn)是吧
后臺代碼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)文章
在 IE 中調(diào)用 javascript 打開 Excel 表
在 IE 中調(diào)用 javascript 打開 Excel 表...2006-12-12
Kettle中使用JavaScrip調(diào)用jar包對文件內(nèi)容進(jìn)行MD5加密的操作方法
這篇文章主要介紹了Kettle中使用JavaScrip調(diào)用jar包對文件內(nèi)容進(jìn)行MD5加密的方法,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09
JavaScript:void(0)用法及一些常見問題解決辦法
這篇文章主要介紹了javascript:void(0)在JavaScript中的用法,探討了其防止鏈接默認(rèn)行為的作用,提供了使用示例,并針對常見問題如與#的區(qū)別、事件綁定和鍵盤訪問進(jìn)行了講解,需要的朋友可以參考下2024-12-12
JS動(dòng)態(tài)高度虛擬列表實(shí)現(xiàn)原理解析
這篇文章將和大家一起探討一下動(dòng)態(tài)高度虛擬列表原理并指出常見虛擬列表采用累計(jì)高度方式存在缺點(diǎn),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-11-11
JavaScript常見函數(shù)類型和用途實(shí)現(xiàn)場景分析
在JavaScript中,除了惰性函數(shù)和防抖函數(shù)外,還有許多其他有用的函數(shù)模式和功能函數(shù),以下是一些常見的函數(shù)類型和用途,感興趣的朋友一起看看吧2024-12-12
innertext , insertadjacentelement , insertadjacenthtml , ins
innertext , insertadjacentelement , insertadjacenthtml , insertadjacenttext 等區(qū)別...2007-06-06

