判断URL文件是不是在于在。

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的更多相关文章

  1. WebClient.DownloadFile(线程机制,异步下载文件)

    线程机制(避免卡屏),异步下载文件. 我做网站的监控,WebClient.DownloadFile这个方法是我经常用到的,必要的时候肯定是要从网上下载些什么(WebRequest 也可以下载网络文件, ...

  2. Tomcat报java.lang.ClassNotFoundException: 1catalina.org.apache.juli.FileHandler

    最近在生产环境部署Tomcat的时候,在启动的时候,在控制台报"java.lang.ClassNotFoundException: 1catalina.org.apache.juli.Fil ...

  3. 多种下载文件方式 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 ...

  4. 警惕使用WebClient.DownloadFile(string uri,string filePath)方法

    原文:警惕使用WebClient.DownloadFile(string uri,string filePath)方法 WebClient.DownloadFile(string uri,string ...

  5. 下载文件downloadFile

    public static void downLoadFile(InputStream inStream, String fileName) { if (StringUtils.isBlank(fil ...

  6. wx.downloadFile问题

    http://www.wxapp-union.com/forum.php?mod=viewthread&tid=2988(copy) 这个问题,研究者甚少,以至于相关问题直到今天,仍然属于未知 ...

  7. IIS下载,WebClient().DownloadFile下载

    new System.Net.WebClient().DownloadFile(serverPath, localPath); 有时候使用的时候,文件下载不下来.需要设置一下服务器上IIS的权限

  8. TFS二次开发05——下载文件(DownloadFile)

    前面介绍了怎样读取TFS上目录和文件的信息,怎么建立服务器和本地的映射(Mapping). 本节介绍怎样把TFS服务器上的文件下载到本地. 下载文件可以有两种方式: using Microsoft.T ...

  9. Tomcat报错java.lang.ClassNotFoundException: 2localhost.org.apache.juli.FileHandler

    Can't load log handler "1catalina.org.apache.juli.FileHandler" java.lang.ClassNotFoundExce ...

随机推荐

  1. 在Ubuntu下编译wpa_supplicant

    最近在研究WiFi联盟的多屏互动协议Miracast,其中需要用到wpa_supplicant,编译中遇到了一些问题,经过一下午的折腾也都解决了,下面分享给各位. 一.编译需要用到三个库源码包,分别是 ...

  2. ADF_Tutorials系列17_ADF Faces_ADF预定义组件的创建和使用

    2013-05-01 Created By BaoXinjian

  3. RAC_Oracle集群服务安装RAC(案例)

    2015-01-23 Created By BaoXinjian Thanks and Regards

  4. Workflow_如何处理标准异常和自定义异常(案例)

    2014-05-31 Created By BaoXinjian

  5. Codeforces Round #359 (Div. 2)C - Robbers' watch

    C. Robbers' watch time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  6. HDU 3555 Bomb 数位DP 入门

    给出n,问所有[0,n]区间内的数中,不含有49的数的个数 数位dp,记忆化搜索 dfs(int pos,bool pre,bool flag,bool e) pos:当前要枚举的位置 pre:当前要 ...

  7. webuploader在同一个页面支持多个按钮实例

    之前在时候用到webuploader ,起初是支持单实例,后来要求支持多实例. webuploder API网址,如果不懂我说的可以去查看http://fex.baidu.com/webuploade ...

  8. ubuntu更换源后报错:W: GPG error: (转载)

    From:http://www.njava.com/njava-626.html 更换163源后,更新源时出现错误. # apt-get update W: GPG error: http://ext ...

  9. linux free命令(转载)

    来源:http://www.cnblogs.com/peida/archive/2012/12/25/2831814.html free命令可以显示Linux系统中空闲的.已用的物理内存及swap内存 ...

  10. setcookie各个参数详解

    定义和用法 setcookie() 函数向客户端发送一个 HTTP cookie. cookie 是由服务器发送到浏览器的变量.cookie 通常是服务器嵌入到用户计算机中的小文本文件.每当计算机通过 ...