using System.IO;
using System.Net;
using System.Text.RegularExpressions;
protected void Button2_Click(object sender, EventArgs e)
{
//自动保存远程图片
WebClient client = new WebClient();
//备用Reg:<img.*?src=([\"\'])(http:\/\/.+\.(jpg|gif|bmp|bnp))\1.*?>
Regex reg = new Regex("IMG[^>]*?src\\s*=\\s*(?:\"(?<1>[^\"]*)\"|'(?<1>[^\']*)')", RegexOptions.IgnoreCase);
MatchCollection m = reg.Matches(TextBox1.Text);
foreach (Match math in m)
{
string imgUrl = math.Groups[1].Value;
//在原图片名称前加YYMMDD重名名并上传
Regex regName = new Regex(@"\w+.(?:jpg|gif|bmp|png)", RegexOptions.IgnoreCase);
string strNewImgName = DateTime.Now.ToShortDateString().Replace("-", "") + regName.Match(imgUrl).ToString();
try
{
if (!Directory.Exists(Server.MapPath("Auto/")))
Directory.CreateDirectory(Server.MapPath("Auto/"));
//保存图片
client.DownloadFile(imgUrl, Server.MapPath("Auto/") + strNewImgName);
InsertImage(Server.MapPath("Auto/") + strNewImgName);
}
catch(Exception ex){}
finally{}
client.Dispose();
}
Response.Write("<script>alert('远程图片保存成功,保存路径为ImgUpload/auto')</script>");
}
转载于:https://blog.51cto.com/xiaoyaosr/394708