JS下载文件|无刷新下载文件
来源:互联网 发布:网络新骗局 编辑:程序博客网 时间:2024/05/22 01:40
后台代码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>
2 1
- JS下载文件|无刷新下载文件
- js+php实现无刷新下载文件
- 无刷新文件下载Excel文件
- ajax的无刷新文件下载
- HTML无刷新下载文件方法总汇
- angularJS实现无刷新文件下载
- ajax的无刷新文件下载
- ajax无刷新下载文件 虚拟form
- Ajax下载文件(页面无刷新)
- js实现不刷新文件下载功能
- jQ文件无刷新上传插件下载及介绍
- spring mvc + ajax实现无刷新下载文件
- 无刷新实现文件下载,同时带有错误提示
- 强制下载JS文件
- 自动下载js文件
- js下载文件
- js 实现文件下载
- JS实现文件下载
- 1145: n钱买n只鸡
- Java访问MySQL数据库的SqlHelper类
- 随机数选择
- c# 中内部类的简单介绍
- 导出C++类到LUA脚本
- JS下载文件|无刷新下载文件
- 杭电OJ 1007:Quoit Design
- QT中不显示图标的问题
- 第三章 字符串、向量和数组
- webservice简单实例
- shell 求余数
- 关于Spring AOP无法嵌套拦截
- [kruskal]poj 1251
- MAC MYSQL ERROR 1044 (42000)