ASP.NET中FileUpload文件上传控件应用实例
使用 fileupload 控件,可以为用户提供一种将文件从用户的计算机发送到服务器的方法。该控件在允许用户上载图片、文本文件或其他文件时很有用。要上载的文件将在回发期间作为浏览器请求的一部分提交给服务器。在文件上载完毕后,您可以用代码管理该文件。
大致了解了一下fileupload,让我们来看一下fileupload几个实际应用中问题的处理方法。
1.一次上传多个文件
要一次上传多个文件,我们可以像传单个文件那样对每个文件单独进行处理,除此之外,我们还可以使用httpfilecollection类捕获从request对象发送来的所有文件,然后再单独对每个文件进行处理,代码如下:
protected void button1_click(object sender, eventargs e)
{
string filepath = server.mappath("upload") + "\\";
httpfilecollection uploadfiles = request.files;
for (int i = 0; i < uploadfiles.count; i++)
{
httppostedfile postedfile = uploadfiles[i];
try
{
if (postedfile.contentlength > 0)
{
label1.text += "文件 #" + (i + 1) + ":" + system.io.path.getfilename(postedfile.filename) + "<br/>";
postedfile.saveas(filepath + system.io.path.getfilename(postedfile.filename));
}
}
catch (exception ex)
{
label1.text += "发生错误: " + ex.message;
}
}
}
2.上传文件类型的验证
对上传文件类型的验证既可以在客户端进行,也可以在服务器端进行。客户端可以使用验证控件来进行,不过我们今天主要说说如何在服务器端进行验证。上边cs文件中已经用getextension获取了文件的扩展名,只要稍加判断即可实现上传类型的验证:
protected void button1_click(object sender, eventargs e)
{
if (fileupload1.hasfile)
{
fileext = system.io.path.getextension(fileupload1.filename);
if (fileext == ".rar" || fileext == ".zip")
{
try
{
fileupload1.saveas(server.mappath("upload") + "\\" + fileupload1.filename);
label1.text = "客户端路径:" + fileupload1.postedfile.filename + "<br>" +
"文件名:" + system.io.path.getfilename(fileupload1.filename) + "<br>" +
"文件扩展名:" + system.io.path.getextension(fileupload1.filename) + "<br>" +
"文件大小:" + fileupload1.postedfile.contentlength + " kb<br>" +
"文件mime类型:" + fileupload1.postedfile.contenttype + "<br>" +
"保存路径:" + server.mappath("upload") + "\\" + fileupload1.filename;
}
catch (exception ex)
{
label1.text = "发生错误:" + ex.message.tostring();
}
}
else
{
label1.text = "只允许上传rar、zip文件!";
}
}
else
{
label1.text = "没有选择要上传的文件!";
}
}
需要注意的是,我们不能过分依赖于客户端验证控件和服务器端上述方法的验证,因为用户只需将文件扩展名更改为允许的类型就可以避开上边的验证,这对用户来说并不是件困难的事情。
3.解决文件大小限制
在asp.net 2.0中fileupload默认上传文件最大为4m,不过我们可以在web.cofig中修改相关节点来更改这个默认值,相关节点如下:
<system.web>
<httpruntime maxrequestlength="40690" executiontimeout="6000" />
</system.web>
maxrequestlength表示可上传文件的最大值,executiontimeout表示asp.net关闭前允许发生的上载秒数。
4."multipart/form-data"和request共存
在asp程序中一旦使用表单上传文件(form的enctype属性值为multipart/form-data),服务器端就不能再用request.form来获取表单的值,这种限制在asp.net 2.0中已经不存在了:
protected void button1_click(object sender, eventargs e)
{
if (fileupload1.hasfile)
{
try
{
fileupload1.saveas(server.mappath("upload") + "\\" + fileupload1.filename);
label1.text = "上传文件:" + fileupload1.filename + "<br>" +
"说明:" + request.form["textbox1"];//也可以用"textbox1.text"来获取说明
}
catch (exception ex)
{
label1.text = "发生错误:" + ex.message.tostring();
}
}
else
{
label1.text = "没有选择要上传的文件!";
}
}
应用范例
default.aspx:
<%@ page language="c#" autoeventwireup="true" codefile="default.aspx.cs" inherits="_default" %> <!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>无标题页</title> </head> <body> <form id="form1" runat="server"> <asp:fileupload id="fileupload1" runat="server" /> <asp:button id="button1" runat="server" onclick="button1_click" text="button" /> <asp:regularexpressionvalidator id="regularexpressionvalidator1" runat="server" controltovalidate="fileupload1" errormessage="必须是 jpg或者gif文件" validationexpression="^(([a-za-z]:)|(\\{2}\w+)\$?)(\\(\w[\w].*))+(.jpg|.jpg|.gif|.gif)$"></asp:regularexpressionvalidator> </form> </body> </html>
default.aspx.cs:
using system; using system.data; using system.configuration; using system.web; using system.web.security; using system.web.ui; using system.web.ui.webcontrols; using system.web.ui.webcontrols.webparts; using system.web.ui.htmlcontrols; public partial class _default : system.web.ui.page { protected void page_load(object sender, eventargs e) { } protected void button1_click(object sender, eventargs e) { string savepath = @"f:\111\"; if (fileupload1.hasfile) { string filename; filename = fileupload1.filename; savepath +=filename; fileupload1.saveas(savepath); page.response.write(fileupload1.postedfile.contenttype + fileupload1.postedfile.contentlength+"<br>"); page.response.write("<img src='"+savepath+"'>"); } else { page.response.write("fff"); } } }
该范例应用regularexpressionvalidator控件限制只能上传jpg、jpg、gif、gif格式的文件,当然最好后台也做一下限制,上面已经讲解过具体的操作方法。