using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Collections;
using System.Security;
using System.Security.Cryptography;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;
using System.Configuration;
using System.Drawing.Imaging;
using System.Drawing;
namespace demoClass.Class
{
/// <summary>
/// Common 的摘要说明。
/// </summary>
public class CommonClass:Page
{
static string FILETYPE=System.Configuration.ConfigurationSettings.AppSettings["fileType"];
static int FILESIZE=Convert.ToInt32(System.Configuration.ConfigurationSettings.AppSettings["fileSize"]);
System.Drawing.Image.GetThumbnailImageAbort ball=null;
System.Drawing.Image FormImg,img;
char[] sep={'|'};
public CommonClass(){}
#region 获得CheckBoxLis,RadioButtonList,ListBox控件所有选中值并以逗号格开,形式:"2|3|4
/// <summary>
/// 获得CheckBoxLis,RadioButtonList,ListBox控件所有选中值并以逗号格开,形式:"2|3|4"
/// </summary>
/// <param name="listctrl">CheckBoxLis,RadioButtonList,ListBox控件</param>
/// <param name="type">类型,是想获得Value还是Text值(true为value)</param>
/// <returns></returns>
public static string getCtrlValue(Control listctrl,bool type)
{
string str_Value="";
//===CheckBoxList===
if (listctrl is CheckBoxList)
{
for (int i=0;i<((CheckBoxList)listctrl).Items.Count;i++)
{
if (((CheckBoxList)listctrl).Items[i].Selected==true)
{
if (type==true)
{
str_Value=str_Value+((CheckBoxList)listctrl).Items[i].Value+"|";
}
else
{
str_Value=str_Value+((CheckBoxList)listctrl).Items[i].Text+"|";
}
}
}
if (str_Value!="")
{
str_Value=str_Value.Substring(0,str_Value.Length-1);
}
}
//===RadioButtonList===
if (listctrl is RadioButtonList)
{
for (int i=0;i<((RadioButtonList)listctrl).Items.Count;i++)
{
if (((RadioButtonList)listctrl).Items[i].Selected==true)
{
if(type==true)
{
str_Value=((RadioButtonList)listctrl).Items[i].Value;
}
else
{
str_Value=((RadioButtonList)listctrl).Items[i].Text;
}
}
}
}
//===ListBox===
if (listctrl is ListBox)
{
for (int i=0;i<((ListBox)listctrl).Items.Count;i++)
{
if (((ListBox)listctrl).Items[i].Selected==true)
{
if(type==true)
{
str_Value=str_Value+((ListBox)listctrl).Items[i].Value+"|";
}
else
{
str_Value=str_Value+((ListBox)listctrl).Items[i].Text+"|";
}
}
}
if (str_Value!="")
{
str_Value=str_Value.Substring(0,str_Value.Length-1);
}
}
return str_Value;
}
#endregion
#region 把数据绑定到DropDownList和CheckBoxList控件
/// <summary>
/// 把数据绑定到DropDownList和CheckBoxList控件
/// </summary>
/// <param name="id">控件Value值字段</param>
/// <param name="name">控件Text值字段</param>
/// <param name="lst">IList数据源</param>
/// <param name="listctrl">控件Id</param>
public static void setCtrlValue(string id,string name,IList lst,Control listctrl)
{
//===DropDownList===
if (listctrl is DropDownList)
{
((DropDownList)listctrl).DataValueField=id;
((DropDownList)listctrl).DataTextField=name;
((DropDownList)listctrl).DataSource=lst;
((DropDownList)listctrl).DataBind();
}
//===CheckBoxList===
if (listctrl is CheckBoxList)
{
((CheckBoxList)listctrl).DataValueField=id;
((CheckBoxList)listctrl).DataTextField=name;
((CheckBoxList)listctrl).DataSource=lst;
((CheckBoxList)listctrl).DataBind();
}
//===RadioButtonList===
if (listctrl is RadioButtonList)
{
((RadioButtonList)listctrl).DataValueField=id;
((RadioButtonList)listctrl).DataTextField=name;
((RadioButtonList)listctrl).DataSource=lst;
((RadioButtonList)listctrl).DataBind();
}
//===ListBox===
if (listctrl is ListBox)
{
((ListBox)listctrl).DataValueField=id;
((ListBox)listctrl).DataTextField=name;
((ListBox)listctrl).DataSource=lst;
((ListBox)listctrl).DataBind();
}
}
/// <summary>
/// 把数据绑定到DropDownList和CheckBoxList控件
/// </summary>
/// <param name="DefaultValue">默认的Value值</param>
/// <param name="DefaultText">默认的Text值</param>
/// <param name="id">数据库的字段,用于Value</param>
/// <param name="name">数据库的字段,用于Text</param>
/// <param name="lst"></param>
/// <param name="listctrl"></param>
public static void setCtrlValue(string DefaultValue,string DefaultText,string id,string name,IList lst,Control listctrl)
{
//===DropDownList===
if (listctrl is DropDownList)
{
((DropDownList)listctrl).DataValueField=id;
((DropDownList)listctrl).DataTextField=name;
((DropDownList)listctrl).DataSource=lst;
((DropDownList)listctrl).DataBind();
ListItem it=new ListItem(DefaultText,DefaultValue);
it.Selected=true;
((DropDownList)listctrl).Items.Insert(0,it);
}
//==================CheckBoxList===========
if (listctrl is CheckBoxList)
{
((CheckBoxList)listctrl).DataValueField=id;
((CheckBoxList)listctrl).DataTextField=name;
((CheckBoxList)listctrl).DataSource=lst;
((CheckBoxList)listctrl).DataBind();
ListItem it=new ListItem(DefaultText,DefaultValue);
it.Selected=true;
((CheckBoxList)listctrl).Items.Insert(0,it);
}
//==================RadioButtonList===========
if (listctrl is RadioButtonList)
{
((RadioButtonList)listctrl).DataValueField=id;
((RadioButtonList)listctrl).DataTextField=name;
((RadioButtonList)listctrl).DataSource=lst;
((RadioButtonList)listctrl).DataBind();
ListItem it=new ListItem(DefaultText,DefaultValue);
it.Selected=true;
((RadioButtonList)listctrl).Items.Insert(0,it);
}
//==================ListBox===========
if (listctrl is ListBox)
{
((ListBox)listctrl).DataValueField=id;
((ListBox)listctrl).DataTextField=name;
((ListBox)listctrl).DataSource=lst;
((ListBox)listctrl).DataBind();
ListItem it=new ListItem(DefaultText,DefaultValue);
it.Selected=true;
((ListBox)listctrl).Items.Insert(0,it);
}
}
/// <summary>
/// 把数据绑定到DropDownList和CheckBoxList控件
/// </summary>
/// <param name="strValueField">默认选中的值</param>
/// <param name="DefaultValue">默认的value值</param>
/// <param name="DefaultText">默认的Text值</param>
/// <param name="id">数据库的字段,用于Value</param>
/// <param name="name">数据库的字段,用于text</param>
/// <param name="lst">数据源Ilist</param>
/// <param name="listctrl">控件的ID</param>
public static void setCtrlValue(string strValueField,string DefaultValue,string DefaultText,string id,string name,IList lst,Control listctrl)
{
//===DropDownList===
if (listctrl is DropDownList)
{
((DropDownList)listctrl).DataValueField=id;
((DropDownList)listctrl).DataTextField=name;
((DropDownList)listctrl).DataSource=lst;
((DropDownList)listctrl).DataBind();
ListItem it=new ListItem(DefaultText,DefaultValue);
((DropDownList)listctrl).Items.Insert(0,it);
}
if (listctrl is DropDownList)
{
for (int i=0;i<((DropDownList)listctrl).Items.Count;i++)
{
if (strValueField==((DropDownList)listctrl).Items[i].Value.ToString())
{
((DropDownList)listctrl).Items[i].Selected=true;
}
}
}
//===CheckBoxList===
if (listctrl is CheckBoxList)
{
((CheckBoxList)listctrl).DataValueField=id;
((CheckBoxList)listctrl).DataTextField=name;
((CheckBoxList)listctrl).DataSource=lst;
((CheckBoxList)listctrl).DataBind();
ListItem it=new ListItem(DefaultText,DefaultValue);
((CheckBoxList)listctrl).Items.Insert(0,it);
}
if (listctrl is CheckBoxList)
{
for (int i=0;i<((CheckBoxList)listctrl).Items.Count;i++)
{
if (strValueField==((CheckBoxList)listctrl).Items[i].Value.ToString())
{
((CheckBoxList)listctrl).Items[i].Selected=true;
}
}
}
//=============RadioButtonList==================
if (listctrl is RadioButtonList)
{
((RadioButtonList)listctrl).DataValueField=id;
((RadioButtonList)listctrl).DataTextField=name;
((RadioButtonList)listctrl).DataSource=lst;
((RadioButtonList)listctrl).DataBind();
ListItem it=new ListItem(DefaultText,DefaultValue);
((RadioButtonList)listctrl).Items.Insert(0,it);
}
if (listctrl is RadioButtonList)
{
for (int i=0;i<((RadioButtonList)listctrl).Items.Count;i++)
{
if (strValueField==((RadioButtonList)listctrl).Items[i].Value.ToString())
{
((RadioButtonList)listctrl).Items[i].Selected=true;
}
}
}
//=============ListBox==================
if (listctrl is ListBox)
{
((ListBox)listctrl).DataValueField=id;
((ListBox)listctrl).DataTextField=name;
((ListBox)listctrl).DataSource=lst;
((ListBox)listctrl).DataBind();
ListItem it=new ListItem(DefaultText,DefaultValue);
((ListBox)listctrl).Items.Insert(0,it);
}
if (listctrl is ListBox)
{
for (int i=0;i<((ListBox)listctrl).Items.Count;i++)
{
if (strValueField==((ListBox)listctrl).Items[i].Value.ToString())
{
((ListBox)listctrl).Items[i].Selected=true;
}
}
}
}
/// <summary>
/// 把数据绑定到DropDownList和CheckBoxList控件
/// </summary>
/// <param name="val">字符串(如:"1|2|3|4|5")以"|"隔开.用于value</param>
/// <param name="txt">字符串(如:"1|2|3|4|5")以"|"隔开.用于text</param>
/// <param name="listctrl">控件的ID</param>
public static void setCtrlValue(string val,string txt,Control listctrl)
{
string[] arr_Val=val.Split('|');
string[] arr_Txt=txt.Split('|');
//===DropDownList===
if (listctrl is DropDownList)
{
for (int i=0;i<arr_Val.Length;i++)
{
ListItem it=new ListItem(arr_Txt[i],arr_Val[i]);
((DropDownList)listctrl).Items.Add(it);
}
}
//===CheckBoxList===
if (listctrl is CheckBoxList)
{
for (int i=0;i<arr_Val.Length;i++)
{
ListItem it=new ListItem(arr_Txt[i],arr_Val[i]);
((CheckBoxList)listctrl).Items.Add(it);
}
}
//===RadioButtonList===
if (listctrl is RadioButtonList)
{
for (int i=0;i<arr_Val.Length;i++)
{
ListItem it=new ListItem(arr_Txt[i],arr_Val[i]);
((RadioButtonList)listctrl).Items.Add(it);
}
}
//===ListBox===
if (listctrl is ListBox)
{
for (int i=0;i<arr_Val.Length;i++)
{
ListItem it=new ListItem(arr_Txt[i],arr_Val[i]);
((ListBox)listctrl).Items.Add(it);
}
}
}
#endregion
#region 服务器端弹出alert对话框
/// <summary>
/// 服务器端弹出alert对话框
/// </summary>
/// <param name="str_Message">提示信息,例子:"请输入您姓名!"</param>
/// <param name="page">Page类</param>
public static void Alert(string str_Message,Page page)
{
page.RegisterStartupScript("","<script>alert('"+str_Message+"');</script>");
}
/// <summary>
/// 服务器端弹出alert对话框
/// </summary>
/// <param name="str_Ctl_Name">获得焦点控件Id值,比如:txt_Name</param>
/// <param name="str_Message">提示信息,例子:"请输入您姓名!"</param>
/// <param name="page">Page类</param>
public static void Alert_select(string str_Ctl_Name,string str_Message,Page page)
{
page.RegisterStartupScript("","<script>alert('"+str_Message+"');document.forms(0)."+str_Ctl_Name+".focus(); document.forms(0)."+str_Ctl_Name+".select();</script>");
}
/// <summary>
///
/// </summary>
/// <param name="str_Ctl_Name"></param>
/// <param name="str_Message"></param>
/// <param name="uploadfile">上传文件成功后弹出专用的!</param>
/// <param name="page"></param>
public static void Alert_upload(string str0,string str1,Page page)
{
page.RegisterStartupScript("","<script language='javascript'>alert(/"文件上传成功!/");SetTitleImg(/""+str0+"/",/""+str1+"/");self.close();</script>");
}
public static void Alert_upload(string str0,Page page)
{
page.RegisterStartupScript("","<script language='javascript'>alert('文件上传成功!');SetTitleImg(/""+str0+"/");self.close();</script>");
}
public static void Alert_YesNO(string _Msg, string URL, Page _Page)
{
string StrScript;
StrScript = ("<script language=javascript>");
StrScript += "var retValue=window.confirm('" + _Msg + "');" + "if(retValue){window.location='" + URL + "';}";
StrScript += ("</script>");
_Page.RegisterStartupScript("MsgBox1", StrScript.ToString());
}
public static void Alert_Redirect(string _Msg, string URL, Page _Page)
{
string StrScript;
StrScript = ("<script language=javascript>");
StrScript += ("alert('" + _Msg + "');");
StrScript += ("window.location='" + URL + "';");
StrScript += ("</script>");
_Page.RegisterStartupScript("MsgBox2", StrScript.ToString());
}
public static void Alert_Close(string _Msg, Page _Page)
{
string StrScript;
StrScript = ("<script language=javascript>");
StrScript += ("alert('" + _Msg + "');");
StrScript += ("_self.close();");
StrScript += ("</script>");
_Page.RegisterStartupScript("MsgBox2", StrScript.ToString());
}
#endregion
#region 弹出新窗口
public static void winopen(string path,string width,string height,Page page)
{
page.RegisterStartupScript("","<script>window.open('"+path+"','','width="+width+" ,height="+height+"');</script>");
}
#endregion
#region 字符串的一系列操作
public static string GetDateRandom()
{
string strData=DateTime.Now.ToString("yyyyMMddhhmmss");
strData=strData.Replace(":","");
strData=strData.Replace("-","");
strData=strData.Replace(" ","");
return strData;
}
/// <summary>
/// 获得某个字符串在另个字符串最后一次出现时后面所有字符
/// </summary>
/// <param name="strOriginal">要处理字符</param>
/// <param name="strSymbol">符号</param>
/// <returns>返回值</returns>
public static string GetLastStr(string strOriginal,string strSymbol)
{
int strPlace=strOriginal.LastIndexOf(strSymbol)+strSymbol.Length;
strOriginal=strOriginal.Substring(strPlace);
return strOriginal;
}
/// <summary>
/// 获得某个字符串在另个字符串第一次出现时后面所有字符
/// </summary>
/// <param name="strOriginal">要处理字符</param>
/// <param name="strSymbol">符号</param>
/// <returns>返回字符</returns>
public static string GetFirstStr(string strOriginal,string strSymbol)
{
int strPlace=strOriginal.IndexOf(strSymbol);
if (strPlace!=-1)
strOriginal=strOriginal.Substring(0,strPlace);
return strOriginal;
}
/// <summary>
/// 去掉HTML格式后的字符串
/// </summary>
/// <param name="text">HTML字符</param>
/// <returns>返回一个去掉HTML后的字符串</returns>
public static string StripTags(string text)
{
text = Regex.Replace(text, @" ", "", RegexOptions.IgnoreCase);
text = Regex.Replace(text, @"/r/n", "", RegexOptions.IgnoreCase);
return Regex.Replace(text, @"<.+?>", "", RegexOptions.Singleline);
}
/// <summary>
/// 截取字符串
/// </summary>
/// <param name="text">要被截取的字符串</param>
/// <param name="length">要被截取的字符串</param>
/// <returns></returns>
public static string CutString(string text,int length)
{
if(text.Length<length)
return text;
else
return text.Substring(0,length)+"...";
}
/// <summary>
/// 截取字符串
/// </summary>
/// <param name="text">要被截取的字符串</param>
/// <param name="length">要被截取的字符串</param>
/// <param name="start">开始位置</param>
/// <returns></returns>
public static string CutString(string text,int length,int start)
{
if(text.Length<length)
return text;
else
return text.Substring(start,length);
}
/// <summary>
/// 替换html中的特殊字符
/// </summary>
/// <param name="theString">需要进行替换的文本。</param>
/// <returns>替换完的文本。</returns>
public static string HtmlEncode(string theString)
{
theString=theString.Replace(">", ">");
theString=theString.Replace("<", "<");
theString=theString.Replace(" ", " ");
theString=theString.Replace(" ", " ");
theString=theString.Replace("/"", """);
theString=theString.Replace("/'", "'");
theString=theString.Replace("/n", "<br/> ");
return theString;
}
/// <summary>
/// 恢复html中的特殊字符
/// </summary>
/// <param name="theString">需要恢复的文本。</param>
/// <returns>恢复好的文本。</returns>
public static string HtmlDiscode(string theString)
{
theString=theString.Replace(">", ">");
theString=theString.Replace("<", "<");
theString=theString.Replace(" "," ");
theString=theString.Replace(" "," ");
theString=theString.Replace(""","/"");
theString=theString.Replace("'","/'");
theString=theString.Replace("<br/> ","/n");
return theString;
}
/// <summary>
/// 过滤非法的自定义字符
/// </summary>
/// <param name="text">过滤非法的自定义字符</param>
/// <param name="str">中间以"|"分开</param>
/// <returns></returns>
public static string ConvertText(string text,string str)
{
if ((text != null) && (text != String.Empty))
{
string[] temp=str.Split('|');
for(int i=0;i<temp.Length-1;i++)
{
for(int j=0;j<temp[i].Length;j++)
{
text=text.Replace(temp[i].ToString(),"*****");
}
}
return text;
}
return null;
}
/// <summary>
/// 去除HTML代码
/// </summary>
/// <param name="Htmlstring"></param>
/// <returns></returns>
public static string NoHTML(string Htmlstring)
{
string strOutput=Htmlstring;
strOutput = Regex.Replace(strOutput,@"<script[^>]*?>.*?</script>","",RegexOptions.IgnoreCase);
// strOutput = Regex.Replace(strOutput,@"<(///s*)?!?((/w+:)?/w+)(/w+(/s*=?/s*(([""'])(在个别情况下会引起IIS吃爆CPU的现象
strOutput = Regex.Replace(strOutput,@"<(.[^>]*)>","",RegexOptions.IgnoreCase);
strOutput = Regex.Replace(strOutput,@"([/r/n])[/s]+","",RegexOptions.IgnoreCase);
strOutput = Regex.Replace(strOutput,@"-->","",RegexOptions.IgnoreCase);
strOutput = Regex.Replace(strOutput,@"<!--.*","",RegexOptions.IgnoreCase);
strOutput = Regex.Replace(strOutput,@"&(quot|#34);","/"",RegexOptions.IgnoreCase);
strOutput = Regex.Replace(strOutput,@"&(amp|#38);","&",RegexOptions.IgnoreCase);
strOutput = Regex.Replace(strOutput,@"&(lt|#60);","<",RegexOptions.IgnoreCase);
strOutput = Regex.Replace(strOutput,@"&(gt|#62);",">",RegexOptions.IgnoreCase);
strOutput = Regex.Replace(strOutput,@"&(nbsp|#160);"," ",RegexOptions.IgnoreCase);
strOutput = Regex.Replace(strOutput,@"&(iexcl|#161);","/xa1",RegexOptions.IgnoreCase);
strOutput = Regex.Replace(strOutput,@"&(cent|#162);","/xa2",RegexOptions.IgnoreCase);
strOutput = Regex.Replace(strOutput,@"&(pound|#163);","/xa3",RegexOptions.IgnoreCase);
strOutput = Regex.Replace(strOutput,@"&(copy|#169);","/xa9",RegexOptions.IgnoreCase);
strOutput = Regex.Replace(strOutput, @"&#(/d+);","",RegexOptions.IgnoreCase);
strOutput.Replace("<","");
strOutput.Replace(">","");
strOutput.Replace("/r/n","");
strOutput=HttpContext.Current.Server.HtmlEncode(strOutput).Trim();
return HttpContext.Current.Server.HtmlEncode(strOutput).Trim();
}
public string getQuertString(string str)
{
if(Request.QueryString["str"]!=null)
{
if(Request.QueryString["str"].Trim()!="")
{
return str;
}
}
return null;
}
public string getForm(string str)
{
if(Request.Form["str"]!=null)
{
if(Request.Form["str"].Trim()!="")
{
return str;
}
}
return null;
}
/// <summary>
/// 检测日期是否正确
/// </summary>
/// <param name="date"></param>
/// <returns></returns>
public static bool IsStringDate(string date)
{
DateTime dt;
try
{
dt=DateTime.Parse(date);
}
catch(FormatException e)
{
//日期格式不正确时
e.ToString();
return false;
}
return true;
}
/// <summary>
/// 判断是否是数字,包括小数和整数。
/// </summary>
/// <param name="_value"></param>
/// <returns></returns>
public static bool IsNumber(string _value)
{
return CommonClass.QuickValidate("^(0|([1-9]+[0-9]*))(.[0-9]+)?$",_value);
}
/// <summary>
/// 检查一个字符串是否是纯数字构成的,一般用于查询字符串参数的有效性验证。
/// </summary>
/// <param name="_value">需验证的字符串。。</param>
/// <returns>是否合法的bool值。</returns>
public static bool IsNumberId(string _value)
{
return CommonClass.QuickValidate("^[1-9]*[0-9]*$",_value);
}
/// <summary>
/// 检查一个字符串是否是纯字母和数字构成的,一般用于查询字符串参数的有效性验证。
/// </summary>
/// <param name="_value">需验证的字符串。</param>
/// <returns>是否合法的bool值。</returns>
public static bool IsLetterOrNumber(string _value)
{
return CommonClass.QuickValidate("^[a-zA-Z0-9_]*$",_value);
}
public static bool QuickValidate(string _express,string _value)
{
System.Text.RegularExpressions.Regex myRegex=new System.Text.RegularExpressions.Regex(_express);
if(_value.Length==0)
{
return false;
}
return myRegex.IsMatch(_value);
}
#endregion
#region 鼠标移上去让DataGrid变色
/// <summary>
/// 鼠标移上时变颜色
/// </summary>
/// <param name="dg"></param>
/// <param name="color"></param>
/// <param name="hoverColor"></param>
public static void DataGridHoverScript(DataGrid dg,string color,string hoverColor)
{
//鼠标移上时变颜色
for(int i=0;i<dg.Items.Count;i++)
{
if(dg.Items[i].ItemType.ToString()=="Item" || dg.Items[i].ItemType.ToString()=="AlternatingItem")
{
TableRow tr=(TableRow)dg.Items[i].Cells[0].Parent;
tr.Attributes.Add("onmouseover","this.bgColor='"+hoverColor+"'");
tr.Attributes.Add("onmouseout","this.bgColor='"+color+"'");
}
}
}
#endregion
#region 文件上传操作
/// <summary>
/// 普通文件上传
/// </summary>
/// <param name="path">文件的存放的目录</param>
/// <param name="fl_Name">HtmlInputFile控件的名称</param>
/// <returns></returns>
public string UpFile(string path,HtmlInputFile fl_Name,Page page)
{
//string filetype=null;
if (fl_Name.PostedFile.FileName.ToString()=="")
{
page.RegisterStartupScript("","<script>alert('请先选择一个文件再上传');</script>");
}
if(fl_Name.PostedFile.ContentLength>FILESIZE)
{
page.RegisterStartupScript("","<script>alert('文件大小限定在"+(FILESIZE/1024)+"MB以内');</script>");
}
string str_filetype=GetLastStr(Path.GetFileName(fl_Name.PostedFile.FileName),"."); // 获得文件类型
string str_RandomFileName=GetDateRandom()+"."+str_filetype; // 获得随机文件名
if ((FILETYPE+"|").IndexOf(str_filetype.ToLower()+"|")==-1)
{
page.RegisterStartupScript("","<script>alert('请选择"+FILETYPE+"等类型文件');</script>");
}
if(File.Exists(Server.MapPath(path+str_RandomFileName)))
{
page.RegisterStartupScript("","<script>alert('该文件已经存在,请改名再上传!');</script>");
}
string strFullPath=Server.MapPath(path+str_RandomFileName);
fl_Name.PostedFile.SaveAs(strFullPath);
return Path.GetFileName(str_RandomFileName);
}
/// <summary>
/// 图片上传并且自动同比例缩微图
/// </summary>
/// <param name="smallpath">存放缩微图的路径</param>
/// <param name="bigpath">存放图片的路局</param>
/// <param name="fl_Name">HtmlInputFile控件的名称</param>
/// <param name="Size">图片的宽度或者高度一方超过size就产生缩微图,否则缩微图就是图片它本身</param>
/// <returns></returns>
public string UpFile(string smallpath,string bigpath,HtmlInputFile fl_Name,int Size,Page page)
{
if (fl_Name.PostedFile.FileName.ToString()=="")
{
page.RegisterStartupScript("","<script>alert('请先选择一个文件再上传');</script>");
}
if(fl_Name.PostedFile.ContentLength>FILESIZE)
{
page.RegisterStartupScript("","<script>alert('文件大小限定在"+(FILESIZE/1024)+"MB以内');</script>");
}
string str_filetype=GetLastStr(Path.GetFileName(fl_Name.PostedFile.FileName),"."); // 获得文件类型
string str_RandomFileName=GetDateRandom()+"."+str_filetype; // 获得随机文件名
if ((FILETYPE+"|").IndexOf(str_filetype.ToLower()+"|")==-1)
{
page.RegisterStartupScript("","<script>alert('请选择"+FILETYPE+"等类型文件');</script>");
}
if(File.Exists(Server.MapPath(bigpath+str_RandomFileName)))
{
page.RegisterStartupScript("","<script>alert('该文件已经存在,请改名再上传!');</script>");
}
string strFullPath=Server.MapPath(bigpath+str_RandomFileName);
fl_Name.PostedFile.SaveAs(strFullPath);
string smallpicname=GetDateRandom()+"."+str_filetype;
string smallpicpath=Server.MapPath(smallpath)+smallpicname;
//return smallpicpath;
CreateThumbnailImage(Size,smallpicpath,strFullPath,str_filetype);
return smallpicname+","+str_RandomFileName;
}
/// <summary>
/// 创建缩微图
/// </summary>
/// <returns></returns>
protected void CreateThumbnailImage(int size,string s_path,string b_path,string fileType)
{
FormImg=System.Drawing.Image.FromFile(b_path);
int w,h=0;
w=FormImg.Width;
h=FormImg.Height;
if(w>size||h>size)
{
if (w>h)
{
//以宽度为比例产生缩微图
img=FormImg.GetThumbnailImage(size,(size*h)/w,ball,new System.IntPtr());
}
else if(h>w)
{
//以高度为比例产生缩微图
img=FormImg.GetThumbnailImage((size*w)/h,size,ball,new System.IntPtr());
//img=FormImg.GetThumbnailImage(size,(size*h)/w,ball,new System.IntPtr());
}
else
{
img=FormImg.GetThumbnailImage((size*w)/h,size,ball,new System.IntPtr());
}
}
else
{
//====不变
img=FormImg.GetThumbnailImage(w,h,ball,new System.IntPtr());
}
if(fileType.ToLower()=="jpg")
img.Save(s_path,ImageFormat.Jpeg);
else if(fileType.ToLower()=="gif")
img.Save(s_path,ImageFormat.Gif);
else if(fileType.ToLower()=="bmp")
img.Save(s_path,ImageFormat.Bmp);
else if(fileType.ToLower()=="png")
img.Save(s_path,ImageFormat.Png);
}
// public IList uploadfileList(string DirectoryName)
// {
// ArrayList arr=null;
// DirectoryInfo DI=new DirectoryInfo(DirectoryName);
// FileInfo[] fi=DI.GetFiles();
// arr=new ArrayList();
// foreach(FileInfo fiTemp in fi)
// {
// uploadfileinfo fileinfo=new uploadfileinfo();
// fileinfo.uploadfilename=fiTemp.Name;
// fileinfo.uploadAccessTime=fiTemp.LastAccessTime;
// fileinfo.uploadDate=fiTemp.CreationTime;
// fileinfo.uploadfilesize=Math.Round(fiTemp.Length/1024.0,1);
// fileinfo.uploadfiletype=fiTemp.Extension.ToLower();
// arr.Add(fileinfo);
// }
// arr.Reverse();
// return arr;
// }
public int uploadfileCount(string DirectoryName)
{
DirectoryInfo DI=new DirectoryInfo(Server.MapPath(DirectoryName));
FileInfo[] fi=DI.GetFiles();
return fi.Length;
}
public void delUploadFile(string fileName)
{
FileInfo fi=new FileInfo(Server.MapPath(fileName));
fi.Delete();
}
#endregion
#region 重定向
/// <summary>
///
/// </summary>
/// <param name="msg">具体的信息</param>
/// <param name="url">返回的URL</param>
/// <param name="targetURL">重定向的目标文件名</param>
public void Redir(string msg,string url,string targetURL)
{
Server.Transfer(targetURL+"?msg="+Server.UrlEncode(msg)+"&url="+Server.UrlEncode(url));
}
/// <summary>
/// 重定向
/// </summary>
/// <param name="msg">具体的信息</param>
/// <param name="url">返回的URL</param>
/// <param name="picUrl">可以显示图片</param>
/// <param name="targetURL">重定向的目标文件名</param>
public void Redir(string msg,string url,string targetURL,string picUrl)
{
Server.Transfer(targetURL+"?msg="+Server.UrlEncode(msg)+"&url="+Server.UrlEncode(url)+"&picUrl="+Server.UrlEncode(picUrl));
}
#endregion
}
}