保存远程图片到本地的问题 我想做到,复制网上的文章。就直接把图片抓取保存到本地来。。不知道在asp.net怎么实现?对了。有没有这个功能的编辑器推介一下。。谢谢 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 首先用正则从文章中匹配出所有图片地址;然后用WebClient类下载到本地就可以了(下载图片这一步在文章入库的时候实现就可以了)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;}} fck可以么??我用的就是,好像没这个功能吧?? 首先需要获取网页的html源代码,把内容保存起来,然后分析文章内容,把有关图片转换成绝对URL地址,然后利用网络流的方式下载这些图片,保存到本地机器。 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"; } } } 如何用正则表达式从HTML分析出我想要的图片呢? 解析html代码中的图片的正则csdn上面很多,具本要看你从代码中加入图的格式,我随手写了个,没测试: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 "";}} 请问有关数据库的知识 asp.net ajax 读取另一页面数据 导致本页面无法操作 获取当前时间,并插入数据库出现问题! 急!!!救命了~~一个很麻烦的问题~~20060726 一个CleckBox的问题 微软官方.net论坛安装后不能用。 我想用DropDownList下拉列表来显示图片的名字然后用户在选择图片名字时图片会随着鼠标的移动而改变各位大哥有没有什么办法 如何自动登陆下拉地址栏 菜鸟提问:如何利用js或者ajax技术实现鼠标事件? 谁有水晶报表9 For .Net的安装序列号 关于Web下水晶报表的疑问 web form 点击“设计”时,VS没有反映
然后用WebClient类下载到本地就可以了(下载图片这一步在文章入库的时候实现就可以了)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;
}
}
fck可以么??我用的就是,好像没这个功能吧??
{
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";
}
}
}
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 "";
}
}