以下方法用在web请求方法中:
void ReplaceContentImagerBase65Src()
{
Regex regImg = new Regex(@"<img src=(.*?)>", RegexOptions.IgnoreCase);
string base64SourceValue = string.Empty, base64Value = string.Empty, fileName = string.Empty;
MatchCollection matches = regImg.Matches(Content);
int i = 0;
string[] imgList = new string[matches.Count];
foreach (Match match in matches)
{
imgList[i++] = match.Value;
base64SourceValue = match.Groups[1].Value.Substring(1, match.Groups[1].Value.Length - 2);
if (!base64SourceValue.StartsWith("data:"))
{
continue;
};
fileName = DateTime.Now.ToString("yyMMddHHmmssfff") + ".jpg";
imageSavePath = path + fileName;
base64Value = base64SourceValue.Substring(base64SourceValue.IndexOf(",") + 1).Replace("\"", "");
Base64ToImage(base64Value, imageSavePath);
Content = Content.Replace(base64SourceValue, @"http://blog.abc.com/img/" + fileName);
}
}