asp.net有两个控件,允许用户将文件上传到web服务器。当服务器收到用户提交的文件数据,应用程序就可以保存,检查或忽略它。 以下控件允许上传文件:
两个控件都允许文件上传,但是fileupload
控件会自动设置表单的编码,而htmlinputfile
则不会。
在本教程中,我们演示如何使用fileupload
控件。 fileupload
控件允许用户浏览并选择要上传的文件,提供浏览按钮和用于输入文件名的文本框。
当用户通过输入名称或浏览在文本框中输入文件名,就可以调用fileupload
控件的saveas
方法来将文件保存到磁盘。
fileupload
的基本语法是:
<asp:fileupload id= "uploader" runat = "server" />
fileupload
类从webcontrol
类派生,并继承其所有成员。除此之外,fileupload
类具有以下只读属性:
编号 | 属性 | 描述 |
---|---|---|
1 | filebytes |
返回要上传的文件中的字节数组。 |
2 | filecontent |
返回指向要上传的文件的流对象。 |
3 | filename |
返回要上传的文件的名称。 |
4 | hasfile |
指定控件是否有要上传的文件。 |
5 | postedfile |
返回对上传文件的引用。 |
要上传的文件封装在httppostedfile
类型的对象中,可以通过fileupload
类的postedfile
属性访问。
httppostedfile
类具有以下常用属性:
编号 | 属性 | 描述 |
---|---|---|
1 | contentlength |
以字节为单位返回上传文件的大小。 |
2 | contenttype |
返回上传文件的mime类型。 |
3 | filename |
返回完整的文件名。 |
4 | inputstream |
返回指向上传文件的流对象。 |
以下示例演示了fileupload
控件及其属性。窗体有一个fileupload
控件以及一个保存按钮和一个用于显示文件名,文件类型和文件长度的标签控件。
打开visual studio ,创建一个空的网站项目:fileuploading ,如下 -
然后,在项目名称上点击右键选择:添加->添加新项 ,填写文件名称为:default.aspx , 如下图所示 -
在设计视图中,窗体如下所示:
default.aspx 文件中的代码如下:
<%@ page language="c#" autoeventwireup="true" codefile="default.aspx.cs" inherits="_default" %>
<!doctype html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<title>文件上传示例</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h3> 文件上传:</h3>
<br />
<asp:fileupload id="fileupload1" runat="server" />
<br />
<asp:button id="btnsave" runat="server" onclick="btnsave_click" text="上传保存" style="width:85px" />
<br /><br />
<asp:label id="lblmessage" runat="server" />
</div>
</form>
</body>
</html>
保存按钮后端的代码(default.aspx.cs )如下所示:
using system;
using system.collections.generic;
using system.linq;
using system.text;
using system.web;
using system.web.ui;
using system.web.ui.webcontrols;
public partial class _default : system.web.ui.page
{
protected void page_load(object sender, eventargs e)
{
}
protected void btnsave_click(object sender, eventargs e)
{
stringbuilder sb = new stringbuilder("empty file");
string fn = system.io.path.getfilename(fileupload1.postedfile.filename);
string savelocation = server.mappath("upload") + "\\" + fn;
lblmessage.text = savelocation;
if ((fileupload1.postedfile != null) && (fileupload1.postedfile.contentlength > 0))
{
try
{
sb.appendformat(" 正在上传文件: {0}", fileupload1.filename);
//saving the file
//fileupload1.saveas("f:\\worksp\\asp.net\\fileuploading\\" + fileupload1.filename);
fileupload1.postedfile.saveas(savelocation);
//showing the file information
sb.appendformat("<br/> 保存为: {0}", fileupload1.postedfile.filename);
sb.appendformat("<br/> 文件类型: {0}", fileupload1.postedfile.contenttype);
sb.appendformat("<br/> 文件长度: {0}", fileupload1.postedfile.contentlength);
sb.appendformat("<br/> 文件名称: {0}", fileupload1.postedfile.filename);
}catch (exception ex)
{
sb.append("<br/> 错误 <br/>");
sb.appendformat("unable to save file <br/> {0}", ex.message);
}
}
lblmessage.text = sb.tostring();
}
}
请注意以下几点:
stringbuilder
类是从system.io
命名空间派生的,所以需要包含它。try
和catch
块用于捕获错误,并显示错误消息。执行上面项目,得到以下结果 -
选择一个要上传的文件,然后提交上传 -