例子代码:

public void Down()
{
TransmitFile(@"/File/KBPub.zip");
}
public void TransmitFile(string filePath) //filePath 下载的文件的相对路径
{
try
{
filePath = Server.MapPath(filePath);
if (System.IO.File.Exists(filePath))
{
FileInfo info = new FileInfo(filePath);
long fileSize = info.Length;
System.Web.HttpContext.Current.Response.Clear();
System.Web.HttpContext.Current.Response.ContentType = "application/x-zip-compressed";
System.Web.HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + filePath.Substring(filePath.LastIndexOf("\\") + 1)); //filename 下载后显示的文件名
//不指明Content-Length用Flush的话不会显示下载进度的
System.Web.HttpContext.Current.Response.AddHeader("Content-Length", fileSize.ToString());
System.Web.HttpContext.Current.Response.TransmitFile(filePath, 0, fileSize);
System.Web.HttpContext.Current.Response.Flush();
}
}
catch
{ }
finally
{
System.Web.HttpContext.Current.Response.Close();
}

}

WriteFile方式:

public void WriteFile(string filePath)
{
try
{
filePath = Server.MapPath(filePath);
if (System.IO.File.Exists(filePath))
{
FileInfo info = new FileInfo(filePath);
long fileSize = info.Length;
System.Web.HttpContext.Current.Response.Clear();
System.Web.HttpContext.Current.Response.ContentType = "application/octet-stream";
System.Web.HttpContext.Current.Response.AddHeader("Content-Disposition", "attachement;filename=" + Server.UrlEncode(info.FullName));
//指定文件大小
System.Web.HttpContext.Current.Response.AddHeader("Content-Length", fileSize.ToString());
System.Web.HttpContext.Current.Response.WriteFile(filePath, 0, fileSize);
System.Web.HttpContext.Current.Response.Flush();
}
}
catch
{ }
finally
{
System.Web.HttpContext.Current.Response.Close();
}
}

TransmitFile下载文件(部分转载)的更多相关文章

  1. 每天一个linux命令(26):用SecureCRT来上传和下载文件(转载自竹子)

    用SSH管理linux服务器时经常需要远程与本地之间交互文件.而直接用SecureCRT自带的上传下载功能无疑是最方便的,SecureCRT下的文件传输协议有ASCII.Xmodem.Zmodem. ...

  2. linux上传下载文件(转载https://www.jb51.net/article/143112.htm)

    转载于:https://www.jb51.net/article/143112.htmLinux下目录复制:本机->远程服务器 1 scp -r /home/shaoxiaohu/test1 z ...

  3. response下载文件 (转载)

    核心代码: ? DataSet ds = dBll.GetList("ID=" + ID); ? string docName = "a.doc";//文件名, ...

  4. 转载: 正确处理浏览器在下载文件时HTTP头的编码问题(Content-Disposition)

    最近在做一个下载工具时,发现CSDN上的资源下载时竟然没有被拦截到,经过分析,终于有了一个发现,解决了我之前做文件下载时的乱码问题,所以转载这篇释疑文章,希望有人可以看到,可以从中得到帮助,也用来备忘 ...

  5. libcurl开源库在Win7 + VS2012环境下编译、配置详解 以及下载文件并显示下载进度 demo(转载)

    转载:http://blog.csdn.net/fengshuiyue/article/details/39530093(基本教程) 转载:https://my.oschina.net/u/14207 ...

  6. 【转载】C# 从服务器下载文件

    支持并尊重原创!原文地址:https://www.cnblogs.com/GoCircle/p/6429136.html 一.//TransmitFile实现下载 protected void But ...

  7. C#实现FTP文件夹下载功能【转载】

    网上有很多FTP单个文件下载的方法,前段时间需要用到一个FTP文件夹下载的功能,于是找了下网上的相关资料结合MSDN实现了一段FTP文件夹下载的代码. 实现的思路主要是通过遍历获得文件夹下的所有文件, ...

  8. ajax请求不能下载文件(转载)

    最近在做文件下载,后台写了个控制层,直接走进去应该就可以下载文件,各种文件图片,excel等 但是起初老是下载失败,并且弹出下面的乱码: 前台请求代码: $('#fileexcel').unbind( ...

  9. java web 下载文件 response.setHeader()的用法 (转载)

    response.setHeader()的用法 response.setHeader()下载中文文件名乱码问题 收藏 1. HTTP消息头 (1)通用信息头 即能用于请求消息中,也能用于响应信息中,但 ...

随机推荐

  1. START167 AND BOOT167

    http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.faqs/ka10535.html C166: START167 AND BOO ...

  2. js页面文字选中后分享到新浪微博实现

    demo您可以狠狠地点击这里:js文字选中分享到新浪微博demo 方法与代码 选中即分享的功能看上去比较高级,其实实现是相当简单的.其中的会让人头大,一般人也不感兴趣的原理这里就直接跳过.这个js文字 ...

  3. shared_ptr注意事项

    对shared_ptr的Copy构造和Copy赋值,会改变引用计数,但是对shared_ptr中原始资源的Copy构造和Copy赋值,不会改变引用计数.因此存在下面的危险情况: 1.获取资源时,初始化 ...

  4. 在C#中利用Nuget包使用SQLite数据库和Linq to SQLite

    本来是学习在VB中使用SQLite数据库和Linq to SQLite,结果先学习到了在C#中使用SQLite数据库和Linq to SQLite的方法,写出来与大家共同学习.(不知道算不算不务正业) ...

  5. CheckBoxList 只能选2个选项

    // ////下面判断用户是否多选,每选择一次比较一次,看是否超过规定名额 //string ClickedItem = Request.Form["__EVENTTARGET"] ...

  6. MySQL--索引条件下推优化

    http://blog.163.com/li_hx/blog/static/1839914132015782821512/ 一 什么是“索引条件下推” “索引条件下推”,称为 Index Condit ...

  7. 去掉或者修改 input、select 等表单的【默认样式 】

    隐藏input等表单的默认样式的背景: textarea,select,input{-webkit-appearance: none; -moz-appearance: none; -o-appear ...

  8. flume监控之ganglia

    对于日志来说,我觉得监控意义不大,因为写的速度一般不会特别快,但是如果是spooldir source,里面一小时放入十几G的数据让flume解析,特别是在结合kafka或者其他框架的话,监控就显得重 ...

  9. C# 数据的加密解密

    /// <summary> /// 加密数据 /// </summary> /// <param name="Text"></param& ...

  10. SpringMVC上传(通过物理路径)

    内容来源:http://blog.csdn.net/kouwoo/article/details/40507565 一.配置文件:SpringMVC 用的是 的MultipartFile来进行文件上传 ...