判断是否是网络路径
#region 识别urlStr是否是网络路径
/// <summary>
/// 识别urlStr是否是网络路径
/// </summary>
/// <param name="urlStr"></param>
/// <returns></returns>
public static bool UrlDiscern(string urlStr)
{
if (Regex.IsMatch(urlStr, @"((http|ftp|https)://)(([a-zA-Z0-9\._-]+\.[a-zA-Z]{2,6})|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}))(:[0-9]{1,4})*(/[a-zA-Z0-9\&%_\./-~-]*)?"))
{
return true;
}
else
{
return false;
}
}
#endregion
判断网络路径是否存在
/// <summary>
/// 检查uri链接是否有效
/// </summary>
/// <param name="strUri"></param>
/// <returns></returns>
public static bool CheckUri(string strUri)
{
try
{
System.Net.HttpWebRequest.Create(strUri).GetResponse();
return true;
}
catch
{
return false;
}
}
将获取网络路径图片转化成流数据
/// <summary>
/// 图片以流数据返回
/// </summary>
/// <param name="path">网络路径</param>
/// <returns></returns>
public ActionResult show(string path)
{
bool flag = CheckUri(path);
if (!flag)
{
string file = Server.MapPath("/Content/20180726_0001.jpg");//默认图片
byte[] imgByte = System.IO.File.ReadAllBytes(file);
return File(imgByte, "image/jpg");
}
WebRequest request = WebRequest.Create(path);
WebResponse response = request.GetResponse();
Stream s = response.GetResponseStream();
byte[] data = new byte[1024];
int length = 0;
MemoryStream ms = new MemoryStream();
while ((length = s.Read(data, 0, data.Length)) > 0)
{
ms.Write(data, 0, length);
}
ms.Seek(0, SeekOrigin.Begin);
return File(ms, "image/jpg");
}