.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 ...
随机推荐
- 百度地图api窗口信息自定义
百度地图加载完后,完全可以用dom方法操作,比较常用的就是点击mark的弹窗,利用jQuery可以很快的创建弹窗,需要注意的就是地图都是异步加载,所以绑定时间要用 jQuery 事件 - delega ...
- jquery easy ui 学习 (3) window 限制在父类窗体内
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- 如何让div出现滚动条
由于DIV本身属性并没有滚动条设置,但是有些地方的设计却需要出现滚动条,如何实现呢?本人采用CSS样式来控制显示!而且也很简单,代码如下: <div style="OVERFLOW-Y ...
- Scut:脚本引擎
Scut 可以执行 C#.Python.Lua 三种类型的脚步,Scut 是如何加载并传递参数的呢? 首先值得注意的是:Scut 在编译时就会将逻辑层脚本源码复制到bin/Script的目录下. 1. ...
- didEndEditingRowAtIndexPath with nil indexPath
在UITableViewController中,通过滑动删除按钮删除一行,首先收到Table view data source call: tableView:commitEditingStyle:f ...
- 【性能测试】【Jmeter】学习(2)——录制一段脚本
打开JMeter工具,录制一段脚本(我做的是录制登陆网站后点击设备的开关设定,然后再退出) 1).创建一个线程组(右键点击“测试计划”--->“添加”---->“线程组”) 2).添加录制 ...
- SVN项目库错误Unsupported FS format svn: Expected FS format between '1' and '4'; found format '6'
SVN项目库错误Unsupported FS format svn: Expected FS format between '1' and '4'; found format '6' 从这里找到解决方 ...
- HDOJ(HDU) 2078 复习时间
Problem Description 为了能过个好年,xhd开始复习了,于是每天晚上背着书往教室跑.xhd复习有个习惯,在复习完一门课后,他总是挑一门更简单的课进行复习,而他复习这门课的效率为两门课 ...
- 构建你的第一个App
Building Your First App 原文链接:http://developer.android.com/training/basics/firstapp/index.html 译文链接1: ...
- Robot Framework 项目搭建
首先新建一个项目“RobotDemo".项目Type一般选择“Directory”形式. 项目第一层可以放3种文件:Test Suite.Directory 和 Resource File. ...