保存远程图片到本地的问题

duangjian 2008-07-28 11:27:20
我想做到,复制网上的文章。就直接把图片抓取保存到本地来。。不知道在asp.net怎么实现?

对了。有没有这个功能的编辑器推介一下。。谢谢
...全文
129 15 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
shadowjl 2008-07-28
  • 打赏
  • 举报
回复
学习
glboy12 2008-07-28
  • 打赏
  • 举报
回复
FckEditor
suyiming 2008-07-28
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 wangkun9999 的回复:]
首先用正则从文章中匹配出所有图片地址;
然后用WebClient类下载到本地就可以了(下载图片这一步在文章入库的时候实现就可以了)

C# code
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("~\\Pic…
[/Quote]
wangkun9999 2008-07-28
  • 打赏
  • 举报
回复
首先用正则从文章中匹配出所有图片地址;
然后用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;
}
}
duangjian 2008-07-28
  • 打赏
  • 举报
回复
谢谢楼上的。不过好像正规表达式有点问题。。。
wangkun9999 2008-07-28
  • 打赏
  • 举报
回复
解析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 "";
}
}


duangjian 2008-07-28
  • 打赏
  • 举报
回复
楼上的是病毒,恶心!
hznetnewpower 2008-07-28
  • 打赏
  • 举报
回复
简单的 看这里 软件
duangjian 2008-07-28
  • 打赏
  • 举报
回复
如何用正则表达式从HTML分析出我想要的图片呢?
chocolee 2008-07-28
  • 打赏
  • 举报
回复
    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";
}
}
}
mengxj85 2008-07-28
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 wangkun9999 的回复:]
首先用正则从文章中匹配出所有图片地址;
然后用WebClient类下载到本地就可以了(下载图片这一步在文章入库的时候实现就可以了)

C# code
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("~\\Pic…
[/Quote]

学习
jl_lsj 2008-07-28
  • 打赏
  • 举报
回复
up
周公 2008-07-28
  • 打赏
  • 举报
回复
首先需要获取网页的html源代码,把内容保存起来,然后分析文章内容,把有关图片转换成绝对URL地址,然后利用网络流的方式下载这些图片,保存到本地机器。
duangjian 2008-07-28
  • 打赏
  • 举报
回复
duangjian 2008-07-28
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 glboy12 的回复:]
FckEditor
[/Quote]

fck可以么??我用的就是,好像没这个功能吧??

62,243

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