.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 ...
随机推荐
- 转发:[Python]内存管理
本文为转发,原地址为:http://chenrudan.github.io/blog/2016/04/23/pythonmemorycontrol.html 本文主要为了解释清楚python的内存管理 ...
- powerpoint取色器有什么用|ppt取色器使用教程
在使用powerpoint过程中常常发现一些功能我们很少用到,其实是自己不会用的原因,关于powerpoint取色器有什么用呢?接下来我们一起来学一下ppt取色器使用教程. powerpoint取色器 ...
- 实验五:分析system_call中断处理过程
原创作品转载请注明出处<Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 如果我写的不好或者有误的地方请留言 ...
- PHP之路——VC库
VC库:https://pan.baidu.com/s/1dF9LslV 密码:v7ap
- JS之路——日期函数
时间对象是一个我们经常要用到的对象,无论是做时间输出.时间判断等操作时都与这个对象离不开.除开JavaScript中的时间对象外,在VbScript中也有许多的时间对象,而且非常好用.下面还是按照我们 ...
- Android 判断当前设备是手机还是平板
Android开发需要适配手机和平板,有些需求实现时就要求判断设备是手机还是平板.网上很多说通过设备尺寸.DPI.版本号.是否具备电话功能等进行判断,不过都不算太精确.这里分享一个简洁给力的方法(官方 ...
- Cracking the coding interview--Q2.3
Implement an algorithm to delete a node in the middle of a singly linked list,given only access to t ...
- DEDECMS调用最新评论
{dede:feedback row='5' titlelen='24' infolen='80'} <div class="yhplk"><div>[fi ...
- Java StringTokenizer分割字符串
除了String本身的split方法,JDK在很早就提供了StringTokenizer这个类来分割字符串: 不过,由于正则表达式和String本身的功能越来越多,StringTokenizer已经被 ...
- [Design Pattern] Mediator Pattern 简单案例
Meditor Pattern,即调解模式,用一个调解类类处理所有的沟通事件,使得降低多对象之间的沟通难度,属于行为类的设计模式.为了方便理解记忆,我也称其为,沟通模式. 下面是一个调解模式的简单案例 ...