using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Common
{
public class FileHelper : Page
{
#region 获取滚动图片/广告图片列表
/// <summary>
/// 获取滚动图片/广告图片列表
/// </summary>
/// <returns></returns>
public static Dictionary<string, string> GetRollImageList(string Path)
{
Dictionary<string, string> imglist = new Dictionary<string, string>();
XmlDocument xml = new XmlDocument();
xml.Load(Path);
XmlNode root = xml.SelectSingleNode("image");
XmlNodeList childlist = root.ChildNodes;
foreach (XmlNode node in childlist)
{
imglist.Add(node.Attributes["path"].Value, node.ChildNodes[0].InnerText);
}
return imglist;
}
#endregion
#region 滚动图片xml增加一个节点
/// <summary>
/// 滚动图片xml增加一个节点
/// </summary>
/// <param name="xmlpath"></param>
/// <param name="imgpath"></param>
/// <param name="href"></param>
public static void AddNewXmlNode(string xmlpath, string imgname, string href, string imgpath)
{
XmlDocument xml = new XmlDocument();
xml.Load(xmlpath);
XmlNode root = xml.SelectSingleNode("image");
XmlElement xe = xml.CreateElement("imageinfo");
xe.SetAttribute("path", imgpath + imgname);
XmlElement xe1 = xml.CreateElement("href");
xe1.InnerText = href;
xe.AppendChild(xe1);
root.AppendChild(xe);
xml.Save(xmlpath);
}
#endregion
#region 滚动图片xml删除一个节点
/// <summary>
/// 滚动图片xml删除一个节点
/// </summary>
/// <param name="xmlpath"></param>
/// <param name="imgpath"></param>
public static void DelXmlNode(string xmlpath, string imgname, string imgpath)
{
XmlDocument xml = new XmlDocument();
xml.Load(xmlpath);
XmlNode root = xml.SelectSingleNode("image");
XmlNodeList xnl = xml.SelectSingleNode("image").ChildNodes;
foreach (XmlNode xn in xnl)
{
XmlElement xe = (XmlElement)xn;
if (xe.GetAttribute("path") == imgpath + imgname)
{
root.RemoveChild(xe);
}
}
xml.Save(xmlpath);
}
#endregion
#region 获取滚动图片列表
/// <summary>
/// 获取滚动图片列表
/// </summary>
/// <returns></returns>
public string GetRollImage()
{
StringBuilder sb = new StringBuilder();
Dictionary<string, string> list = FileHelper.GetRollImageList(Server.MapPath("~/sysconfig/RollImage.xml"));
foreach (string key in list.Keys)
{
sb.Append("<a href=/"" + list[key] + "/"><img src=/"" + key.Replace("~/", "../") + "/" /></a>");
}
sb.Append("");
return sb.ToString();
}
#endregion
#region 删除图片
/// <summary>
/// 删除图片
/// </summary>
/// <param name="path"></param>
public void DeleteImg(string path)
{
FileInfo file = new FileInfo(Server.MapPath(path));
if (File.Exists(Server.MapPath(path)))
{
File.Delete(Server.MapPath(path));
}
}
#endregion
/// <summary>
/// 上传文件
/// </summary>
/// <returns></returns>
public string UpFile(FileUpload up, string oldFile,string filename)
{
string result = "";
if (up.HasFile)
{
if (GetExtension(up.FileName)!= "exe")
{
string fileName = up.FileName;
string JueDuiUrl = Server.MapPath("~/" + filename + "");
string xiangduiurl = "~/" + filename + "" + "/" + fileName;
if (!Directory.Exists(JueDuiUrl))
{
Directory.CreateDirectory(JueDuiUrl);//如果不存在,则创建
}
string webFilePath = JueDuiUrl + "//" + fileName; // 服务器端文件路径
if (!File.Exists(webFilePath))
{
if (!string.IsNullOrEmpty(oldFile))
{
new Common.FileHelper().DeleteImg(oldFile);
}
up.SaveAs(webFilePath);
result = xiangduiurl;
}
else
{
return "1";
}
}
else
{
return "2";
}
}
return result;
}
/// <summary>
/// 获取文件扩展名
/// </summary>
/// <param name="fileName"></param>
/// <returns></returns>
public static string GetExtension(string fileName)
{
int i = fileName.LastIndexOf(".") + 1;
string Name = fileName.Substring(i);
return Name;
}
}
}