using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Drawing;
using System.Drawing.Design;
namespace Yingnet.Common
{
/// <summary>
/// FileUpload 的摘要说明。E:programCommonFileUpload.bmp
/// </summary>
[ToolboxBitmap(typeof(Yingnet.Common.FileUpload), "FileUpload.bmp"),
DefaultProperty("Text"), DefaultEvent("Click"),
ToolboxData("<{0}:FileUpload runat=server></{0}:FileUpload>")]
public class FileUpload : System.Web.UI.WebControls.WebControl {
/// <summary>
/// 上传按钮
/// </summary>
private Button button=new Button();
/// <summary>
/// 上传文件个数
/// </summary>
private int filenum=1;
/// <summary>
/// File对象
/// </summary>
private HtmlInputFile[] file;
/// <summary>
/// 保存路径,默认为系统的临时目录
/// </summary>
private string path=System.IO.Path.GetTempPath();
/// <summary>
/// 上传的文件名组
/// </summary>
private string[] filename;
/// <summary>
/// 后缀文件名组
/// </summary>
private string[] suffix;
/// <summary>
///过滤器,写法是.txt;.abc
/// </summary>
private string filter="";
/// <summary>
/// 限制文件上传大小,为0是不限制,单位是字节
/// </summary>
private int size=0;//System.ComponentModel.DefaultEventAttribute
/// <summary>
/// 上传事件
/// </summary>
[Bindable(true),Category("事件"),Description("上传后激发的事件")
]
public event EventHandler Click;
/// <summary>
/// 上传文件数
/// </summary>
[Bindable(true),
Category("参数"),Description("设定上传文件的个数"),
DefaultValue("1")]
public int FileNum{
set{
if(value<1){
value=1;
}
filenum=value;
this.Controls.Clear();
file=new HtmlInputFile[filenum];
filename=new string[filenum];
suffix=new string[filenum];
for(int i=0;i<filenum;i++) {
file[i]=new HtmlInputFile();
this.Controls.Add(file[i]);
}
this.Controls.Add(button);
}
get{
return filenum;
}
}
/// <summary>
/// 上传按钮的文本
/// </summary>
[Bindable(true),
Category("参数"), Description("设定上传文件的路径"),
DefaultValue("1")]
/// <summary>
/// 上传路径
/// </summary>
public string UploadPath {
set{
if("".Equals(value)||value==null){
value=System.IO.Path.GetTempPath();
}
path=value;
}
get{
return path;
}
}
/// <summary>
/// 得到文件名
/// </summary>
public string[] Filename{
get{
return filename;
}
}
/// <summary>
/// 得到后缀
/// </summary>
public string[] Suffix{
get{
return suffix;
}
}
/// <summary>
/// 过滤器
/// </summary>
public string Filter{
set{
filter=value;
}
get{
return filter;
}
}
/// <summary>
/// 限制大小
/// </summary>
public int FileSize{
set{
size=value;
}
get{
return size;
}
}
/// <summary>
/// 快捷键
/// </summary>
public override string AccessKey{
get{
return button.AccessKey;
}
set{
button.AccessKey=value;
}
}
/// <summary>
/// 背景
/// </summary>
public override System.Drawing.Color BackColor{
get{
return button.BackColor;
}
set{
button.BackColor=value;
}
}
/// <summary>
/// 边框颜色
/// </summary>
public override System.Drawing.Color BorderColor{
get{
return button.BorderColor;
}
set{
button.BorderColor=value;
}
}
/// <summary>
/// 边框风格
/// </summary>
public override BorderStyle BorderStyle{
get{
return button.BorderStyle;
}
set{
button.BorderStyle=value;
}
}
/// <summary>
/// 文本
/// </summary>
[Bindable(true),
Category("Appearance"),
DefaultValue("")]
public string Text
{
get
{
return button.Text;
}
set
{
button.Text = value;
}
}
public FileUpload():base(){
FileNum=1;
button.Click+=new EventHandler(this.button_Click);
this.Controls.Add(button);
}
/// <summary>
/// 按钮事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button_Click(object sender, EventArgs e){
Upload();
///添加你的代码
if(Click!=null)
Click(sender,e); ///抛处事件
}
/// <summary>
/// 上传
/// </summary>
private void Upload(){
System.Web.HttpPostedFile postedFile;
for(int i=0;i<filenum;i++){
try{
postedFile=file[i].PostedFile;
if(postedFile!=null) {
if(postedFile.ContentLength>size && size!=0){
break;
}
string suf=GetSuffix(postedFile.FileName);
if(filter!=null && filter.IndexOf(suf)>-1){
break;
}
filename[i]=DateTime.Now.Ticks.ToString();
suffix[i]=suf;
postedFile.SaveAs(System.IO.Path.Combine(path,filename[i]+suf));
}
}finally{
filename[i]="";
}
}
}
/// <summary>
/// 获取后缀名
/// </summary>
/// <param name="filename">文件名</param>
/// <returns>返回带.的后缀名</returns>
private string GetSuffix(string filename){
int index=filename.LastIndexOf(".");
if(index>0){
return filename.Substring(index);
}
return "";
}
}
}
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Drawing;
using System.Drawing.Design;
namespace Yingnet.Common
{
/// <summary>
/// FileUpload 的摘要说明。E:programCommonFileUpload.bmp
/// </summary>
[ToolboxBitmap(typeof(Yingnet.Common.FileUpload), "FileUpload.bmp"),
DefaultProperty("Text"), DefaultEvent("Click"),
ToolboxData("<{0}:FileUpload runat=server></{0}:FileUpload>")]
public class FileUpload : System.Web.UI.WebControls.WebControl {
/// <summary>
/// 上传按钮
/// </summary>
private Button button=new Button();
/// <summary>
/// 上传文件个数
/// </summary>
private int filenum=1;
/// <summary>
/// File对象
/// </summary>
private HtmlInputFile[] file;
/// <summary>
/// 保存路径,默认为系统的临时目录
/// </summary>
private string path=System.IO.Path.GetTempPath();
/// <summary>
/// 上传的文件名组
/// </summary>
private string[] filename;
/// <summary>
/// 后缀文件名组
/// </summary>
private string[] suffix;
/// <summary>
///过滤器,写法是.txt;.abc
/// </summary>
private string filter="";
/// <summary>
/// 限制文件上传大小,为0是不限制,单位是字节
/// </summary>
private int size=0;//System.ComponentModel.DefaultEventAttribute
/// <summary>
/// 上传事件
/// </summary>
[Bindable(true),Category("事件"),Description("上传后激发的事件")
]
public event EventHandler Click;
/// <summary>
/// 上传文件数
/// </summary>
[Bindable(true),
Category("参数"),Description("设定上传文件的个数"),
DefaultValue("1")]
public int FileNum{
set{
if(value<1){
value=1;
}
filenum=value;
this.Controls.Clear();
file=new HtmlInputFile[filenum];
filename=new string[filenum];
suffix=new string[filenum];
for(int i=0;i<filenum;i++) {
file[i]=new HtmlInputFile();
this.Controls.Add(file[i]);
}
this.Controls.Add(button);
}
get{
return filenum;
}
}
/// <summary>
/// 上传按钮的文本
/// </summary>
[Bindable(true),
Category("参数"), Description("设定上传文件的路径"),
DefaultValue("1")]
/// <summary>
/// 上传路径
/// </summary>
public string UploadPath {
set{
if("".Equals(value)||value==null){
value=System.IO.Path.GetTempPath();
}
path=value;
}
get{
return path;
}
}
/// <summary>
/// 得到文件名
/// </summary>
public string[] Filename{
get{
return filename;
}
}
/// <summary>
/// 得到后缀
/// </summary>
public string[] Suffix{
get{
return suffix;
}
}
/// <summary>
/// 过滤器
/// </summary>
public string Filter{
set{
filter=value;
}
get{
return filter;
}
}
/// <summary>
/// 限制大小
/// </summary>
public int FileSize{
set{
size=value;
}
get{
return size;
}
}
/// <summary>
/// 快捷键
/// </summary>
public override string AccessKey{
get{
return button.AccessKey;
}
set{
button.AccessKey=value;
}
}
/// <summary>
/// 背景
/// </summary>
public override System.Drawing.Color BackColor{
get{
return button.BackColor;
}
set{
button.BackColor=value;
}
}
/// <summary>
/// 边框颜色
/// </summary>
public override System.Drawing.Color BorderColor{
get{
return button.BorderColor;
}
set{
button.BorderColor=value;
}
}
/// <summary>
/// 边框风格
/// </summary>
public override BorderStyle BorderStyle{
get{
return button.BorderStyle;
}
set{
button.BorderStyle=value;
}
}
/// <summary>
/// 文本
/// </summary>
[Bindable(true),
Category("Appearance"),
DefaultValue("")]
public string Text
{
get
{
return button.Text;
}
set
{
button.Text = value;
}
}
public FileUpload():base(){
FileNum=1;
button.Click+=new EventHandler(this.button_Click);
this.Controls.Add(button);
}
/// <summary>
/// 按钮事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button_Click(object sender, EventArgs e){
Upload();
///添加你的代码
if(Click!=null)
Click(sender,e); ///抛处事件
}
/// <summary>
/// 上传
/// </summary>
private void Upload(){
System.Web.HttpPostedFile postedFile;
for(int i=0;i<filenum;i++){
try{
postedFile=file[i].PostedFile;
if(postedFile!=null) {
if(postedFile.ContentLength>size && size!=0){
break;
}
string suf=GetSuffix(postedFile.FileName);
if(filter!=null && filter.IndexOf(suf)>-1){
break;
}
filename[i]=DateTime.Now.Ticks.ToString();
suffix[i]=suf;
postedFile.SaveAs(System.IO.Path.Combine(path,filename[i]+suf));
}
}finally{
filename[i]="";
}
}
}
/// <summary>
/// 获取后缀名
/// </summary>
/// <param name="filename">文件名</param>
/// <returns>返回带.的后缀名</returns>
private string GetSuffix(string filename){
int index=filename.LastIndexOf(".");
if(index>0){
return filename.Substring(index);
}
return "";
}
}
}