需要实现功能:从网上复制一篇文章下来,包括文字,图片(有些图片是在本地),将文章的HTML代码保存到数据库,文章中的图片要下载下来放在本地,同时HTML中的图片链接地址也要换成本地路径。下次打开该文章,图片就是本地的。
问题:功能基本实现了,就是在HTML中用正则找到图片,一个循环将HTML中所有图片路径换成本地路径,在网上复制文章时没有问题,如果图片在本地,保存后会连着2个图片地址指向同一路径。
比如说一篇文章有10张图,图片链接为:001.jpg,002.jpg,003.jpg,004.jpg,005.jpg,006.jpg,007.jpg,008.jpg,009.jpg,010.jpg,保存后HTML中的链接变成001.jpg,002.jpg,002.jpg,003.jpg,003.jpg,004.jpg,004.jpg,005.jpg,005.jpg,006.jpg,调试的时候将断点设在循环内,按F5循环10次后,结果是正确的,如果只循环了5次然后将断点去掉,那么前5张图片链接正确,后5张又开始2张重复。
下面是代码:
//保存
string content = txtContent.Text.Trim();
Model_Info_Resource m_Info_Resource = new Model_Info_Resource();
m_Info_Resource.Info_Type = int.Parse(ddlType.SelectedItem.Value);
m_Info_Resource.Info_Game = int.Parse(ddlGame.SelectedItem.Value);
m_Info_Resource.Info_Title = txtTitle.Text.Trim();
m_Info_Resource.Info_Keyword = txtKeyword.Text.Trim();
m_Info_Resource.UserID = int.Parse(Session["userID"].ToString());
//将内容的图片下载到本地
string regImg = @"<img[^>]*src=(['""]?)[^'""\s>]*\1?[^>]*>";//匹配图片的正则
string regSrc = @"src=(['""]?)[^'""\s]*\1?(\s)?";//匹配SRC的正则
MatchCollection col = Regex.Matches(content, regImg, RegexOptions.IgnoreCase);
string img = "";
string url = "";
string oldUrl = "";
string fileName = "";
WebClient client = new WebClient();
for (int i = 0; i < col.Count; i++)
{
img = col[i].Value;
url = Regex.Match(img, regSrc).Value;
url = url.Substring(url.IndexOf("src=") + 4, url.Length - url.IndexOf("src=") - 4);
url = url.Replace("\"", "").Trim();
if (url == "")
{
continue;
}
oldUrl = url;
int flag = 0;
if (url.Substring(0, 12).ToString() == "UploadImages")//图片在本地
{
flag = 1;
url = Server.MapPath(url);
//if (File.Exists(url))
//{
// continue;
//}
}
fileName = getFileName();//用随机数表示图片名称
content = content.Replace(oldUrl, "UploadImages\\" + fileName);//替换文章中的图片链接
fileName = Server.MapPath("UploadImages\\") + fileName;
if (!File.Exists(fileName))
{
if (flag == 0)//图片在网上下
{
try
{
client.DownloadFile(url, fileName);
}
catch
{ }
}
else//图片在本地
{
try
{
File.Copy(url, fileName);
}
catch { }
}
}
}
client.Dispose();
m_Info_Resource.Info_Content = content;
int info_ID = Convert.ToInt32(ViewState["infoID"]);
int returnValue = 0;
if (info_ID == 0)//新增
{
try
{
returnValue = BLL_Info_Resource.Insert(m_Info_Resource);
}
catch
{
return;
}
}
else
{
m_Info_Resource.Info_ID = info_ID;
try
{
returnValue = BLL_Info_Resource.Update(m_Info_Resource);
}
catch
{
return;
}
}
if (returnValue == -1)
{
labMsg.Visible = true;
return;
}
if (info_ID == 0)
{
txtTitle.Text = "";
txtContent.Text = "";
txtKeyword.Text = "";
BaseHelp.Alert("保存成功", Page);
}
else
{
Response.Write("<script>alert('保存成功');window.location='InfoList.aspx';</script>");
}
问题:功能基本实现了,就是在HTML中用正则找到图片,一个循环将HTML中所有图片路径换成本地路径,在网上复制文章时没有问题,如果图片在本地,保存后会连着2个图片地址指向同一路径。
比如说一篇文章有10张图,图片链接为:001.jpg,002.jpg,003.jpg,004.jpg,005.jpg,006.jpg,007.jpg,008.jpg,009.jpg,010.jpg,保存后HTML中的链接变成001.jpg,002.jpg,002.jpg,003.jpg,003.jpg,004.jpg,004.jpg,005.jpg,005.jpg,006.jpg,调试的时候将断点设在循环内,按F5循环10次后,结果是正确的,如果只循环了5次然后将断点去掉,那么前5张图片链接正确,后5张又开始2张重复。
下面是代码:
//保存
string content = txtContent.Text.Trim();
Model_Info_Resource m_Info_Resource = new Model_Info_Resource();
m_Info_Resource.Info_Type = int.Parse(ddlType.SelectedItem.Value);
m_Info_Resource.Info_Game = int.Parse(ddlGame.SelectedItem.Value);
m_Info_Resource.Info_Title = txtTitle.Text.Trim();
m_Info_Resource.Info_Keyword = txtKeyword.Text.Trim();
m_Info_Resource.UserID = int.Parse(Session["userID"].ToString());
//将内容的图片下载到本地
string regImg = @"<img[^>]*src=(['""]?)[^'""\s>]*\1?[^>]*>";//匹配图片的正则
string regSrc = @"src=(['""]?)[^'""\s]*\1?(\s)?";//匹配SRC的正则
MatchCollection col = Regex.Matches(content, regImg, RegexOptions.IgnoreCase);
string img = "";
string url = "";
string oldUrl = "";
string fileName = "";
WebClient client = new WebClient();
for (int i = 0; i < col.Count; i++)
{
img = col[i].Value;
url = Regex.Match(img, regSrc).Value;
url = url.Substring(url.IndexOf("src=") + 4, url.Length - url.IndexOf("src=") - 4);
url = url.Replace("\"", "").Trim();
if (url == "")
{
continue;
}
oldUrl = url;
int flag = 0;
if (url.Substring(0, 12).ToString() == "UploadImages")//图片在本地
{
flag = 1;
url = Server.MapPath(url);
//if (File.Exists(url))
//{
// continue;
//}
}
fileName = getFileName();//用随机数表示图片名称
content = content.Replace(oldUrl, "UploadImages\\" + fileName);//替换文章中的图片链接
fileName = Server.MapPath("UploadImages\\") + fileName;
if (!File.Exists(fileName))
{
if (flag == 0)//图片在网上下
{
try
{
client.DownloadFile(url, fileName);
}
catch
{ }
}
else//图片在本地
{
try
{
File.Copy(url, fileName);
}
catch { }
}
}
}
client.Dispose();
m_Info_Resource.Info_Content = content;
int info_ID = Convert.ToInt32(ViewState["infoID"]);
int returnValue = 0;
if (info_ID == 0)//新增
{
try
{
returnValue = BLL_Info_Resource.Insert(m_Info_Resource);
}
catch
{
return;
}
}
else
{
m_Info_Resource.Info_ID = info_ID;
try
{
returnValue = BLL_Info_Resource.Update(m_Info_Resource);
}
catch
{
return;
}
}
if (returnValue == -1)
{
labMsg.Visible = true;
return;
}
if (info_ID == 0)
{
txtTitle.Text = "";
txtContent.Text = "";
txtKeyword.Text = "";
BaseHelp.Alert("保存成功", Page);
}
else
{
Response.Write("<script>alert('保存成功');window.location='InfoList.aspx';</script>");
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货