DownLoadFile - FileHandler
public class FileHandler
{
public static bool DownLoadFile(string path, string fileName)
{
bool result = false;
if (!string.IsNullOrEmpty(path))
{
if (path.StartsWith("http") || path.StartsWith("https"))
{
result = InternetDownload(path, fileName);
}
else
{
result = LocalDownload(path, fileName);
}
}
return result;
} private static bool LocalDownload(string path, string fileName)
{
bool result = false;
//Physical path "D:\InvoicePDF\"
string filePath = path + fileName;
if (File.Exists(filePath))
{
result = true;
}
else
{
try
{
//Relative path "~/InvoicePDF/"
filePath = HttpContext.Current.Server.MapPath(filePath);
if (File.Exists(filePath))
{
result = true;
}
}
catch
{
result = false;
}
}
if (result)
{
try
{
//string filePath = HttpContext.Current.Server.MapPath(path + fileName);
FileInfo fileInfo = new FileInfo(filePath);
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.ClearHeaders();
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName);
HttpContext.Current.Response.AddHeader("Content-Length", fileInfo.Length.ToString());
HttpContext.Current.Response.AddHeader("Content-Transfer-Encoding", "binary");
HttpContext.Current.Response.ContentType = "application/octet-stream";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");
HttpContext.Current.Response.WriteFile(fileInfo.FullName);
HttpContext.Current.Response.Flush();
}
catch (Exception e)
{
}
finally
{
HttpContext.Current.Response.End();
}
}
return result;
} private static bool InternetDownload(string path, string fileName)
{
string url = path + fileName;
if (UrlIsExist(url))
{
ResponseRedirect.Redirect(HttpContext.Current.Response, url, "_blank", "");
//System.Web.HttpContext.Current.Response.Redirect(url,false);
}
return false;//Never use default behavior
} private static bool UrlIsExist(string url)
{
System.Uri u = null;
try
{
u = new Uri(url);
}
catch { return false; }
bool isExist = false;
System.Net.HttpWebRequest r = System.Net.HttpWebRequest.Create(u) as System.Net.HttpWebRequest;
r.Method = "HEAD";
try
{
System.Net.HttpWebResponse s = r.GetResponse() as System.Net.HttpWebResponse;
if (s.StatusCode == System.Net.HttpStatusCode.OK)
{
isExist = true;
}
}
catch (System.Net.WebException x)
{
try
{
isExist = ((x.Response as System.Net.HttpWebResponse).StatusCode != System.Net.HttpStatusCode.NotFound);
}
catch { isExist = (x.Status == System.Net.WebExceptionStatus.Success); }
}
return isExist;
}
}
DownLoadFile - FileHandler的更多相关文章
- WebClient.DownloadFile(线程机制,异步下载文件)
线程机制(避免卡屏),异步下载文件. 我做网站的监控,WebClient.DownloadFile这个方法是我经常用到的,必要的时候肯定是要从网上下载些什么(WebRequest 也可以下载网络文件, ...
- Tomcat报java.lang.ClassNotFoundException: 1catalina.org.apache.juli.FileHandler
最近在生产环境部署Tomcat的时候,在启动的时候,在控制台报"java.lang.ClassNotFoundException: 1catalina.org.apache.juli.Fil ...
- 多种下载文件方式 Response.BinaryWrite(byte[] DocContent);Response.WriteFile(System.IO.FileInfo DownloadFile .FullName);Response.Write(string html2Excel);
通过html给xls赋值,并下载xls文件 一.this.Response.Write(sw.ToString());System.IO.StringWriter sw = new System.IO ...
- 警惕使用WebClient.DownloadFile(string uri,string filePath)方法
原文:警惕使用WebClient.DownloadFile(string uri,string filePath)方法 WebClient.DownloadFile(string uri,string ...
- 下载文件downloadFile
public static void downLoadFile(InputStream inStream, String fileName) { if (StringUtils.isBlank(fil ...
- wx.downloadFile问题
http://www.wxapp-union.com/forum.php?mod=viewthread&tid=2988(copy) 这个问题,研究者甚少,以至于相关问题直到今天,仍然属于未知 ...
- IIS下载,WebClient().DownloadFile下载
new System.Net.WebClient().DownloadFile(serverPath, localPath); 有时候使用的时候,文件下载不下来.需要设置一下服务器上IIS的权限
- TFS二次开发05——下载文件(DownloadFile)
前面介绍了怎样读取TFS上目录和文件的信息,怎么建立服务器和本地的映射(Mapping). 本节介绍怎样把TFS服务器上的文件下载到本地. 下载文件可以有两种方式: using Microsoft.T ...
- Tomcat报错java.lang.ClassNotFoundException: 2localhost.org.apache.juli.FileHandler
Can't load log handler "1catalina.org.apache.juli.FileHandler" java.lang.ClassNotFoundExce ...
随机推荐
- iwpriv
AuthMode {OPEN,SHARED,WEPAUTO,WPAPSK,WPA2PSK,WPANONE} ::Set Authentication Mode EncrypTy ...
- [svn]svn merge
转:http://blog.csdn.net/keda8997110/article/details/21813035 Step by Step 完成merge 目录: Branch的必要性 1.本地 ...
- TMDS协议
1 概述 1.1 连接结构 图1 TMDS连接结构 数据流中包含了像素和控制数据,发送器在任何给定的输入时钟周期,到底是编码像素数据还是控制数据取决于数据使能信号DE,DE有效时,指示像素数 据 ...
- Spark1.4启动spark-shell时initializing失败
错误信息如下: 5/11/03 16:48:15 INFO spark.SparkContext: Running Spark version 1.4.1 15/11/03 16:48:15 WARN ...
- DBA_Oracle Erp升级时如何确定具体的Patch ID(案例)
2014-07-03 Created By BaoXinjian
- PLSQL_Oracle临时表Temporary Table基本概念和用法(概念)
2014-06-08 Created By BaoXinjian
- DirFile
using System; using System.Text; using System.IO; namespace MyListen { /// <summary> /// 文件操作夹 ...
- java类中serialversionuid 作用 是什么?举个例子说明
serialVersionUID适用于Java的序列化机制.简单来说,Java的序列化机制是通过判断类的serialVersionUID来验证版本一致性的.在进行反序列化时,JVM会把传来的字节流中的 ...
- Struts2返回JSON对象的方法总结
如果是作为客户端的HTTP+JSON接口工程,没有JSP等view视图的情况下,使用Jersery框架开发绝对是第一选择.而在基于Spring3 MVC的架构下,对HTTP+JSON的返回类型也有很好 ...
- Python进阶06 循环对象
作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 这一讲的主要目的是为了大家在读Python程序的时候对循环对象有一个基本概念. 循 ...