选择显示字体大小

在vs.net下创建文件上载控件

前言:
还记得在asp3.0里,我们为了上载文件可真是煞费苦心,写了一大堆的代码,可执行起来还是那么慢。但在asp.net里这个问题可以轻松搞定,这篇文章我们就探讨如何建立一个用户自定义的文件上载控件,并在我们的.aspx程序中使用它。
正文
第一步:开发自定义文件上载控件
打开vs.net,建立一个工程:webapp,我们使用webapp项目来做我们的工作。在项目webapp上点右健选择add下的add web user control…,这时我们就可以建立一个用户自定义控件():fileup.ascx,注意这个文件的扩展名是:.ascx。添加过程如下图所示:
图:添加用户自定义控件

图:添加用户自定义控件
我们建立fileupload.ascx文件后,就可以象布置.html页面一样来设置布局。我们这个项目是要建立一个用户自定义的文件上载控件,在一个上载控件中有三个必备的元素,从某种意义上讲也可以说是“对象”:取得将要上载文件的htmlinputfile控件、保存文件名的textbox控件、按钮button控件。我们可以使用vs.net的工具箱里的file field来直接添加它(看,vs.net充分考虑了我们的需求),并把它的runat属性设为server,来告诉程序“我要在服务器上运行它”。为了体会asp.net为我们带来的优势,我们使用服务器web控件:textbox和button。控件的布局如下:


图:控件布局
界面设计完成以后,我们需要进一步设置各个控件的属性,主要有控件的id,text等,这里需要强调的关键有两点:一是htmlinputfile控件的runat值:server;另外一个是form表单的enctype属性:multipart/form-data,以支持多部分mime数据上载。fileupload.ascx文件的html代码如下:
fileup.ascx
<%@ control language="c#" autoeventwireup="false" codebehind="fileup.ascx.cs" inherits="webapp.fileup"%>

<html>
<head>
</head>
<body>
<!-- add html content and server controls. do not add server
<form>
tags. -->
<form enctype="multipart/form-data" runat=server method=post id=form1>
<table cellspacing=1 cellpadding=1 width=400 border=0 height=151>
<tr>
selecte file to upload:
<input type=file id=filename runat="server" name="filename"/>
</td>
</tr>
<tr>
<td style="height: 27px">
save the name as:<asp:textbox id=txtsavename runat="server" height="24px" width="130px"></asp:textbox></td>
</tr>
<tr>
<td valign=center align=right>
<asp:button id=btnuplod runat="server" text="send file" height="24px" width="93px">
</asp:button>
</td>
</tr>
<tr>
<td valign=top>
<asp:label id=lblstatusc runat="server" height="33px" width="383px">
</asp:label>
</td>
</tr>
</table>
</form>
</body>
</html>
接下来,我们进行文件上载的处理工作。在.ascx页面上我们双击button按钮,或者右键文件名fileupload.ascx选择view code,就可进入.ascs.cs文件,进行我们的编程工作。
asp.net为我们封装了丰富的编程接口,减少了编程的工作量。并且,我们不需要知道这些接口内部的工作原理,我们只要知道一个类的属性、方法等的用法就能进行快速的开发。
asp.net为我们提供了一个system.web名字空间,system.web名字空间提供了基于browser/server系统的类和接口。我们的文件上载控件就要使用其中的httppostedfile类,所以我们首先了解httppostedfile类的一些相关的属性和方法。
属性:
contentlength 取得将要上载文件的字节数,也就是文件的大小
contenttype 客户端文件的mime类型
filename 上载文件的文件名
inputstream 建立一个stream对象,指向将要读取文件的内容
方法:
gettype 取得当前实例的文件类型
saveas 把mime消息体作为文件保存在服务器
tostring 返回当前对象的表现
熟悉以上的属性和方法后,我们就开始开发我们的文件上载控件。为了便于读者理解,我们首先看代码,完整代码如下:
fileup.ascx.cs:
namespace webapp
{
using system;
using system.io;
using system.data;
using system.drawing;
using system.web;
using system.web.ui.webcontrols;
using system.web.ui.htmlcontrols;

/// <summary>
/// summary description for fileup.
/// </summary>
public class fileup : system.web.ui.usercontrol
{
protected system.web.ui.webcontrols.button btnuplod;
protected system.web.ui.webcontrols.label lblstatusc;
protected system.web.ui.htmlcontrols.htmlinputfile filename;
protected system.web.ui.webcontrols.textbox txtsavename;
protected string uploadfolder = "c:\\temp\\";

/// <summary>
///
/// </summary>
public fileup()
{
this.init += new system.eventhandler(page_init);
}

private void page_load(object sender, system.eventargs e)
{
// put user code to initialize the page here
}

private void page_init(object sender, eventargs e)
{
//
// codegen: this call is required by the asp.net web form designer.
//
initializecomponent();
}

#region web form designer generated code
/// required method for designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void initializecomponent()
{
this.btnuplod.click += new system.eventhandler(this.btnuplod_click);
this.load += new system.eventhandler(this.page_load);

}
#endregion

private void btnuplod_click(object sender, system.eventargs e)
{
if (txtsavename.text.tostring() =="")
{
lblstatusc.text = "没有选择另存为的文件名称";
return;
}

if (filename.postedfile != null)
{

string strfileinfo = "file name: "+
filename.postedfile.filename +
"file type: "+
filename.postedfile.contenttype +
"file length:"+
filename.postedfile.contentlength ;
try
{
filename.postedfile.saveas("uploadfolder"+txtsavename.text.tostring());
lblstatusc.text = "file uploaded successfully:"+strfileinfo;
}
catch(exception ee)
{
lblstatusc.text = "file uploaded error:"+ee.tostring();
}
}
}

}
}
让我们来逐行分析程序。
程序开始是一个名字空间的声明:namespace webapp 这是系统根据项目自动生成的,我们可以手动更改它,或者删除它,但作者不建议删除名字空间,使用名字空间是一个良好的编程模式,便于以后的扩展工作。
using system;
using system.io;
using system.data;
using system.drawing;
using system.web;
using system.web.ui.webcontrols;
using system.web.ui.htmlcontrols;
上面的代码为程序引入了我们需要的类,当然如果不怕以后麻烦也可以不首先引用,而在使用每个类时都写入名字空间。比如我们要使用刚才介绍的httppostedfile 类的postedfile.saveas方法,我们就要这样写了:system.web.ui.htmlcontrols.postedfile.saveas(),是不是很烦?
fileup : system.web.ui.usercontrol
说明fileup类继承了system.web.ui.usercontrol类。
protected system.web.ui.webcontrols.button btnuplod;
protected system.web.ui.webcontrols.label lblstatusc;
protected system.web.ui.htmlcontrols.htmlinputfile filename;
protected system.web.ui.webcontrols.textbox txtsavename;
protected string uploadfolder = "c:\\temp\\";
上面的代码定义了btnuplod、lblstatusc等几个实例。
下面我们着重分析btnuplod_click事件,当用户点击“send file”按钮时程序调用该事件。
if (txtsavename.text.tostring() =="")
{
lblstatusc.text = "没有选择另存为的文件名称";
return;
}
这里检验用户是否输入了将要保存的文件名,如果没有则返回。
if (filename.postedfile != null)
{

string strfileinfo = "file name: "+
filename.postedfile.filename +
"file type: "+
filename.postedfile.contenttype +
"file length:"+
filename.postedfile.contentlength ;
try
{
filename.postedfile.saveas("uploadfolder"+txtsavename.text.tostring());
lblstatusc.text = "file uploaded successfully:"+strfileinfo;
}
catch(exception ee)
{
lblstatusc.text = "file uploaded error:"+ee.tostring();
}
}
这段代码在用户已选择了文件后才能执行,strfileinfo保存了文件的相关信息,读者可以看看httppostedfile类相关属性的使用。使用try{…}catch{…}监测程序,并输出错误信息,使用saveas方法将文件保存到服务器
到现在为止,我们已成功的建立了一个文件上载控件。那么在别的.aspx程序中使用它呢?
使用自定义文件上载控件
使用任何的自定义控件我们都需要使用 register 指令,相关用法这里就不做详细的介绍了,读者可以参考sdk熟悉它的用法。我们先看代码:
controltest.aspx:
<%@ page language="c#" codebehind="controltest.aspx.cs" autoeventwireup="false" inherits="webapp.controltest" %>
<%@ register tagprefix="test" tagname="fileupload" src="fileup.ascx" %>

