.net 文件下载方法
public void DownLoadMethod(string FilePath)
{
string hzm = Path.GetExtension(FilePath).ToLower();
string FileName = Path.GetFileNameWithoutExtension(FilePath);
string Name = FileName + hzm;
if (hzm.ToLower() == ".pdf")
{
Response.ContentType = "application/pdf";
Response.AppendHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(Name, System.Text.Encoding.UTF8));
Response.TransmitFile(Server.MapPath(FilePath));
}
else
if (hzm.ToLower() == ".jpeg" || hzm.ToLower() == ".jpg")
{
Response.ContentType = "image/jpeg";
Response.AppendHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(Name, System.Text.Encoding.UTF8));
Response.TransmitFile(Server.MapPath(FilePath));
}
else
if (hzm.ToLower() == ".gif")
{
Response.ContentType = "image/GIF";
Response.AppendHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(Name, System.Text.Encoding.UTF8));
Response.TransmitFile(Server.MapPath(FilePath));
}
else
if (hzm.ToLower() == ".doc" || hzm.ToLower() == "rtf" || hzm.ToLower() == ".docx")
{
Response.ContentType = "Application/msword";
Response.AppendHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(Name, System.Text.Encoding.UTF8));
Response.TransmitFile(Server.MapPath(FilePath));
}
else
if (hzm.ToLower() == ".xls" || hzm.ToLower() == ".xlsx")
{
Response.ContentType = "Application/x-msexcel";
Response.AppendHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(Name, System.Text.Encoding.UTF8));
Response.TransmitFile(Server.MapPath(FilePath));
}
else
if (hzm.ToLower() == ".txt")
{
Response.ContentType = "text/plain";
Response.AppendHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(Name, System.Text.Encoding.UTF8));
Response.TransmitFile(Server.MapPath(FilePath));
}
else
if (hzm.ToLower() == ".htm" || hzm.ToLower() == ".html")
{
Response.ContentType = "text/HTML";
Response.AppendHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(Name, System.Text.Encoding.UTF8));
Response.TransmitFile(Server.MapPath(FilePath));
}
else
{
if (hzm.ToLower() == ".zip")
{
//以字符流的形式下载文件
FileStream fs = new FileStream(Server.MapPath(FilePath), FileMode.Open);
byte[] bytes = new byte[(int)fs.Length];
fs.Read(bytes, 0, bytes.Length);
fs.Close();
Response.ContentType = "application/octet-stream";
//通知浏览器下载文件而不是打开
Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(Name, System.Text.Encoding.UTF8));
Response.BinaryWrite(bytes);
Response.Flush();
Response.End();
}
}
}
//下载 压缩文件 *.zip
private void DownloadFiles(string url)
{
if (url.Equals(""))
{
return;
}
try
{
if (!File.Exists(Server.MapPath(url)))
{
return;
}
FileInfo fi = new FileInfo(Server.MapPath(url));
Response.Clear();
Response.ContentType = "application/octet-stream";
string fileName = fi.FullName.Substring(fi.FullName.LastIndexOf("\\") + 1);
if (fileName.Length > 32 && fileName.Split('-').Length >= 5)
{
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(fi.FullName.Substring(fi.FullName.LastIndexOf("\\") + 1).Substring(fi.FullName.Substring(fi.FullName.LastIndexOf("\\") + 1).IndexOf('_') + 1), System.Text.Encoding.UTF8));
}
else
{
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(fi.FullName.Substring(fi.FullName.LastIndexOf("\\") + 1), System.Text.Encoding.UTF8));
}
Response.AppendHeader("Content-Length", fi.Length.ToString());
Response.WriteFile(fi.FullName);
Response.Flush();
Response.End();
}
catch (FileNotFoundException ex)
{
Response.Write(ex.Message);
}
}
.net 文件下载方法的更多相关文章
- struts2 实现文件下载方法汇总
http://pengranxiang.iteye.com/blog/259401 一.通过struts2提供的下载机制下载文件: 项目名为 struts2hello ,所使用的开发环境是MyEcli ...
- spring+jpg环境下,spring实现文件下载web实现通用的文件下载方法
jar包复制到WEB-INF 文件夹lib下: commons-io-1.3.2.jar public static String download(HttpServletRequest reques ...
- .net文件下载方法汇总
转载自:http://blog.sina.com.cn/s/blog_680942070101ahsq.html //TransmitFile实现下载 protected void Button1_C ...
- Xutils呼叫流源代码文件下载方法
//我主要是好奇Xutils哪里回调onLoading(),查找等了很久也没找到,果然easy查找只是把它写下来 前言: 1.代码摘要只有主线,提供一般流程 2.为了易于理解,码变量名,而是类名的驼峰 ...
- .net core webapi带权限的文件下载方法
众所周知,在webapi中,如果有个接口需要权限,一般会将带权限的字段塞进header中.但是,在带权限的文档下载接口中,无论是用post,还是get方式,我们无法设置header头信息.苦恼呀?别急 ...
- base64格式文件下载方法
下载图片时,接口返回的地址是base64格式的文件数据,因为页面需要把base64格式的数据转换为文件,再进行下载: 解决方案: 下载按钮: <el-button type="defa ...
- 一个简单的PHP文件下载方法 download
<?php /* * *@param function downloadFile 文件下载 * *@param string $filename 下载文件的路径(根目录下的绝对路径) * *@p ...
- php文件下载方法收藏(附js下载技巧)
function down($url){ header('Content-Description: File Transfer'); header('Content-Type: application ...
- Chrome浏览器M53更新后超链接的dispatchEvent(evt)方法无法触发文件下载
一个经典的js前台文件下载方法: var aLink = document.createElement('a'); var datatype="data:text/plain;charset ...
随机推荐
- 【行为型】Strategy模式
策略模式意图将解决问题的算法分别封装成一个个对象的形式,并使这些算法对象相互间可被替换.模式比较简单,对于策略对象结构的设计,可抽象一个抽象基类,并定义好相关算法(纯)虚接口,并由各种具体的实现算法子 ...
- JQuery学习笔记【CSS选择符】--02
Jquery的程序入口: <html> <head> <title></title> <script type="text/javasc ...
- 【新手--android日记】实现IOS风格电话界面
[前言--新手日记] 开始学习android开发,通过做一个通讯录练习,打算实现各种自己想实现的功能. 新手作品,技术含量很浅.主要是记录自己的学习过程. 纯学习之用,求评论,求建议,求教导. [正题 ...
- 怎样在Swift中使用CocoaPods-b
最近关于CocoaPods有很多的议论.你可能从别的开发者那里听到过,或者在Github的目录中看到过.如果你之前从来没有用过,你可能会问,"CocoaPods到底是什么?" 它不 ...
- 利用github for windows 工具将本地的内容同步到github上
1 利用github for windows工具来创建一个Repository,名字叫weixin,具体步骤 请参考这篇文章 根据default storage directory的设置 我知道我所创 ...
- 多备份CEO胡茂华:创业路上的五道坎
本文由多备份CEO胡茂华记述,授权南七道发表,未做删改.胡茂华:腾讯第116号员工,历任腾讯总监.盛大CTO (旅游).1号店技术副总裁.现担任云服务提供商多备份联合创始人&CEO. 2014 ...
- 【转】显示Ubuntu文件浏览器的地址栏--不错
原文网址:http://www.blogbus.com/anythingok-logs/144447448.html Ubuntu默认使用nautilus作为其可视化的文件浏览器,其默认值不显示地址栏 ...
- Delphi TcxTreeList 怎们显示下拉列表
TcxTreeList 怎们显示下拉列表, 操作如下: 1.定义一个TStringlist中存储你的信息. 2.然后选定在TcxTreeList的哪个列要变成TCombobox, 如下图: 3. ...
- HDOJ(HDU) 1720 A+B Coming(进制)
Problem Description Many classmates said to me that A+B is must needs. If you can't AC this problem, ...
- DLL入门浅析(2)——如何使用DLL
转载自:http://www.cppblog.com/suiaiguo/archive/2009/07/20/90621.html 上文我简单的介绍了如何建立一个简单DLL,下面再我简单的介绍一下如何 ...