//文件名称:WmjWebControls.cs
using System.Drawing;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System;
namespace Wmj
{
public class FileUpLoad : Panel
{
private HtmlInputFile htmlInputFile;
private Button button;
private Label label;
public FileUpLoad() : base()
{
htmlInputFile=new HtmlInputFile();
button=new Button();
button.Text="上传";
button.Click+=new EventHandler(this.Button_Click);
label=new Label();
label.Text="<font size=2>请选择上传文件的路径</font>";
this.Controls.Add(htmlInputFile);
this.Controls.Add(button);
this.Controls.Add(label);
this.Width=450;
this.Height=30;
this.BorderStyle=BorderStyle.Dotted;
this.BorderWidth=1;
}
private void Button_Click(object sender, EventArgs e)
{
System.Web.HttpPostedFile postedFile=htmlInputFile.PostedFile;
if(postedFile!=null)
{
try{
string fileName=PathToName(postedFile.FileName);
if(!fileName.EndsWith(Extension))
{label.Text="You must select "+Extension+" file!"; return;}
if(postedFile.ContentLength>int.Parse(FileLength))
{label.Text="File too big!";return;}
postedFile.SaveAs(SavePath+fileName);
label.Text="Upload File Successfully!";
return;
}catch(System.Exception exc){label.Text=exc.Message;return;}
}
label.Text="Please select a file to upload!";
return;
}
private string savePath="";
private string extension="";
private string fileLength="0";
//上传的文件保存在服务器上的位置默认为c:/ 这些属性一般都是在asp.net的标记中设置也可以在codebehind中设置
public string SavePath
{
get
{
if(savePath!="") return savePath;
return "c://";
}
set
{
savePath=value;
}
}
//上传文件的最大长度 单位k 默认为1k
public string FileLength
{
get
{
if(fileLength!="0") return fileLength;
return "1024";
}
set
{
fileLength=(int.Parse(value)*1024).ToString();
}
}
//上传文件的扩展名 默认为txt
public string Extension
{
get
{
if(extension!="") return extension;
return "txt";
}
set
{
extension=value;
}
}
public string PathToName(string path)
{
int pos=path.LastIndexOf("//");
return path.Substring(pos+1);
}
}
}
using System.Drawing;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System;
namespace Wmj
{
public class FileUpLoad : Panel
{
private HtmlInputFile htmlInputFile;
private Button button;
private Label label;
public FileUpLoad() : base()
{
htmlInputFile=new HtmlInputFile();
button=new Button();
button.Text="上传";
button.Click+=new EventHandler(this.Button_Click);
label=new Label();
label.Text="<font size=2>请选择上传文件的路径</font>";
this.Controls.Add(htmlInputFile);
this.Controls.Add(button);
this.Controls.Add(label);
this.Width=450;
this.Height=30;
this.BorderStyle=BorderStyle.Dotted;
this.BorderWidth=1;
}
private void Button_Click(object sender, EventArgs e)
{
System.Web.HttpPostedFile postedFile=htmlInputFile.PostedFile;
if(postedFile!=null)
{
try{
string fileName=PathToName(postedFile.FileName);
if(!fileName.EndsWith(Extension))
{label.Text="You must select "+Extension+" file!"; return;}
if(postedFile.ContentLength>int.Parse(FileLength))
{label.Text="File too big!";return;}
postedFile.SaveAs(SavePath+fileName);
label.Text="Upload File Successfully!";
return;
}catch(System.Exception exc){label.Text=exc.Message;return;}
}
label.Text="Please select a file to upload!";
return;
}
private string savePath="";
private string extension="";
private string fileLength="0";
//上传的文件保存在服务器上的位置默认为c:/ 这些属性一般都是在asp.net的标记中设置也可以在codebehind中设置
public string SavePath
{
get
{
if(savePath!="") return savePath;
return "c://";
}
set
{
savePath=value;
}
}
//上传文件的最大长度 单位k 默认为1k
public string FileLength
{
get
{
if(fileLength!="0") return fileLength;
return "1024";
}
set
{
fileLength=(int.Parse(value)*1024).ToString();
}
}
//上传文件的扩展名 默认为txt
public string Extension
{
get
{
if(extension!="") return extension;
return "txt";
}
set
{
extension=value;
}
}
public string PathToName(string path)
{
int pos=path.LastIndexOf("//");
return path.Substring(pos+1);
}
}
}