#region 复制粘贴的图片下载到服务器,修改时删除原有的重新添加
public void pic(string guid, bool isModfy)
{
string key = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(guid, "MD5");
//虚拟路径
string imageVarPath = System.Configuration.ConfigurationManager.AppSettings["NoticeAnnouncePath"];
txtContent = Request["FckEditorContent"];
//对图片的保存
ArrayList tempArray = GetHtmlImageListArray(txtContent);
string strArticle_FirstImage = string.Empty;//接受图片的变量
List<string> filenameList = new List<string>();
for (int i = 0; i < tempArray.Count; i++)
{
//CookBook.Model.IamgeModel img = (IamgeModel)tempArray[i];
string strPath = tempArray[i].ToString();
WebClient web = new WebClient();
string app = strPath;
string oldBasc = strPath;
if (app.Contains("http://") == true)
{
#region
string newImageName = saveimage(strPath, key, i);
//string strpath = "/Content/Notificate/"; //CookBook.Component.Public.GetConfigPar("CookBook_Picture");
// string filename = strpath + newImageName;
string sv = Server.MapPath(imageVarPath); //据对路径
string strNewPath = sv + newImageName; //绝对路径+文件名
string strNewUrl = imageVarPath + newImageName; //相对路径+文件名
filenameList.Add(newImageName); //序号+key+time+扩展名
try
{
web.DownloadFile(app, strNewPath);//将图片保存到本地
}
catch (Exception e)
{
Response.Write("<script>alert('" + e.Message + "')</script>");
}
txtContent = txtContent.Replace(strPath, strNewUrl);//将文本中的图片路径替换成上传后的路径
#endregion
}
else
{
string pathone = Server.MapPath(strPath);
string FileType = strPath.Substring(strPath.LastIndexOf(".") + 1, (strPath.Length - strPath.LastIndexOf(".") - 1));
//string newpath="/Content/Notificate/" + i + DateTime.Now.ToString("yyyyMMddHHmmss") + "pic" + "." + FileType;
string fn = key + i + DateTime.Now.ToString("yyyyMMddHHmmss") + "pic" + "." + FileType;
string newpath = imageVarPath + fn;
string path = Server.MapPath(newpath);
web.UploadFile(path, pathone);
txtContent = txtContent.Replace(pathone, path);
txtContent = txtContent.Replace(strPath, newpath);
filenameList.Add(fn);
}
}
if (isModfy)
{
//从路径下读取所有文件列表 key
DirectoryInfo di = new System.IO.DirectoryInfo(Server.MapPath(imageVarPath));
FileInfo[] fimore = di.GetFiles();
List<FileInfo> fifoList = new List<FileInfo>();
foreach (FileInfo fi in fimore)
{
//将满足条件的放入另一个集合 ,此通知对应的图片
if (key == fi.Name.Substring(0, 32))
{
fifoList.Add(fi);
}
}
//遍历读取出来的列表
foreach (FileInfo hasfile in fifoList)
{
//如果读取出来的文件不在添加项中,则说明已被删除,删除服务器文件
if (!filenameList.Contains(hasfile.Name))
{
hasfile.Delete();
}
}
}
}
/// <summary>
/// 返回图片标签列表字符串的ImageListArray
/// 将HTML字符串根据传入的strUrl页地址得到该strUrl页的图片标签列表字符串的ImageListArray
/// </summary>
/// <param name="strUrl">URL页面地址</param>
/// <param name="strHtml">传入的strHtml字符串</param>
/// <returns>返回图片标签的数组</returns>
//public ArrayList GetPageImageListArray(string strHtml)
//{
// return this.GetHtmlImageListArray(strHtml);
//}
/// <summary>
/// 返回图片标签列表字符串的ImageListArray
/// 将HTML字符串根据传入的strUrl页地址得到该strUrl页的图片标签列表字符串的ImageListArray
/// </summary>
/// <param name="strUrl">URL页面地址</param>
/// <param name="strHtml">传入的strHtml字符串</param>
/// <returns>返回图片标签的数组</returns>
public ArrayList GetHtmlImageListArray(string strHtml)
{
//string strHigherUrl = this.ConvertUrlHigher(strUrl);
//string strRootUrl = this.ConvertUrlRoot(strUrl);
ArrayList arrayListImageTag = new ArrayList();
//if (strUrl == "")
//{
// strHtml = null;
// return arrayListImageTag;
//}
Regex r = new Regex(@"(<IMG)[^>]*.", RegexOptions.IgnoreCase | RegexOptions.Compiled);
MatchCollection mc = r.Matches(strHtml);
for (int i = 0; i < mc.Count; i++)
{
if (mc[i].Value.ToLower().StartsWith("<img") == true)
{
string strTagRightFill = strHtml.Substring(mc[i].Index);
string strTag = mc[i].Value;
int intMore = strTagRightFill.IndexOf(">");
while (intMore > -1)
{
intMore = strTagRightFill.IndexOf(">", intMore);
int intMoreNext = strTagRightFill.IndexOf(">", intMore + 1);
if (intMore != -1)
{
if (intMoreNext != -1)
{
string strEstimateLess = strTagRightFill.Substring(intMore, intMoreNext - intMore);
if (strEstimateLess.IndexOf("<") != -1)
{
strTag = strTagRightFill.Substring(0, intMore + 1);
break;
}
else
{
intMore++;
strTag = strTagRightFill.Substring(0, intMoreNext + 1);
}
}
else
{
intMore++;
}
}
}
// model.Infomation_Tags = strTag;
// model.Infomation_FirstPic = this.GetImageSrc(strTag);
//imageModel.ImageType = this.GetImageType(imageModel.ImageSrc);
// model.Infomation_FirstPic = model.Infomation_FirstPic.Substring(model.Infomation_FirstPic.LastIndexOf("/") + 1);
//if (imageModel.ImageSrc.ToLower().IndexOf("http") < 0)//不是完整地址的
//{
// if (imageModel.ImageSrc.Substring(0, 1) == "/")
// {
// imageModel.ImageSrc = strRootUrl + imageModel.ImageSrc;
// }
// else
// {
// imageModel.ImageSrc = strHigherUrl + imageModel.ImageSrc;
// }
//}