<html>
<head>
<meta content="microsoft visual studio 7.0" name=generator>
<meta content=c# name=code_language>
<meta content=jscript name=vs_defaultclientscript>
<meta content="inte.net explorer 5.0" name=vs_targetschema>
</head>
<body ms_positioning="gridlayout">
<test:con runat="server" id=con1>
</test:con>
</body>
</html>
你看:
<test:con runat="server" id=fielupload>
</test:con>
就这么简单!需要提示的是在<test:con runat="server" id=fielupload></test:con>外面不能再有<form>标签了,否则不能编译成功。
好了,让我们看一下我们的执行结果吧!


图:执行结果
注:该程序在win2000+sdk(2728)环境下测试通过

  


 


关键字 本文所属关键字

相关 与本文相关文章

分类 所有文章关键字导航

源码编程相关

Java   Asp   PHP   .Net   XML   C/C++   CGI   VB   Jsp   J2ee   J2se   J2me   EJB   Servlet   Tomcat   Resin   Struts   Weblogic   Eclipse   ANT   GUI   JMS   Web servise   IDEA   Webphere   Hibernate   Spring   Jboss   Applet   Swing   Socket   Javamail   Perl   Ajax   P2P   安全   模式   框架   测试   开源   游戏

SQL数据库相关

My-SQL   Ms-SQL   Access   DB2   Oracle   Sybase   SQLserver   索引   存储过程   加密   数据库   分页   视图  

手机无线相关

3G   Wap   CDMA   GRPS   GSM   IVR   彩信   短信   无线   增值业务

网页设计制作相关

HTML   CSS   网页配色   网页特效   Javascript   VBscript   Dreamweaver   Frontpage   JS   Web   网站设计

网站建设推广相关

建站经验   网站优化   网站排名   推广   Alexa

操作系统/服务器相关

Windows XP   Windows 2000   Windows 2003   Windows Me   Windows 9.x   Linux   UNIX   注册表   操作系统   服务器   应用服务器

图形图像多媒体相关

Photoshop   Fireworks   Flash   Coreldraw   Illustrator   Freehand   Photoimpact   多媒体   图形图像

标准 网站致力的规范

Valid CSS!

无不良内容,无不良广告,无恶意代码

Valid XHTML 1.0 Transitional

creativecommons