.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 ...
随机推荐
- bzoj2067: [Poi2004]SZN
Description String-Toys joint-stock 公司需要你帮他们解决一个问题. 他们想制造一个没有环的连通图模型. 每个图都是由一些顶点和特定数量的边构成. 每个顶点都可以连向 ...
- How Many Points of Intersection?
uva10790:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_prob ...
- SCALA当的trait
不是特别懂,但感觉和RUBY当中的MIX-IN功能有几分相似,这又扯到了多重继承及JAVA当中的接口虚拟类了.. package com.hengheng.scala class UseTrait { ...
- UMHexagonS搜索过程
通过相邻块的预测得到mvp后,会以mvp为基础搜索最佳的匹配块,UMHexagonS就是h.264中用的一种搜索算法. UMHexagonS是一种整像素搜索算法,也就是搜索过程中,参考图像一直都是原来 ...
- -_-#Error
Error: Argument passed in must be a single String of 12 bytes or a string of 24 hex characters Mongo ...
- uglifyjs压缩js文件(指令压缩/ 批量压缩/ 编程方式压缩)
一.指令压缩 1.安装node,npm——详细见nodejs安装与使用入门 2.安装 uglifyjs——npm install -g uglify-js 3.压缩例子:1)uglifyjs mai ...
- 抽象基类(ABC),纯虚函数
#ifndef _ACCTABC_H_ #define _ACCTABC_H_ //(* #include <iostream> #include <string> //*) ...
- Java学习日记-10 集合
集合(Collection) 1. 什么是集合? 集合是一组对象组成的一个整体,又称为容器,集合类属于java.util包.集合不同于数组的地方在于,一是它的容量是可变的,二是集合中只能存储对象,不能 ...
- [Java] TreeMap - 源代码学习笔记
TreeMap 实现了 SortedMap 和 NavigableMap 接口,所有本文还会记录 SortedMap 和 NavigableMap 的阅读笔记. SortedMap 1. 排序的比较应 ...
- 【树形动态规划】【CTSC1997】选课 解题报告
CTSC1997-选课 描述 学校实行学分制.每门的必修课都有固定的学分,同时还必须获得相应的选修课程学分.学校开设了N(N<300)门的选修课程,每个学生可选课程的数量M是给定的.学生选修了这 ...