在线等急:怎样用类实现文件上传???
我想用类现在文件上传,但是在类里怎样获得页面控件的信息(如LABEL,TEXTBOX)?请高手指点!!! 问题点数:20、回复次数:10Top
1 楼bluesmile979(笑着)回复于 2005-01-07 14:32:35 得分 0
作为参数传递一下Top
2 楼binbin1845(方伍)回复于 2005-01-07 15:11:10 得分 0
还是得不到LABEL里的值,因为在不同的页面,可不可以说具体点Top
3 楼binbin1845(方伍)回复于 2005-01-08 09:35:41 得分 0
请问:可不可以用LABEL做为参数进行传递啊?Top
4 楼lei00529(老婆叫我猪)回复于 2005-01-09 14:11:04 得分 0
http://www.9466.com/web/html/2004/3201.htmTop
5 楼minghui000(沉迷网络游戏)回复于 2005-01-09 14:15:18 得分 0
关注Top
6 楼CSharpProgrammer(风儿吹过)回复于 2005-01-10 00:01:29 得分 0
不能,你必须用 file控件,而且必须获取file对象.只获取客户端的文件路径是不能上传的Top
7 楼CSharpProgrammer(风儿吹过)回复于 2005-01-10 00:04:39 得分 20
上传多个文件控件原代码:
using System;
using System.Web.UI;
using System.ComponentModel;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.Design;
using System.Drawing.Design;
using System.Collections ;
[assembly:System.Web.UI.TagPrefix("Gzee.WebControls","GzeeWebControl")]
namespace Gzee.WebControls
{
/// <summary>
/// GzeeFileUpload 的摘要说明。
/// </summary>
[DefaultProperty("Text"),
ToolboxData("<{0}:GzeeFileUpload runat=server></{0}:GzeeFileUpload>")]
public class GzeeFileUpload : System.Web.UI.WebControls.WebControl,INamingContainer
{
#region 事件
public event System.EventHandler OnUpload ;
#endregion
#region 声明
private string _AddImagePath;
private string _UnloadImagePath;
private string _DelImagePath;
private string _SaveFileDir;
private string _FileUploadCssClass = "" ;
private string _FileListCssClass = "";
private ArrayList unloadFiles ;
private ArrayList _FilesPath ;
private ArrayList _FileNames = new ArrayList() ;
private static Random random = new Random();
private string _AllowFileType = "";
private string _DenyFileType = "";
private FileCollection _files = new FileCollection() ;
#endregion
#region 属性
[Category("图片"),
Bindable(true),
Editor(typeof(ImageUrlEditor),typeof(UITypeEditor))]
public string AddImagePath
{
get{return _AddImagePath;}
set
{
_AddImagePath=value;
ChildControlsCreated = false ;
}
}
[Category("图片"),
Bindable(true),
Editor(typeof(ImageUrlEditor),typeof(UITypeEditor))]
public string UnloadImagePath
{
get{return _UnloadImagePath;}
set
{
_UnloadImagePath=value;
ChildControlsCreated = false ;
}
}
public ArrayList FilesPath
{
get
{
return _FilesPath;
}
set
{
_FilesPath = value;
}
}
public ArrayList FileNames
{
get
{
return _FileNames ;
}
}
[Category("图片"),
Bindable(true),
Editor(typeof(ImageUrlEditor),typeof(UITypeEditor))]
public string DelImagePath
{
get{return _DelImagePath;}
set
{
_DelImagePath=value;
ChildControlsCreated = false ;
}
}
[Category("存储目录"),
Bindable(true),
Editor(typeof(ImageUrlEditor),typeof(UITypeEditor))]
public string SaveFileDir
{
get{return _SaveFileDir;}
set{_SaveFileDir=value;}
}
[Category("控件样式"),
Bindable(true)]
public string FileUploadCssClass
{
get{return _FileUploadCssClass;}
set{_FileUploadCssClass=value;}
}
[Category("控件样式"),
Bindable(true)]
public string FileListCssClass
{
get{return _FileListCssClass;}
set{_FileListCssClass=value;}
}
[Category("上传文件格式"),
Bindable(true),
Description("允许上传的文件格式:\n例如:.jpg|.gif|.bmp")]
public string AllowFileType
{
get{return _AllowFileType;}
set{_AllowFileType=value;}
}
[Category("上传文件格式"),
Bindable(true),
Description("拒绝上传的文件格式:\n例如:.exe|.bat|.com")]
public string DenyFileType
{
get{return _DenyFileType;}
set{_DenyFileType=value;}
}
public FileCollection Files
{
get
{
return _files;
}
}
#endregion
#region 重写的方法
protected override void OnInit(EventArgs e)
{
if (this.Page.Session[GetSessinKey()]!=null)
{
unloadFiles = (ArrayList)this.Page.Session[GetSessinKey()] ;
}
else
{
unloadFiles = new ArrayList();
}
base.OnInit (e);
}
protected override void CreateChildControls()
{
this.Controls.Clear();
//表格
Table table = new Table() ;
table.Width=this.Width ;
table.Height = this.Height ;
//浏览控件行
TableRow topRow = new TableRow();
TableCell topCell = new TableCell();
topRow.Cells.Add(topCell);
table.Rows.Add(topRow);
//文件列表
TableRow centerRow = new TableRow();
TableCell centerCell = new TableCell();
centerRow.Cells.Add(centerCell);
table.Rows.Add(centerRow);
//按扭行
TableRow bottomRow = new TableRow();
TableCell bottomCell = new TableCell();
bottomRow.Cells.Add(bottomCell);
table.Rows.Add(bottomRow);
//上传控件
HtmlInputFile inputFile = new HtmlInputFile();
inputFile.Attributes.Add("class",_FileUploadCssClass);
inputFile.Style.Add("WIDTH","100%");
topCell.Controls.Add(inputFile);
//列表控件
ListBox fileList = new ListBox();
fileList.Width=new Unit(100,UnitType.Percentage);
fileList.EnableViewState = true;
fileList.DataSource = unloadFiles;
fileList.CssClass = _FileListCssClass;
fileList.DataValueField = "Value";
centerCell.Controls.Add(fileList);
//添加按钮
System.Web.UI.WebControls.ImageButton ibtAdd = new ImageButton();
ibtAdd.ImageUrl = _AddImagePath;
ibtAdd.Click +=new ImageClickEventHandler(ibtAdd_Click);
bottomCell.Controls.Add(ibtAdd);
//删除按钮
System.Web.UI.WebControls.ImageButton ibtDel = new ImageButton();
ibtDel.ImageUrl = _DelImagePath;
ibtDel.Click +=new ImageClickEventHandler(ibtDel_Click);
bottomCell.Controls.Add(ibtDel);
Top
8 楼CSharpProgrammer(风儿吹过)回复于 2005-01-10 00:04:46 得分 0
//上传按钮
System.Web.UI.WebControls.ImageButton ibtUnload = new ImageButton();
ibtUnload.ImageUrl = _UnloadImagePath;
ibtUnload.Click +=new ImageClickEventHandler(ibtUnload_Click);
bottomCell.Controls.Add(ibtUnload);
this.Controls.Add(table);
base.CreateChildControls();
}
public override Unit Width
{
get
{
return base.Width;
}
set
{
base.Width = value;
ChildControlsCreated = false ;
}
}
public override Unit Height
{
get
{
return base.Height;
}
set
{
base.Height = value;
ChildControlsCreated = false;
}
}
/// <summary>
/// 将此控件呈现给指定的输出参数。
/// </summary>
/// <param name="output"> 要写出到的 HTML 编写器 </param>
protected override void Render(HtmlTextWriter output)
{
EnsureChildControls();
base.Render(output);
}
#endregion
#region 事件处理程序
//添加上传文件
private void ibtAdd_Click(object sender, ImageClickEventArgs e)
{
HtmlInputFile tfile = (HtmlInputFile)this.Controls[0].Controls[0].Controls[0].Controls[0];
if (tfile.Value.Length<1)
return;
//检测文件类型是否被允许(检测允许的文件类型)
try
{
if (_AllowFileType.Length>0) //如果允许类型属性不为空
{
System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex("("+_AllowFileType+")",System.Text.RegularExpressions.RegexOptions.IgnoreCase);
if (!regex.IsMatch(tfile.Value.Substring(tfile.Value.LastIndexOf("."))))
return;
}
else
{
System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex("("+ _DenyFileType +")",System.Text.RegularExpressions.RegexOptions.IgnoreCase);
if (regex.IsMatch(tfile.Value.Substring(tfile.Value.LastIndexOf("."))))
return;
}
}
catch
{
}
//檢測上传文件是否已在列表中
foreach(HtmlInputFile hif in unloadFiles)
{
if(hif.Value==tfile.Value)
return;
}
unloadFiles.Add(tfile);
Binding();
}
//删除上传文件
private void ibtDel_Click(object sender, ImageClickEventArgs e)
{
ListBox tlist = (ListBox) this.Controls[0].Controls[1].Controls[0].Controls[0] ;
unloadFiles.RemoveAt(tlist.SelectedIndex);
Binding();
}
//上传文件
private void ibtUnload_Click(object sender, ImageClickEventArgs e)
{
UpLoad();
}
public ArrayList UpLoad()
{
ArrayList SendBackFiles = new ArrayList();
foreach(HtmlInputFile hif in unloadFiles)
{
string filepath = BuildPath(hif.Value) ;
_FileNames.Add(System.IO.Path.GetFileName(hif.PostedFile.FileName));
hif.PostedFile.SaveAs(filepath);
string path = _SaveFileDir + GetFileName(filepath);
SendBackFiles.Add(path);
_files.Add(new File(GetFileName(filepath),path));
}
unloadFiles.Clear();
this.DataBind();
_FilesPath = SendBackFiles ;
if (OnUpload!=null)
OnUpload(SendBackFiles,new System.EventArgs());
return SendBackFiles;
}
//獲取SessinID
private string GetSessinKey()
{
string key = this.Page.Request.ServerVariables["PATH_TRANSLATED"] + "_" + this.ID;
return key;
}
//将数据绑定到ListBox
private void Binding()
{
this.Page.Session[GetSessinKey()] = unloadFiles;
this.DataBind();
}
//构造文件名和路径
private string BuildPath(string m_FileName)
{
//string fileExtName = m_FileName.Substring(m_FileName.LastIndexOf("."));
string fileExtName = m_FileName.Substring(m_FileName.LastIndexOf("\\")+1,(m_FileName.Length - m_FileName.LastIndexOf("\\")-1));
switch (fileExtName)
{
case "a":
{
break;
}
}
string filePath = this.Page.Server.MapPath(_SaveFileDir) ;
filePath += "\\";
filePath += DateTime.Now.ToFileTimeUtc();
// string temp = random.NextDouble().ToString();
// filePath += temp.Replace('.','L');
// filePath += DateTime.Now.Millisecond.ToString();
filePath += fileExtName ;
return filePath ;
}
private string GetFileName(string m_FilePath)
{
string ret = m_FilePath.Substring(m_FilePath.LastIndexOf("\\")+1);
return ret;
}
#endregion
}
public class File
{
public File(string m_FileName,string m_FilePath)
{
this._FileName = m_FileName ;
this._FilePath = m_FilePath ;
}
private string _FileName;
private string _FilePath;
public string FileName
{
get
{
return _FileName ;
}
set
{
_FileName = value ;
}
}
public string FilePath
{
get
{
return _FilePath ;
}
set
{
_FilePath = value ;
}
}
}
public class FileCollection : System.Collections.CollectionBase
{
public File this[int index]
{
get
{
return (File) List[index];
}
set
{
List[index] = value;
}
}
public void Add(File m_File)
{
List.Add(m_File);
}
}
}
Top
9 楼eagle40(老鹰)回复于 2005-01-10 00:19:09 得分 0
关注Top
10 楼kentpower(至尊宝玉)回复于 2005-01-10 01:21:14 得分 0
关心中....Top




