62,243
社区成员




using System.Net;
//下载网络文件
//fileurl文件或图片地址
public bool DownloadPIC(string fileurl)
{
WebClient myWebClient = new WebClient();
NewFileName=System.DateTime.Now.ToFileTime()+".GIF";
try
{
myWebClient.DownloadFile(fileurl,Server.MapPath("~\\Picture\\"+NewFileName));
return true;
}
catch
{
return false;
}
}
int iCount=0;
string strhtml='你html框里面的代码';
ArrayList s=praseimg(strhtml);
if (s.Count>0)
{
for (int i=0;i<s.Count;i++)
{
string picurl=DownloadPIC(Convert.ToString(s[i]));
if (picurl!="")
{
strhtml=strhtml.Replace(Convert.ToString(s[i]),picurl) //替换图片的路径为本地路径;
iCount++;
}
}
}
//正则表达式从HTML分析出所有图片
function ArrayList praseimg(string str)
{
ArrayList s=new ArrayList();
System.Text.RegularExpressions.Regex reg=new System.Text.RegularExpressions.Regex("<img[^>]*?src=['\"]?(<src>.*[^'\"])['\"]?.*?>",System.Text.RegularExpressions.RegexOptions.IgnoreCase);
System.Text.RegularExpressions.MatchCollection m = reg.Matches(str);
for (int i = 0; i < m.Count; i++) s.Add(m[i].Groups["value"].ToString());
}
//下载网络文件
//fileurl文件或图片地址
public string DownloadPIC(string fileurl)
{
WebClient myWebClient = new WebClient();
NewFileName=System.DateTime.Now.ToFileTime()+".GIF";
try
{
myWebClient.DownloadFile(fileurl,Server.MapPath("~\\Picture\\"+NewFileName));
return "Picture\\"+NewFileName;
}
catch
{
return "";
}
}
public string getWebClientImg(string url, string sPath)
{
if (isUrl(url))
{
string FileName = url.Substring(url.LastIndexOf("/") + 1);
string extName = FileName.Substring(FileName.LastIndexOf(".") + 1);
if (extName == "jpg" || extName == "gif")
{
try
{
string rdn = Random();
string StringFileName = "";
DateTime dt = System.DateTime.Now;
StringFileName = (dt.ToString()).Replace(":", "");
StringFileName = StringFileName.Replace("-", "");
StringFileName = StringFileName.Replace(" ", "");
StringFileName = StringFileName + rdn + "." + extName;
WebClient wc = new WebClient();
wc.DownloadFile(url, @sPath + "\\pictem\\" + StringFileName);
return StringFileName;
}
catch (WebException we)
{
//HttpContext.Current.Response.Write(we.Message);
//HttpContext.Current.Response.End();
return "nopicture.jpg";
}
}
else
{
try
{
string rdn = Random();
string StringFileName = "";
DateTime dt = System.DateTime.Now;
StringFileName = (dt.ToString()).Replace(":", "");
StringFileName = StringFileName.Replace("-", "");
StringFileName = StringFileName.Replace(" ", "");
StringFileName = StringFileName + rdn + ".jpg";
WebClient wc = new WebClient();
wc.DownloadFile(url, @sPath + "\\pictem\\" + StringFileName);
return StringFileName;
}
catch (WebException we)
{
//HttpContext.Current.Response.Write(we.Message);
//HttpContext.Current.Response.End();
return "nopicture.jpg";
}
}
}
else
{
if (url.IndexOf("uploadpic") >= 0)
{
return url;
}
else
{
return "nopicture.jpg";
}
}
}