当前位置: 移动技术网 > IT编程>开发语言>.net > asp.net fileupload控件上传文件与多文件上传

asp.net fileupload控件上传文件与多文件上传

2017年12月12日  | 移动技术网IT编程  | 我要评论

爆米花乐队,联合早报中国政情,免费电子图书下载

1、前台文件 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>asp.net fileupload控件上传文件_www.jb51.net</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>

2、后端代码 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");
}
 }
}
 

去掉绿色部分就可上传任何文件,它是用一个正则表达式来验证上传文件的类型

在asp.net 2.0中使用fileupload服务器控件很容易的就能将文件上传到服务器。

1、aspx文件代码

<%@ page language="c#" autoeventwireup="true" codefile="fileupload.aspx.cs" inherits="fileupload" %>
<!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>fileupload上传文件示例-jb51.net</title>
</head>
<body>
 <form id="form1" runat="server">
 <div>
<asp:fileupload id="fileupload1" runat="server" />
<asp:button id="button1" runat="server" _disibledevent="button1_click" text="上传文件" /><br />
<asp:label id="label1" runat="server" height="269px" text="label" width="360px"></asp:label></div>
 </form>
</body>
</html>

2、后端代码 aspx.cs:

protected void button1_click(object sender, eventargs e)
{
 if (fileupload1.hasfile)
 {
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 = "没有选择要上传的文件!";
 }
}

1、asp.net fileupload多文件上传的例子

使用fileupload实现多文件上传,可以像传单个文件那样对每个文件单独进行处理,除此之外,还可以使用httpfilecollection类捕获从request对象发送来的所有文件,然后再单独对每个文件进行处理。

后端代码 aspx.cs:

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获取了文件的扩展名,只要稍加判断即可实现上传类型验证:
aspx.cs:

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中已不存在了:

aspx.cs:

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 = "没有选择要上传的文件!";
 }
}

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网