UpLoad.cs
上传用户:lanchensha
上传日期:2022-02-27
资源大小:7530k
文件大小:39k
源码类别:

编辑器/阅读器

开发平台:

C#

  1. //------------------------------------------------------------------------------
  2. //                                  版权声明
  3. //DotNetTextBox免费版源码版权由小宝.NET及Aspxcn中华网工作室所有!
  4. //非盈利性个人网站可免费使用本控件,商业及盈利性网站请购买功能更强大的商业版本授
  5. //权或开发版,如发现任何个人或机构违反本声明,本站将对其追究法律责任!
  6. //商业授权及开发版购买地址:http://www.aspxcn.com.cn/dotnettextbox/default.htm
  7. //联系email:webmaster@aspxcn.com.cn
  8. //------------------------------------------------------------------------------
  9. using System;
  10. using System.Collections;
  11. using System.IO;
  12. using System.Net;
  13. using System.Text;
  14. using System.Web;
  15. using System.Web.UI;
  16. using System.Web.UI.WebControls;
  17. using System.Xml;
  18. namespace DotNetTextBox
  19. {
  20.     #region 自定义控件的上传功能
  21.     /// <summary>
  22.     /// 文件列表的排序处理
  23.     /// </summary>
  24.     public class SortFile : IComparer
  25.     {
  26.         int IComparer.Compare(object a, object b)
  27.         {
  28.             FileInfo fa = (FileInfo)a;
  29.             FileInfo fb = (FileInfo)b;
  30.             if (fa.LastWriteTime < fb.LastWriteTime)
  31.                 return 1;
  32.             if (fa.LastWriteTime > fb.LastWriteTime)
  33.                 return -1;
  34.             return 0;
  35.         }
  36.     }
  37.     /// <summary>
  38.     /// 上传功能页面的类
  39.     /// </summary>
  40.     public partial class UpLoad : System.Web.UI.Page
  41.     {
  42.         /// <summary>
  43.         /// 页面控件的定义
  44.         /// </summary>
  45.         protected string vrPath;
  46.         protected System.Web.UI.WebControls.HiddenField config_type;
  47.         protected System.Web.UI.WebControls.FileUpload FileUpload1;
  48.         protected System.Web.UI.WebControls.Button uploadBtn;
  49.         protected System.Web.UI.WebControls.Button selectAllBtn;
  50.         protected System.Web.UI.WebControls.Button deleteBtn;
  51.         protected System.Web.UI.WebControls.Button editBtn;
  52.         protected System.Web.UI.WebControls.Button canceloading;
  53.         protected System.Web.UI.WebControls.Button remoteupload;
  54.         protected System.Web.UI.WebControls.Button insertTemplate;
  55.         protected System.Web.UI.WebControls.Label space;
  56.         protected System.Web.UI.WebControls.Label useSpace;
  57.         protected System.Web.UI.WebControls.Label path;
  58.         protected System.Web.UI.WebControls.TextBox file_path;
  59.         protected System.Web.UI.WebControls.TextBox remoteurl;
  60.         protected System.Web.UI.WebControls.TextBox inputx;
  61.         protected System.Web.UI.WebControls.TextBox inputy;
  62.         protected System.Web.UI.WebControls.GridView File_List;
  63.         protected System.Web.UI.WebControls.HiddenField config_maxAllUploadSize;
  64.         protected System.Web.UI.WebControls.HiddenField config_smallImages;
  65.         protected System.Web.UI.WebControls.HiddenField config_watermarkImages_path;
  66.         protected System.Web.UI.WebControls.HiddenField config_watermarkImages;
  67.         protected System.Web.UI.WebControls.HiddenField config_watermarkText;
  68.         protected System.Web.UI.WebControls.HiddenField config_watermakOption;
  69.         protected System.Web.UI.WebControls.HiddenField config_watermark;
  70.         protected System.Web.UI.WebControls.HiddenField config_autoname;
  71.         protected System.Web.UI.WebControls.HiddenField config_allowUpload;
  72.         protected System.Web.UI.WebControls.HiddenField config_fileFilters;
  73.         protected System.Web.UI.WebControls.HiddenField config_maxSingleUploadSize;
  74.         protected System.Web.UI.WebControls.HiddenField config_fileListBox;
  75.         protected System.Web.UI.WebControls.HiddenField config_watermarkImagesName;
  76.         protected System.Web.UI.WebControls.HiddenField config_watermarkName;
  77.         protected System.Web.UI.WebControls.HiddenField config_smallImagesName;
  78.         protected System.Web.UI.WebControls.HiddenField config_smallImagesType;
  79.         protected System.Web.UI.WebControls.HiddenField config_smallImagesW;
  80.         protected System.Web.UI.WebControls.HiddenField config_smallImagesH;
  81.         protected System.Web.UI.WebControls.HiddenField templateContent;
  82.         protected System.Web.UI.WebControls.Label maxSingleUploadSize;
  83.         protected System.Web.UI.WebControls.RadioButtonList RadioButtonList1;
  84.         protected XmlDocument config = new XmlDocument();
  85.         /// <summary>
  86.         /// 页面的初始化
  87.         /// </summary>
  88.         protected void Page_Load(object sender, EventArgs e)
  89.         {
  90.             if (!IsPostBack)
  91.             {
  92.                 Response.Expires = -1;
  93.                 if (Request.Cookies["uploadConfig"] != null)
  94.                 {
  95.                     config.Load(Server.UrlDecode(Request.Cookies["uploadConfig"].Value.ToLower()));
  96.                 }
  97.                 else
  98.                 {
  99.                     config.Load(HttpContext.Current.Request.PhysicalApplicationPath + "/system_dntb/uploadconfig/default.config");
  100.                 }
  101.                 if (Request.Cookies["languages"] != null)
  102.                 {
  103.                     ResourceManager.SiteLanguageKey = Request.Cookies["languages"].Value;
  104.                 }
  105.                 else
  106.                 {
  107.                     ResourceManager.SiteLanguageKey = HttpContext.Current.Request.ServerVariables["HTTP_ACCEPT_LANGUAGE"].ToLower().Split(',')[0];
  108.                 }
  109.                 if (config_type.Value == "Images")
  110.                 {
  111.                     config_fileFilters.Value = config.SelectNodes("//configuration/imagesFilters")[0].InnerText;
  112.                     config_watermarkName.Value = config.SelectNodes("//configuration/watermarkName")[0].InnerText;
  113.                     config_watermarkImagesName.Value = config.SelectNodes("//configuration/watermarkImagesName")[0].InnerText;
  114.                     config_smallImagesName.Value = config.SelectNodes("//configuration/smallImagesName")[0].InnerText;
  115.                     config_smallImagesType.Value = config.SelectNodes("//configuration/smallImagesType")[0].InnerText;
  116.                     config_smallImagesW.Value = config.SelectNodes("//configuration/smallImagesW")[0].InnerText;
  117.                     config_smallImagesH.Value = config.SelectNodes("//configuration/smallImagesH")[0].InnerText;
  118.                     config_smallImages.Value = config.SelectNodes("//configuration/smallImages")[0].InnerText.Trim().ToLower();
  119.                     config_watermark.Value = config.SelectNodes("//configuration/watermark")[0].InnerText.Trim().ToLower();
  120.                     config_watermarkImages.Value = config.SelectNodes("//configuration/watermarkImages")[0].InnerText.Trim().ToLower();
  121.                     config_watermarkImages_path.Value = config.SelectNodes("//configuration/watermarkImages_path")[0].InnerText.Trim();
  122.                     config_watermarkText.Value = config.SelectNodes("//configuration/watermarkText")[0].InnerText.Trim();
  123.                     config_watermakOption.Value = config.SelectNodes("//configuration/watermarkOption")[0].InnerText.Trim();
  124.                     RadioButtonList1.Items[0].Text = ResourceManager.GetString("localupload");
  125.                     RadioButtonList1.Items[1].Text = ResourceManager.GetString("remoteupload");
  126.                     remoteupload.Text = ResourceManager.GetString("upload");
  127.                 }
  128.                 else if(config_type.Value == "File")
  129.                 {
  130.                     config_fileFilters.Value = config.SelectNodes("//configuration/fileFilters")[0].InnerText;
  131.                 }
  132.                 else if (config_type.Value == "Media")
  133.                 {
  134.                     config_fileFilters.Value = config.SelectNodes("//configuration/mediaFilters")[0].InnerText;
  135.                 }
  136.                 else if (config_type.Value == "Template")
  137.                 {
  138.                     config_fileFilters.Value = config.SelectNodes("//configuration/templateFilters")[0].InnerText;
  139.                     insertTemplate.Text = ResourceManager.GetString("inserttemplate");
  140.                 }
  141.                 config_maxAllUploadSize.Value = config.SelectNodes("//configuration/maxAllUploadSize")[0].InnerText.Trim();
  142.                 config_autoname.Value = config.SelectNodes("//configuration/autoname")[0].InnerText.Trim().ToLower();
  143.                 config_allowUpload.Value = config.SelectNodes("//configuration/allowUpload")[0].InnerText.Trim().ToLower();
  144.                 config_maxSingleUploadSize.Value = config.SelectNodes("//configuration/maxSingleUploadSize")[0].InnerText.Trim();
  145.                 config_fileListBox.Value=config.SelectNodes("//configuration/fileListBox")[0].InnerText;
  146.                 selectAllBtn.Text = ResourceManager.GetString("selectall");
  147.                 editBtn.Text = ResourceManager.GetString("rename");
  148.                 deleteBtn.Text = ResourceManager.GetString("delete");
  149.                 canceloading.Text = ResourceManager.GetString("canceloading");
  150.                 uploadBtn.Text = ResourceManager.GetString("upload");
  151.                 
  152.                 File_List.Columns[1].HeaderText = ResourceManager.GetString("filename");
  153.                 File_List.Columns[2].HeaderText = ResourceManager.GetString("creationtime");
  154.                 File_List.Columns[3].HeaderText = ResourceManager.GetString("filesize");
  155.                 
  156.                 
  157.                 if (config.SelectNodes("//configuration/delete")[0].InnerText == "false")
  158.                     deleteBtn.Enabled = false;
  159.                 if (config.SelectNodes("//configuration/edit")[0].InnerText == "false")
  160.                     editBtn.Enabled = false;
  161.                 if (config_fileListBox.Value=="false")
  162.                     File_List.Visible = false;
  163.                 maxSingleUploadSize.Text = Double.Parse(config_maxSingleUploadSize.Value) < 1024 ? config_maxSingleUploadSize.Value + "KB" : (Double.Parse(config_maxSingleUploadSize.Value) / 1024).ToString("f2") + "MB";
  164.                 
  165.                 if (Request.Cookies["uploadFolder"] != null)
  166.                 {
  167.                     path.Text = Server.UrlDecode(Request.Cookies["uploadFolder"].Value.ToLower());
  168.                 }
  169.                 if (path.Text=="" || config_allowUpload.Value != "true")
  170.                 {
  171.                     FileUpload1.Enabled = false;
  172.                     uploadBtn.Enabled = false;
  173.                     try
  174.                     {
  175.                         remoteupload.Enabled = false;
  176.                         remoteurl.Enabled = false;
  177.                     }
  178.                     catch
  179.                     {
  180.                     }
  181.                     space.Text = "0MB";
  182.                     useSpace.Text = "0MB";
  183.                     path.Text = "";
  184.                     File_List.Visible = false;
  185.                     deleteBtn.Enabled = false;
  186.                     editBtn.Enabled = false;
  187.                     selectAllBtn.Enabled = false;
  188.                 }
  189.                 else
  190.                 {
  191.                     vrPath = Server.MapPath(path.Text);
  192.                     if (!Directory.Exists(vrPath))
  193.                         Directory.CreateDirectory(vrPath);
  194.                     getDirSize(vrPath);
  195.                 }
  196.             }
  197.         }
  198.         /// <summary>
  199.         /// 返回目录里文件的空间使用量(MB)
  200.         /// </summary>
  201.         protected void getDirSize(string vrPath)
  202.         {
  203.             Double Size = 0;
  204.             if (config_fileListBox.Value != "false")
  205.             {
  206.                 
  207.                 DirectoryInfo d = new DirectoryInfo(vrPath);
  208.                 FileInfo[] fis = d.GetFiles();
  209.                 ArrayList showfile = new ArrayList();
  210.                 Array.Sort(fis,new SortFile());
  211.                 string[] Filters = config_fileFilters.Value.Split(',');
  212.                 foreach (FileInfo fi in fis)
  213.                 {
  214.                     Size += fi.Length;
  215.                     for (int i = 0; i <= Filters.Length - 1; i++)
  216.                     {
  217.                         if (fi.Extension.ToLower() == "." + Filters[i].ToString().ToLower())
  218.                         {
  219.                             showfile.Add(fi);
  220.                             break;
  221.                         }
  222.                     }
  223.                     
  224.                 }
  225.                 File_List.DataSource = showfile;
  226.                 File_List.DataBind();
  227.             }
  228.             Size = Convert.ToDouble((Double)Size / 1024);
  229.             useSpace.Text = Size < 1024 ? Size.ToString("f1") + "KB" : (Size / 1024).ToString("f2") + "MB";
  230.             space.Text = (Double.Parse(config_maxAllUploadSize.Value) - Size) < 1024 ? (Double.Parse(config_maxAllUploadSize.Value) - Size).ToString("f1") + "KB" : ((Double.Parse(config_maxAllUploadSize.Value) - Size) / 1024).ToString("f2") + "MB";
  231.             uploadBtn.CommandArgument = Convert.ToString(Size);
  232.             if (Size >= Double.Parse(config_maxAllUploadSize.Value))
  233.             {
  234.                 FileUpload1.Enabled = false;
  235.                 uploadBtn.Enabled = false;
  236.             }
  237.         }
  238.         /// <summary>
  239.         /// 处理切换本地及远程上传的操作
  240.         /// </summary>
  241.         protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
  242.         {
  243.             if (RadioButtonList1.SelectedValue == "remote")
  244.             {
  245.                 remoteupload.Visible = true;
  246.                 remoteurl.Visible = true;
  247.                 FileUpload1.Visible = false;
  248.                 uploadBtn.Visible = false;
  249.                 
  250.             }
  251.             else
  252.             {
  253.                 remoteupload.Visible = false;
  254.                 remoteurl.Visible = false;
  255.                 FileUpload1.Visible = true;
  256.                 uploadBtn.Visible = true;
  257.             }
  258.         }
  259.         /// <summary>
  260.         /// 处理远程上传的操作
  261.         /// </summary>
  262.         protected void remoteupload_Click(object sender, EventArgs e)
  263.         {
  264.             if (remoteurl.Text!="")
  265.             {
  266.                 
  267.                 //string name = FileUpload1.PostedFile.FileName;
  268.                 //FileInfo file = new FileInfo(name);
  269.                 string[] Filters = config_fileFilters.Value.Split(',');
  270.                 string[] afilename = remoteurl.Text.ToLower().Split('/');
  271.          
  272.                 bool errortype = true;
  273.                 string[] bfilename = afilename[afilename.Length - 1].Split('.');
  274.                 string typeName = bfilename[bfilename.Length - 1].ToLower();
  275.                 //ClientScript.RegisterStartupScript(typeof(Page), "Key", "alert('"+typeName+"')", true);
  276.                 for (int i = 0; i <= Filters.Length - 1; i++)
  277.                 {
  278.                     if (typeName == Filters[i].ToString().ToLower())
  279.                     {
  280.                         errortype = false;
  281.                         break;
  282.                     }
  283.                 }
  284.                 
  285.                 if (!errortype)
  286.                 {
  287.                     HttpWebRequest hwq = (HttpWebRequest)WebRequest.Create(remoteurl.Text.ToLower());
  288.                     HttpWebResponse hwr = (HttpWebResponse)hwq.GetResponse();
  289.                     if (hwr.ContentLength < Double.Parse(config_maxSingleUploadSize.Value) * 1024 && Double.Parse(uploadBtn.CommandArgument) * 1024 + hwr.ContentLength < Double.Parse(config_maxAllUploadSize.Value) * 1024)
  290.                     {
  291.                         string uploadPath = path.Text;
  292.                         string fileName;
  293.                         if (config_autoname.Value == "true")
  294.                         {
  295.                             string y = DateTime.Now.Year.ToString();
  296.                             string m = DateTime.Now.Month.ToString();
  297.                             string d = DateTime.Now.Day.ToString();
  298.                             string h = DateTime.Now.Hour.ToString();
  299.                             string n = DateTime.Now.Minute.ToString();
  300.                             string s = DateTime.Now.Second.ToString();
  301.                             fileName = y + m + d + h + n + s;
  302.                             Random r = new Random();
  303.                             fileName = fileName + r.Next(1000);
  304.                             fileName = fileName + "."+typeName;
  305.                         }
  306.                         else
  307.                         {
  308.                             fileName = afilename[afilename.Length - 1];
  309.                         }
  310.                         string webFilePath = Server.MapPath(uploadPath + fileName);
  311.                         if (!File.Exists(webFilePath))
  312.                         {
  313.                             try
  314.                             {
  315.                                 System.Drawing.Image bmp = System.Drawing.Image.FromStream(hwr.GetResponseStream());
  316.                                 bmp.Save(webFilePath);
  317.                                 if (config_watermark.Value == "true")
  318.                                 {
  319.                                     if (config_watermarkName.Value == "false")
  320.                                     {
  321.                                         File.Move(webFilePath, Server.MapPath(uploadPath + "temp_" + fileName));
  322.                                         watermarkText(Server.MapPath(uploadPath + "temp_" + fileName), webFilePath);
  323.                                         File.Delete(Server.MapPath(uploadPath + "temp_" + fileName));
  324.                                     }
  325.                                     else
  326.                                     {
  327.                                         watermarkText(webFilePath, Server.MapPath(uploadPath + config_watermarkName.Value + fileName));
  328.                                     }
  329.                                 }
  330.                                 else if (config_watermarkImages.Value == "true")
  331.                                 {
  332.                                     if (config_watermarkImagesName.Value == "false")
  333.                                     {
  334.                                         File.Move(webFilePath, Server.MapPath(uploadPath + "temp_" + fileName));
  335.                                         watermarkImages(Server.MapPath(uploadPath + "temp_" + fileName), webFilePath, Server.MapPath(config_watermarkImages_path.Value));
  336.                                         File.Delete(Server.MapPath(uploadPath + "temp_" + fileName));
  337.                                     }
  338.                                     else
  339.                                     {
  340.                                         watermarkImages(webFilePath, Server.MapPath(uploadPath + config_watermarkImagesName.Value + fileName), Server.MapPath(config_watermarkImages_path.Value));
  341.                                     }
  342.                                 }
  343.                                 if (config_smallImages.Value == "true")
  344.                                 {
  345.                                     if (config_smallImagesName.Value == "false")
  346.                                     {
  347.                                         File.Move(webFilePath, Server.MapPath(uploadPath + "temp_" + fileName));
  348.                                         MakeThumbnail(Server.MapPath(uploadPath + "temp_" + fileName), webFilePath, Int32.Parse(config_smallImagesW.Value), Int32.Parse(config_smallImagesH.Value), config_smallImagesType.Value);
  349.                                         File.Delete(Server.MapPath(uploadPath + "temp_" + fileName));
  350.                                     }
  351.                                     else
  352.                                     {
  353.                                         MakeThumbnail(webFilePath, Server.MapPath(uploadPath + config_smallImagesName.Value + fileName), Int32.Parse(config_smallImagesW.Value), Int32.Parse(config_smallImagesH.Value), config_smallImagesType.Value);
  354.                                     }
  355.                                 }
  356.                                 if (config_fileListBox.Value != "false")
  357.                                 {
  358.                                     getDirSize(Server.MapPath(uploadPath));
  359.                                 }
  360.                                 file_path.Text = uploadPath + fileName;
  361.                                 ClientScript.RegisterStartupScript(typeof(Page), "Key", "alert('" + ResourceManager.GetString("uploadsuccessful") + "')", true);
  362.                             }
  363.                             catch (Exception ex)
  364.                             {
  365.                                 ClientScript.RegisterStartupScript(typeof(Page), "Key", "alert('" + ResourceManager.GetString("uploadfail") + ex.Message + "')", true);
  366.                             }
  367.                         }
  368.                         else
  369.                         {
  370.                             ClientScript.RegisterStartupScript(typeof(Page), "Key", "alert('" + ResourceManager.GetString("exist") + "')", true);
  371.                         }
  372.                     }
  373.                     else
  374.                     {
  375.                         ClientScript.RegisterStartupScript(typeof(Page), "Key", "alert('" + ResourceManager.GetString("overmaxsize") + "')", true);
  376.                     }
  377.                     hwr.Close();
  378.                 }
  379.                 else
  380.                 {
  381.                     ClientScript.RegisterStartupScript(typeof(Page), "Key", "alert('" + ResourceManager.GetString("errortype") + "')", true);
  382.                 }
  383.             }
  384.         }
  385.         /// <summary>
  386.         /// 处理本地上传的操作
  387.         /// </summary>
  388.         protected void UploadBtn_Click(object sender, EventArgs e)
  389.         {
  390.             if (FileUpload1.HasFile)
  391.             {
  392.                 string name = FileUpload1.PostedFile.FileName;
  393.                 FileInfo file = new FileInfo(name);
  394.                 string[] Filters = config_fileFilters.Value.Split(',');
  395.                 bool errortype = true;
  396.                 string typeName = file.Extension.ToLower();
  397.                 
  398.                 for (int i = 0; i <= Filters.Length - 1; i++)
  399.                 {
  400.                     if (typeName == "."+Filters[i].ToString().ToLower())
  401.                     {
  402.                         errortype = false;
  403.                         break;
  404.                     }
  405.                 }
  406.                 if (!errortype)
  407.                 {
  408.                     if (FileUpload1.PostedFile.ContentLength < Double.Parse(config_maxSingleUploadSize.Value) * 1024 && Double.Parse(uploadBtn.CommandArgument) * 1024 + FileUpload1.PostedFile.ContentLength < Double.Parse(config_maxAllUploadSize.Value) * 1024)
  409.                     {
  410.                         string uploadPath = path.Text;
  411.                         string fileName;
  412.                         if (config_autoname.Value == "true")
  413.                         {
  414.                             string y = DateTime.Now.Year.ToString();
  415.                             string m = DateTime.Now.Month.ToString();
  416.                             string d = DateTime.Now.Day.ToString();
  417.                             string h = DateTime.Now.Hour.ToString();
  418.                             string n = DateTime.Now.Minute.ToString();
  419.                             string s = DateTime.Now.Second.ToString();
  420.                             fileName = y + m + d + h + n + s;
  421.                             Random r = new Random();
  422.                             fileName = fileName + r.Next(1000);
  423.                             fileName = fileName + typeName;
  424.                         }
  425.                         else
  426.                         {
  427.                             fileName = file.Name;
  428.                         }
  429.                         string webFilePath = Server.MapPath(uploadPath + fileName);
  430.                         if (!File.Exists(webFilePath))
  431.                         {
  432.                             try
  433.                             {
  434.                                 FileUpload1.SaveAs(webFilePath);
  435.                                 if (config_watermark.Value == "true")
  436.                                 {
  437.                                     if (config_watermarkName.Value == "false")
  438.                                     {
  439.                                         File.Move(webFilePath, Server.MapPath(uploadPath + "temp_" + fileName));
  440.                                         watermarkText(Server.MapPath(uploadPath + "temp_" + fileName), webFilePath);
  441.                                         File.Delete(Server.MapPath(uploadPath + "temp_" + fileName));
  442.                                     }
  443.                                     else
  444.                                     {
  445.                                         watermarkText(webFilePath,Server.MapPath(uploadPath + config_watermarkName.Value + fileName));
  446.                                     }
  447.                                 }
  448.                                 else if (config_watermarkImages.Value == "true")
  449.                                 {
  450.                                     if (config_watermarkImagesName.Value == "false")
  451.                                     {
  452.                                         File.Move(webFilePath, Server.MapPath(uploadPath + "temp_" + fileName));
  453.                                         watermarkImages(Server.MapPath(uploadPath + "temp_" + fileName),webFilePath, Server.MapPath(config_watermarkImages_path.Value));
  454.                                         File.Delete(Server.MapPath(uploadPath + "temp_" + fileName));
  455.                                     }
  456.                                     else
  457.                                     {
  458.                                         watermarkImages(webFilePath, Server.MapPath(uploadPath + config_watermarkImagesName.Value + fileName), Server.MapPath(config_watermarkImages_path.Value));
  459.                                     }
  460.                                 }
  461.                                 if (config_smallImages.Value == "true")
  462.                                 {
  463.                                     if (config_smallImagesName.Value == "false")
  464.                                     {
  465.                                         File.Move(webFilePath, Server.MapPath(uploadPath + "temp_" + fileName));
  466.                                         MakeThumbnail(Server.MapPath(uploadPath + "temp_" + fileName), webFilePath, Int32.Parse(config_smallImagesW.Value),Int32.Parse(config_smallImagesH.Value), config_smallImagesType.Value);
  467.                                         File.Delete(Server.MapPath(uploadPath + "temp_" + fileName));
  468.                                     }
  469.                                     else
  470.                                     {
  471.                                         MakeThumbnail(webFilePath, Server.MapPath(uploadPath + config_smallImagesName.Value + fileName), Int32.Parse(config_smallImagesW.Value), Int32.Parse(config_smallImagesH.Value), config_smallImagesType.Value);
  472.                                     }    
  473.                                 }
  474.                                 if (config_fileListBox.Value != "false")
  475.                                 {
  476.                                     getDirSize(Server.MapPath(uploadPath));
  477.                                 }
  478.                                 file_path.Text = uploadPath + fileName;
  479.                                 ClientScript.RegisterStartupScript(typeof(Page), "Key", "alert('" + ResourceManager.GetString("uploadsuccessful") + "')", true);
  480.                             }
  481.                             catch (Exception ex)
  482.                             {
  483.                                 ClientScript.RegisterStartupScript(typeof(Page), "Key", "alert('" + ResourceManager.GetString("uploadfail") + ex.Message + "')", true);
  484.                             }
  485.                         }
  486.                         else
  487.                         {
  488.                             ClientScript.RegisterStartupScript(typeof(Page), "Key", "alert('" + ResourceManager.GetString("exist") + "')", true);
  489.                         }
  490.                     }
  491.                     else
  492.                     {
  493.                         ClientScript.RegisterStartupScript(typeof(Page), "Key", "alert('" + ResourceManager.GetString("overmaxsize") + "')", true);
  494.                     }
  495.                 }
  496.                 else
  497.                 {
  498.                     ClientScript.RegisterStartupScript(typeof(Page), "Key", "alert('" + ResourceManager.GetString("errortype") + "')", true);
  499.                 }
  500.             }
  501.         }
  502.         /// <summary>
  503.         /// 处理缩略图操作
  504.         /// </summary>
  505.         public void MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height, string mode)
  506.         {
  507.             System.Drawing.Image originalImage = System.Drawing.Image.FromFile(originalImagePath);
  508.             int towidth = width;
  509.             int toheight = height;
  510.             int x = 0;
  511.             int y = 0;
  512.             int ow = originalImage.Width;
  513.             int oh = originalImage.Height;
  514.             switch (mode)
  515.             {
  516.                 case "HW"://指定高宽缩放(可能变形)                
  517.                     break;
  518.                 case "W"://指定宽,高按比例                    
  519.                     toheight = originalImage.Height * width / originalImage.Width;
  520.                     break;
  521.                 case "H"://指定高,宽按比例
  522.                     towidth = originalImage.Width * height / originalImage.Height;
  523.                     break;
  524.                 case "Cut"://指定高宽裁减(不变形)                
  525.                     if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight)
  526.                     {
  527.                         oh = originalImage.Height;
  528.                         ow = originalImage.Height * towidth / toheight;
  529.                         y = 0;
  530.                         x = (originalImage.Width - ow) / 2;
  531.                     }
  532.                     else
  533.                     {
  534.                         ow = originalImage.Width;
  535.                         oh = originalImage.Width * height / towidth;
  536.                         x = 0;
  537.                         y = (originalImage.Height - oh) / 2;
  538.                     }
  539.                     break;
  540.                 default:
  541.                     break;
  542.             }
  543.             //新建一个bmp图片
  544.             System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight);
  545.             //新建一个画板
  546.             System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);
  547.             //设置高质量插值法
  548.             g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
  549.             //设置高质量,低速度呈现平滑程度
  550.             g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
  551.             //清空画布并以透明背景色填充
  552.             g.Clear(System.Drawing.Color.Transparent);
  553.             //在指定位置并且按指定大小绘制原图片的指定部分
  554.             g.DrawImage(originalImage, new System.Drawing.Rectangle(0, 0, towidth, toheight),
  555.                 new System.Drawing.Rectangle(x, y, ow, oh),
  556.                 System.Drawing.GraphicsUnit.Pixel);
  557.             try
  558.             {
  559.                 //以jpg格式保存缩略图
  560.                 bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg);
  561.             }
  562.             catch (System.Exception e)
  563.             {
  564.                 throw e;
  565.             }
  566.             finally
  567.             {
  568.                 originalImage.Dispose();
  569.                 bitmap.Dispose();
  570.                 g.Dispose();
  571.             }
  572.         }
  573.         /// <summary>
  574.         /// 处理文字水印的操作
  575.         /// </summary>
  576.         protected void watermarkText(string Path,string newPath)
  577.         {
  578.             System.Drawing.Image image = System.Drawing.Image.FromFile(Path);
  579.             try
  580.             {
  581.                 System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(image);
  582.                 g.DrawImage(image, 0, 0, image.Width, image.Height);
  583.                 System.Drawing.Font f = new System.Drawing.Font("System", 12);
  584.                 System.Drawing.Brush b = new System.Drawing.SolidBrush(System.Drawing.Color.Blue);
  585.                 int x, y;
  586.                 if (inputx.Enabled && inputy.Enabled)
  587.                 {
  588.                     x = Int32.Parse(inputx.Text);
  589.                     y = Int32.Parse(inputy.Text);
  590.                 }
  591.                 else
  592.                 {
  593.                     x = 15;
  594.                     y = 15;
  595.                 }
  596.                 g.DrawString(config_watermarkText.Value, f, b, x, y);
  597.                 g.Dispose();
  598.                 image.Save(newPath);
  599.                 image.Dispose();
  600.             }
  601.             catch
  602.             {
  603.                 image.Save(newPath);
  604.                 image.Dispose();
  605.             }
  606.             
  607.         }
  608.         /// <summary>
  609.         /// 处理图片水印的操作
  610.         /// </summary>
  611.         protected void watermarkImages(string Path,string newPath,string images_path)
  612.         {
  613.             System.Drawing.Image image = System.Drawing.Image.FromFile(Path);
  614.             System.Drawing.Image copyImage = System.Drawing.Image.FromFile(images_path);
  615.             try
  616.             {
  617.                 System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(image);
  618.                 int x, y;
  619.                 if (inputx.Enabled && inputy.Enabled)
  620.                 {
  621.                     x = Int32.Parse(inputx.Text);
  622.                     y = Int32.Parse(inputy.Text);
  623.                 }
  624.                 else
  625.                 {
  626.                     x = 0;
  627.                     y = 0;
  628.                 }
  629.                 g.DrawImage(copyImage, new System.Drawing.Rectangle(x,y, copyImage.Width, copyImage.Height), 0, 0, copyImage.Width, copyImage.Height, System.Drawing.GraphicsUnit.Pixel);
  630.                 //g.DrawImage(copyImage, new System.Drawing.Rectangle(image.Width - copyImage.Width-x, image.Height - copyImage.Height-y, copyImage.Width, copyImage.Height), 0, 0, copyImage.Width, copyImage.Height, System.Drawing.GraphicsUnit.Pixel);
  631.                 g.Dispose();
  632.                 image.Save(newPath);
  633.                 image.Dispose();
  634.             }
  635.             catch
  636.             {
  637.                 image.Save(newPath);
  638.                 image.Dispose();
  639.             }
  640.         }
  641.         /// <summary>
  642.         /// 处理全选的操作
  643.         /// </summary>
  644.         protected void checkAll(object sender, EventArgs e)
  645.         {
  646.             CheckBox cb = (CheckBox)sender;
  647.             if (cb.Text == ResourceManager.GetString("selectall"))
  648.             {
  649.                 foreach (GridViewRow gvr in File_List.Rows)
  650.                 {
  651.                     CheckBox cb1 = (CheckBox)gvr.FindControl("check");
  652.                     cb1.Checked = cb.Checked;
  653.                 }
  654.                 if (cb.Checked)
  655.                 {
  656.                     selectAllBtn.Text = ResourceManager.GetString("unselect");
  657.                 }
  658.                 else
  659.                 {
  660.                     selectAllBtn.Text = ResourceManager.GetString("selectall");
  661.                 }
  662.             }
  663.         }
  664.         /// <summary>
  665.         /// 处理全选按键的操作
  666.         /// </summary>
  667.         protected void selectAllBtn_Click(object sender, EventArgs e)
  668.         {
  669.             try
  670.             {
  671.                 if (selectAllBtn.Text == ResourceManager.GetString("selectall"))
  672.                 {
  673.                     CheckBox cb = (CheckBox)File_List.HeaderRow.FindControl("checkall");
  674.                     cb.Checked = true;
  675.                     foreach (GridViewRow gvr in File_List.Rows)
  676.                     {
  677.                         CheckBox cb1 = (CheckBox)gvr.FindControl("check");
  678.                         cb1.Checked = true;
  679.                     }
  680.                     selectAllBtn.Text = ResourceManager.GetString("unselect");
  681.                 }
  682.                 else
  683.                 {
  684.                     CheckBox cb = (CheckBox)File_List.HeaderRow.FindControl("checkall");
  685.                     cb.Checked = false;
  686.                     foreach (GridViewRow gvr in File_List.Rows)
  687.                     {
  688.                         CheckBox cb1 = (CheckBox)gvr.FindControl("check");
  689.                         cb1.Checked = false;
  690.                     }
  691.                     selectAllBtn.Text = ResourceManager.GetString("selectall");
  692.                 }
  693.             }
  694.             catch
  695.             {
  696.                 ClientScript.RegisterStartupScript(typeof(Page), "Key", "alert('"+ResourceManager.GetString("noselect")+"')", true);
  697.             }
  698.         }
  699.         /// <summary>
  700.         /// 处理插入模板的操作
  701.         /// </summary>
  702.         protected void insertTemplate_Click(object sender, EventArgs e)
  703.         {
  704.             if (file_path.Text != "")
  705.             {
  706.                 string line;
  707.                 StringBuilder strhtml = new StringBuilder();
  708.                 StreamReader sr = new StreamReader(Server.UrlDecode(HttpContext.Current.Request.Cookies["configpath"].Value)+file_path.Text.ToLower(), System.Text.Encoding.Default);
  709.                     while ((line = sr.ReadLine()) != null)
  710.                     {
  711.                         strhtml.Append(line);
  712.                     }
  713.                     sr.Close();
  714.                     templateContent.Value = strhtml.ToString();
  715.                     ClientScript.RegisterStartupScript(typeof(Page), "Key", "insertTemplate();", true);
  716.             }
  717.         }
  718.         /// <summary>
  719.         /// 处理删除按键的操作
  720.         /// </summary>
  721.         protected void deleteBtn_Click(object sender, EventArgs e)
  722.         {
  723.             foreach (GridViewRow dl in File_List.Rows)
  724.             {
  725.                 CheckBox cb = (CheckBox)dl.FindControl("check");
  726.                 if (cb.Checked)
  727.                 {
  728.                     GridViewRow row = (GridViewRow)cb.NamingContainer;
  729.                     File.Delete(Server.MapPath(path.Text + (File_List.Rows[row.RowIndex].FindControl("ListID") as Label).Text));
  730.                 }
  731.             }
  732.             
  733.             getDirSize(Server.MapPath(path.Text));
  734.         }
  735.         /// <summary>
  736.         /// 处理重命名按键的操作
  737.         /// </summary>
  738.         protected void editBtn_Click(object sender, EventArgs e)
  739.         {
  740.             foreach (GridViewRow dl in File_List.Rows)
  741.             {
  742.                 CheckBox cb = (CheckBox)dl.FindControl("check");
  743.                 if (cb.Checked)
  744.                 {
  745.                     GridViewRow row = (GridViewRow)cb.NamingContainer;
  746.                     File_List.EditIndex = row.RowIndex;
  747.                     getDirSize(Server.MapPath(path.Text));
  748.                     (File_List.Rows[File_List.EditIndex].FindControl("editName") as TextBox).Focus();
  749.                     break;
  750.                 }
  751.             }
  752.         }
  753.         /// <summary>
  754.         /// 处理全部不选的操作
  755.         /// </summary>
  756.         protected void File_List_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
  757.         {
  758.             File_List.EditIndex = -1;
  759.             getDirSize(Server.MapPath(path.Text));
  760.         }
  761.         /// <summary>
  762.         /// 处理重命名操作
  763.         /// </summary>
  764.         protected void File_List_RowUpdating(object sender, GridViewUpdateEventArgs e)
  765.         {
  766.             string oldname = (File_List.Rows[e.RowIndex].FindControl("editBtn") as Button).CommandArgument;
  767.             string[] typename = oldname.Split('.');
  768.             File.Move(Server.MapPath(path.Text + oldname), Server.MapPath(path.Text) + (File_List.Rows[e.RowIndex].FindControl("editName") as TextBox).Text.Replace(""", string.Empty).Replace("/", string.Empty).Replace("\", string.Empty) + "." + typename[typename.Length - 1].ToLower());
  769.             File_List.EditIndex = -1;
  770.             getDirSize(Server.MapPath(path.Text));
  771.         }
  772.     }
  773.     #endregion
  774. }