刚开始学习Asp的时候我们实现下载功能可能是这样

<form id="form1" runat="server">ASP:<asp:Button runat="server" ID="btn2" Text="下载文件" OnClick="btn2_onclick"/>
</form>
protected void btn2_onclick(object sender, EventArgs e)
{string fileName = "test.txt";string filePath = HttpContext.Current.Server.MapPath("~/" + fileName);if (File.Exists(filePath)){Response.ContentType = "application/unknow";Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName + "");Response.TransmitFile(filePath);}
}

后来想用Ajax,于是开始动手做

<div>Ajax: <input type="button" id="btn1" value="下载文件"/>
</div>
<script src="jquery.min.js"></script>
<script>$(document).ready(function () {$('#btn1').click(function () {$.post("HandlerDownFile.ashx", { fileName: 'test.txt' })});})
</script>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;namespace WebApplication1
{/// <summary>/// HandlerDownFile 的摘要说明/// </summary>public class HandlerDownFile : IHttpHandler{public void ProcessRequest(HttpContext context){try{string fileName = context.Request["fileName"];string filePath = context.Server.MapPath("~/" + fileName);if (File.Exists(filePath)){context.Response.ContentType = "application/unknow";context.Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName + "");context.Response.TransmitFile(filePath);}}catch (IOException ex){    throw new Exception(ex.Message,ex);}finally{context.Response.Close();}}public bool IsReusable{get{return false;}}}
}

然后发现问题来了,我返回的数据流前端没接头人,那怎么保存?

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="DownFile.aspx.cs" Inherits="WebApplication1.login" %><!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>下载文件</title><script src="jquery.min.js"></script><script>$(document).ready(function () {$('#btn1').click(function () {var xhr = new XMLHttpRequest();xhr.open("POST", "HandlerDownFile.ashx?fileName=test.txt", true);xhr.responseType = "blob";xhr.onload = function () {if (this.status == 200) {var blob = this.response;var reader = new FileReader();reader.readAsDataURL(blob);reader.onload = function (e) {var a = document.createElement('a');a.download = "test.txt";a.href = e.target.result;document.body.appendChild(a);a.click();document.body.removeChild(a);}}};xhr.send();});})</script>
</head>
<body><div>Ajax: <input type="button" id="btn1" value="下载文件"/></div>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;namespace WebApplication1
{/// <summary>/// HandlerDownFile 的摘要说明/// </summary>public class HandlerDownFile : IHttpHandler{public void ProcessRequest(HttpContext context){try{string fileName = context.Request["fileName"];string filePath = context.Server.MapPath("~/" + fileName);if (File.Exists(filePath)){FileStream fs = new FileStream(filePath, 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=" + fileName + "");context.Response.BinaryWrite(bytes);context.Response.Flush();context.Response.End();}}catch (IOException ex){    throw new Exception(ex.Message,ex);}}public bool IsReusable{get{return false;}}}
}

Ajax实现下载文件功能相关推荐

  1. springboot ajax下载文件功能封装

    通过js ajax下载文件功能封装 function exportExcel(formId, url) {try {var queryForm = $("#" + formId); ...

  2. android ftp同步程序,ftp同步 安卓,安卓手机ftp上传下载文件功能同步视频照片

    手机拍照越来方便,手机里的照片也越积越多,手机运行缓慢,本文利用安卓的每步FTP服务APP来自动实现手机视频照片的同步,释放手机被占用的存储空间.在机顶盒上运行每步FTP服务,机顶盒USB口连接U盘做 ...

  3. js ajax上传文件功能

    js ajax上传文件功能 ajax请求 php接收文件(yii框架) ajax请求 <form enctype="multipart/form-data"><i ...

  4. 关于Ajax无法下载文件到浏览器本地的问题(文件下载的几种方式)

    最近在做网站的时候遇到这样一个功能,在如图所示的页面中,需要用户点击链接的时候,能够以异步Ajax的方式判断服务器中是否存储有相应的Excel文件,如果没有的话就提示用户没有找到,如果有的话就下载到用 ...

  5. ajax实现下载文件进度条及方法详解

    javascript使用ajax下载文件进度条实现 <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml&qu ...

  6. Ajax请求导出Excel的问题【ajax不能下载文件】

    最近要给后台添加操作日志导出的功能,后台已经生成.xls文件,本来想只要ajax请求下就完事儿,想象总是美好的,可不管我怎么ajax,想了各种可能原因也无法解决. 问题描述 前端发送ajax[get/ ...

  7. Android开发丶一步步教你实现okhttp带进度的列表下载文件功能

    大家好,我又回来了! 标题好像又起的不知所云,但是貌似也想不起更好的标题,看看效果图 现在有个文件列表,每个列表标签都有一个下载的按钮,点击以下载对应的文件,如果已下载则显示"已下载&quo ...

  8. Linux服务器安装anaconda|并配置jupyter禁止下载文件功能

    一.Linux服务器安装anaconda 1.下载anaconda 1.1 [服务器未联网方案]本地下载好,然后上传到服务器上.下载地址:https://repo.anaconda.com/archi ...

  9. Selenium3自动化测试——13.下载文件功能

    1. 实现目标 下载selenium官网的3.141.0.tar.gz,到脚本所有目录中. 2. 实现代码 这里注意: prefs = {'profile.default_content_settin ...

最新文章

  1. 「大咖云集硅谷AI大会」人工智能商业化的趋势与挑战
  2. 性能调优常见问题与方案
  3. 菜鸟学Java(十一)——GET与POST
  4. Python基础教程(二):基础语法、变量类型
  5. QT的QRadioButton类的使用
  6. 【elasticsearch系列】windows安装IK分词器插件
  7. python永久保存数据_Python学习笔记(四)——文件永久存储
  8. 树莓派3代linux,树莓派 3B 入门 ARMv8 Arch Linux
  9. jumpserver堡垒机 (资源)
  10. Mysql安装相关问题
  11. java多线程和长连接,三方转换通信的实践(2)——数据库端服务程序
  12. IE8无法取得客户端完整路径的解决办法
  13. 通过Python对商品销售数据预测
  14. 后台管理页面(bootdo)
  15. 计算机在录制声音过程中流向,Bandicam中录制电脑声音的具体流程介绍
  16. ffmpeg水平翻转视频,附批量处理脚本
  17. 【清北学堂】dwarf
  18. linux串口 cat,Linux命令操作之cat与cut
  19. 区块链隐私保护文献 An Efficient NIZK Scheme for Privacy-Preserving Transactions over Account-Model Blockchain
  20. 【计算机毕业设计】123网上商城系统设计与实现

热门文章

  1. 湖南科技职业学院计算机网络技术在哪个校区,湖南科技职业学院地址在哪里
  2. 影建无不ProtaBIM 2016 sp5 for Revit 2015 1CD
  3. Go 开源说第二期:GORM 剖析与最佳实践
  4. caxa图文档服务器未启动,05_CAXA图文档2013(实施指南)剖析.pptx
  5. AHP层次分析法理解以及项目实战
  6. 天津科技大学计算机组成原理答案,2017年天津科技大学842自命题计算机学科专业基础综合[专硕]之计算机组成原理考研强化模拟题...
  7. 基于启扬i.MX6UL的免疫荧光分析仪解决方案
  8. 22产品经理需要具备的推广能力
  9. 达梦数据库DCA认证培训经历
  10. 图像灰度化的三种常见方法源码