JS下载文件|无刷新下载文件示例代码_javascript技巧

WBOY
リリース: 2016-05-16 16:52:16
オリジナル
1281 人が閲覧しました

后台代码Handler.ashx

复制代码 代码如下:



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;
}
}

}

前端代码:
复制代码 代码如下:








<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 = iframe; <BR>document.body.appendChild(download_file.iframe); <BR>} <BR>// alert(download_file.iframe); <BR>download_file.iframe.src = url; <br><br>download_file.iframe.style.display = "none"; <br><br><br><br>} <BR></script>


aaaaa
bbbbb
ccccc



ソース:php.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
最新の問題
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!