抓取富文本框里的图片URL下载到本地服务器上并把src替换成本地链接 类似发帖这种富文本编辑器 用户可能会从其他网页上复制过来一些内容 会有图片现在要在后台把这些网络图片都下载到本地并把文本里图片的URL替换成本地的有大湿做过没?指点指点 谢了 很急! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 现在要在后台把这些网络图片都下载到本地并把文本里图片的URL替换成本地的--------------------------用正则取出img的SRC,download到本地服务器,然后在把img的src改为你本地的地址。做法很简单 代码自己慢慢写吧。 给你个例子吧#region 根据CI抓取网页内容 private string Snatch(string cid) { string snatchpath = "http://cid-"+cid+snatchurl; try { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(snatchpath); request.AllowAutoRedirect = false; request.Headers.Add(HttpRequestHeader.AcceptLanguage, "zh-cn"); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); StreamReader reader = new StreamReader(response.GetResponseStream(),Encoding.Default); string content = reader.ReadToEnd(); reader.Close(); reader.Dispose(); response.Close(); //SnatchName(content); return content; } catch (Exception ex) { WebLog.AddSystemException(ex); return ""; } } #endregion #region 抓取图片地址、保存图片 private string ImgDownLoad(string urlhtml) { string imgpath = ""; Regex reg = new Regex("<img id=\"ICc0_usertile\" src=\"(?<g1>.*?)\" errsrc", RegexOptions.IgnoreCase); MatchCollection mc = reg.Matches(urlhtml); //设定要查找的字符串 if (mc.Count > 0) { imgpath = mc[0].Groups["g1"].Value; } return imgpath; //string path = System.Web.HttpContext.Current.Server.MapPath("~/images/"); //string myStringWebResource = null; //WebClient myWebClient = new WebClient(); //string filename = imgpath.Substring(imgpath.LastIndexOf("/") + 1); //myStringWebResource = imgpath; //myWebClient.DownloadFile(myStringWebResource,path+filename); //return "images/"+filename; } #endregion 请教图片滚动问题 asp.net gridview如何嵌套datalist asp.net 利用javascript 弹出窗口的问题 怎样使ASP.NET的网页在不同分辨率下显示正常? AspNetPager无法分页问题!!研究N久了。 ASP.NET技术高级QQ群:9997373,欢迎大家加入! 为什么我建立的虚拟目录无法访问? 字符串中添加空格的问题!很怪的问题! 如果是今天新闻就显示new的图标,最后40分了 实现淘宝的商品图片展示效果 Discuz!NT 2.5论坛问题,都进来看看。
--------------------------用正则取出img的SRC,download到本地服务器,然后在把img的src改为你本地的地址。做法很简单 代码自己慢慢写吧。
给你个例子吧
#region 根据CI抓取网页内容
private string Snatch(string cid)
{
string snatchpath = "http://cid-"+cid+snatchurl;
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(snatchpath);
request.AllowAutoRedirect = false;
request.Headers.Add(HttpRequestHeader.AcceptLanguage, "zh-cn");
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream(),Encoding.Default);
string content = reader.ReadToEnd();
reader.Close();
reader.Dispose();
response.Close();
//SnatchName(content);
return content;
}
catch (Exception ex)
{
WebLog.AddSystemException(ex);
return "";
}
}
#endregion #region 抓取图片地址、保存图片
private string ImgDownLoad(string urlhtml)
{
string imgpath = "";
Regex reg = new Regex("<img id=\"ICc0_usertile\" src=\"(?<g1>.*?)\" errsrc", RegexOptions.IgnoreCase);
MatchCollection mc = reg.Matches(urlhtml); //设定要查找的字符串
if (mc.Count > 0)
{
imgpath = mc[0].Groups["g1"].Value;
}
return imgpath;
//string path = System.Web.HttpContext.Current.Server.MapPath("~/images/");
//string myStringWebResource = null;
//WebClient myWebClient = new WebClient();
//string filename = imgpath.Substring(imgpath.LastIndexOf("/") + 1);
//myStringWebResource = imgpath;
//myWebClient.DownloadFile(myStringWebResource,path+filename); //return "images/"+filename;
}
#endregion