/// <summary>
/// 判断本地、远程图片,取图片
/// </summary>
/// <param name="RecPic">要判断的图片路径</param>
/// <returns></returns>
public static string GetDefualCourseImgtUrl(string RecPic)
{
string resultPath = string.Empty;
string m_ImgPath = UploadPathHelper.GetUploadPath();
if (string.IsNullOrEmpty(RecPic) || (!IsExistRemoteImage(RecPic) && !File.Exists(PathHelper.ServerMapPath("~/"+m_ImgPath + RecPic))))
{
//获取默认图片的方法
resultPath = GetDefaultImageUrl();
}
else
{
if(IsExistRemoteImage(RecPic))
{
resultPath = RecPic;
}
else if(File.Exists(PathHelper.ServerMapPath("~/"+m_ImgPath + RecPic)))//<span style="font-family: Arial, Helvetica, sans-serif;">PathHelper.ServerMapPath自己封装获取绝对路径的方法</span>
{
resultPath = m_ImgPath + RecPic;
}
else
{
//获取默认图片的方法
resultPath = GetDefaultImageUrl();
}
}
return resultPath;
}
/// <summary>
/// 远程判断图片是否存在
/// </summary>
/// <param name="curl"></param>
/// <returns></returns>
private static bool IsExistRemoteImage(string curl)
{
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri(curl));
ServicePointManager.Expect100Continue = false;
((HttpWebResponse)request.GetResponse()).Close();</span>
return true;
}
catch
{
return false;
}
}
Asp.net判断图片存在的并获取图片
最新推荐文章于 2021-12-22 15:14:56 发布
本文介绍了一个用于检查图片路径有效性的方法,包括判断本地和远程图片是否存在的逻辑,并提供了获取默认图片路径的功能。通过一系列条件判断确保能返回有效的图片路径。
471

被折叠的 条评论
为什么被折叠?



