在ASP.NET中实现多文件上传的方法
程序员文章站
2023-11-24 18:31:52
在以前的web应用中,上传文件是个很麻烦的事,现在有了.net,文件上传变得轻而易举。下面的这个例子实现了多文件上传功能。可以动态添加输入表单,上传的文件数量没有限制。代码...
在以前的web应用中,上传文件是个很麻烦的事,现在有了.net,文件上传变得轻而易举。下面的这个例子实现了多文件上传功能。可以动态添加输入表单,上传的文件数量没有限制。代码如下:
multiupload.aspx
<%@ page language="vb" autoeventwireup="false" codebehind="multiupload.aspx.vb"
inherits="aspxweb.multiupload" %>
<!doctype html public "-//w3c//dtd html 4.0 transitional//en" >
<html>
<head>
<title>多文件上传</title>
<script language="javascript">
function addfile()
{
var str = '<input type="file" size="50" name="file">'
document.getelementbyid('myfile').insertadjacenthtml("beforeend",str)
}
</script>
</head>
<body>
<form id="form1" method="post" runat="server" enctype="multipart/form-data">
<center>
<asp:label runat="server" id="mytitle"></asp:label>
<p id="myfile"><input type="file" size="50" name="file"></p>
<p>
<input type="button" value="增加(add)" onclick="addfile()">
<asp:button runat="server" text="上传" id="upload"></asp:button>
<input onclick="this.form.reset()" type="button" value="重置(reset)">
</p>
</center>
<p align="center">
<asp:label id="strstatus" runat="server" font-names="宋体" font-bold="true"
font-size="9pt" width="500px" borderstyle="none" bordercolor="white"></asp:label>
</p>
</form>
</body>
</html>
后代码:multiupload.aspx.vb
public class multiupload
inherits system.web.ui.page
protected withevents upload as system.web.ui.webcontrols.button
protected withevents mytitle as system.web.ui.webcontrols.label
protected withevents strstatus as system.web.ui.webcontrols.label
#region " web form designer generated code "
'this call is required by the web form designer.
<system.diagnostics.debuggerstepthrough()> private sub initializecomponent()
end sub
private sub page_init(byval sender as system.object, byval e as system.eventargs) handles mybase.init
'codegen: this method call is required by the web form designer
'do not modify it using the code editor.
initializecomponent()
end sub
#end region
private sub page_load(byval sender as system.object, byval e as system.eventargs) handles mybase.load
mytitle.text = "<h3>多文件上传</h3>"
upload.text = "开始上传"
if (me.ispostback) then me.saveimages()
end sub
private function saveimages() as system.boolean
'遍历file表单元素
dim files as system.web.httpfilecollection = system.web.httpcontext.current.request.files
'状态信息
dim strmsg as new system.text.stringbuilder("上传的文件分别是:<hr color=red>")
dim ifile as system.int32
try
for ifile = 0 to files.count - 1
'检查文件扩展名字
dim postedfile as system.web.httppostedfile = files(ifile)
dim filename, fileextension as system.string
filename = system.io.path.getfilename(postedfile.filename)
if not (filename = string.empty) then
fileextension = system.io.path.getextension(filename)
strmsg.append("上传的文件类型:" + postedfile.contenttype.tostring() + "<br>")
strmsg.append("客户端文件地址:" + postedfile.filename + "<br>")
strmsg.append("上传文件的文件名:" + filename + "<br>")
strmsg.append("上传文件的扩展名:" + fileextension + "<br><hr>")
'可根据扩展名字的不同保存到不同的文件夹
postedfile.saveas(system.web.httpcontext.current.request.mappath("images/") + filename)
end if
next
strstatus.text = strmsg.tostring()
return true
catch ex as system.exception
strstatus.text = ex.message
return false
end try
end function
end class
c# 版本
upload.aspx
<%@ page language="c#" codebehind="upload.aspx.cs" autoeventwireup="false" inherits="webportal.upload" %>
<!doctype html public "-//w3c//dtd html 4.0 transitional//en" >
<html>
<head>
<title>多文件上传</title>
<script language="javascript">
function addfile()
{
var str = '<input type="file" size="50" name="file">'
document.getelementbyid('myfile').insertadjacenthtml("beforeend",str)
}
</script>
</head>
<body>
<form id="form1" method="post" runat="server" enctype="multipart/form-data">
<div align="center">
<h3>多文件上传</h3>
<p id="myfile"><input type="file" size="50" name="file"></p>
<p>
<input type="button" value="增加(add)" onclick="addfile()">
<input onclick="this.form.reset()" type="button" value="重置(reset)">
<asp:button runat="server" text="开始上传" id="uploadbutton"></asp:button>
</p>
<p>
<asp:label id="strstatus" runat="server" font-names="宋体" font-bold="true" font-size="9pt"
width="500px" borderstyle="none" bordercolor="white"></asp:label>
</p>
</div>
</form>
</body>
</html>
upload.aspx.cs
using system;
using system.collections;
using system.componentmodel;
using system.data;
using system.drawing;
using system.web;
using system.web.sessionstate;
using system.web.ui;
using system.web.ui.webcontrols;
using system.web.ui.htmlcontrols;
namespace webportal
{
/// <summary>
/// upload 的摘要说明。
/// 实现多文件上传
/// </summary>
public class upload : system.web.ui.page
{
protected system.web.ui.webcontrols.button uploadbutton;
protected system.web.ui.webcontrols.label strstatus;
private void page_load(object sender, system.eventargs e)
{
/// 在此处放置用户代码以初始化页面
if (this.ispostback) this.saveimages();
}
private boolean saveimages()
{
///'遍历file表单元素
httpfilecollection files = httpcontext.current.request.files;
/// '状态信息
system.text.stringbuilder strmsg = new system.text.stringbuilder();
strmsg.append("上传的文件分别是:<hr color=red>");
try
{
for(int ifile = 0; ifile < files.count; ifile++)
{
///'检查文件扩展名字
httppostedfile postedfile = files[ifile];
string filename, fileextension;
filename = system.io.path.getfilename(postedfile.filename);
if (filename != "")
{
fileextension = system.io.path.getextension(filename);
strmsg.append("上传的文件类型:" + postedfile.contenttype.tostring() + "<br>");
strmsg.append("客户端文件地址:" + postedfile.filename + "<br>");
strmsg.append("上传文件的文件名:" + filename + "<br>");
strmsg.append("上传文件的扩展名:" + fileextension + "<br><hr>");
///'可根据扩展名字的不同保存到不同的文件夹
///注意:可能要修改你的文件夹的匿名写入权限。
postedfile.saveas(system.web.httpcontext.current.request.mappath("images/") + filename);
}
}
strstatus.text = strmsg.tostring();
return true;
}
catch(system.exception ex)
{
strstatus.text = ex.message;
return false;
}
}
#region web 窗体设计器生成的代码
override protected void oninit(eventargs e)
{
//
// codegen: 该调用是 asp.net web 窗体设计器所必需的。
//
initializecomponent();
base.oninit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void initializecomponent()
{
this.id = "upload";
this.load += new system.eventhandler(this.page_load);
}
#endregion
}
}
multiupload.aspx
<%@ page language="vb" autoeventwireup="false" codebehind="multiupload.aspx.vb"
inherits="aspxweb.multiupload" %>
<!doctype html public "-//w3c//dtd html 4.0 transitional//en" >
<html>
<head>
<title>多文件上传</title>
<script language="javascript">
function addfile()
{
var str = '<input type="file" size="50" name="file">'
document.getelementbyid('myfile').insertadjacenthtml("beforeend",str)
}
</script>
</head>
<body>
<form id="form1" method="post" runat="server" enctype="multipart/form-data">
<center>
<asp:label runat="server" id="mytitle"></asp:label>
<p id="myfile"><input type="file" size="50" name="file"></p>
<p>
<input type="button" value="增加(add)" onclick="addfile()">
<asp:button runat="server" text="上传" id="upload"></asp:button>
<input onclick="this.form.reset()" type="button" value="重置(reset)">
</p>
</center>
<p align="center">
<asp:label id="strstatus" runat="server" font-names="宋体" font-bold="true"
font-size="9pt" width="500px" borderstyle="none" bordercolor="white"></asp:label>
</p>
</form>
</body>
</html>
后代码:multiupload.aspx.vb
public class multiupload
inherits system.web.ui.page
protected withevents upload as system.web.ui.webcontrols.button
protected withevents mytitle as system.web.ui.webcontrols.label
protected withevents strstatus as system.web.ui.webcontrols.label
#region " web form designer generated code "
'this call is required by the web form designer.
<system.diagnostics.debuggerstepthrough()> private sub initializecomponent()
end sub
private sub page_init(byval sender as system.object, byval e as system.eventargs) handles mybase.init
'codegen: this method call is required by the web form designer
'do not modify it using the code editor.
initializecomponent()
end sub
#end region
private sub page_load(byval sender as system.object, byval e as system.eventargs) handles mybase.load
mytitle.text = "<h3>多文件上传</h3>"
upload.text = "开始上传"
if (me.ispostback) then me.saveimages()
end sub
private function saveimages() as system.boolean
'遍历file表单元素
dim files as system.web.httpfilecollection = system.web.httpcontext.current.request.files
'状态信息
dim strmsg as new system.text.stringbuilder("上传的文件分别是:<hr color=red>")
dim ifile as system.int32
try
for ifile = 0 to files.count - 1
'检查文件扩展名字
dim postedfile as system.web.httppostedfile = files(ifile)
dim filename, fileextension as system.string
filename = system.io.path.getfilename(postedfile.filename)
if not (filename = string.empty) then
fileextension = system.io.path.getextension(filename)
strmsg.append("上传的文件类型:" + postedfile.contenttype.tostring() + "<br>")
strmsg.append("客户端文件地址:" + postedfile.filename + "<br>")
strmsg.append("上传文件的文件名:" + filename + "<br>")
strmsg.append("上传文件的扩展名:" + fileextension + "<br><hr>")
'可根据扩展名字的不同保存到不同的文件夹
postedfile.saveas(system.web.httpcontext.current.request.mappath("images/") + filename)
end if
next
strstatus.text = strmsg.tostring()
return true
catch ex as system.exception
strstatus.text = ex.message
return false
end try
end function
end class
c# 版本
upload.aspx
<%@ page language="c#" codebehind="upload.aspx.cs" autoeventwireup="false" inherits="webportal.upload" %>
<!doctype html public "-//w3c//dtd html 4.0 transitional//en" >
<html>
<head>
<title>多文件上传</title>
<script language="javascript">
function addfile()
{
var str = '<input type="file" size="50" name="file">'
document.getelementbyid('myfile').insertadjacenthtml("beforeend",str)
}
</script>
</head>
<body>
<form id="form1" method="post" runat="server" enctype="multipart/form-data">
<div align="center">
<h3>多文件上传</h3>
<p id="myfile"><input type="file" size="50" name="file"></p>
<p>
<input type="button" value="增加(add)" onclick="addfile()">
<input onclick="this.form.reset()" type="button" value="重置(reset)">
<asp:button runat="server" text="开始上传" id="uploadbutton"></asp:button>
</p>
<p>
<asp:label id="strstatus" runat="server" font-names="宋体" font-bold="true" font-size="9pt"
width="500px" borderstyle="none" bordercolor="white"></asp:label>
</p>
</div>
</form>
</body>
</html>
upload.aspx.cs
using system;
using system.collections;
using system.componentmodel;
using system.data;
using system.drawing;
using system.web;
using system.web.sessionstate;
using system.web.ui;
using system.web.ui.webcontrols;
using system.web.ui.htmlcontrols;
namespace webportal
{
/// <summary>
/// upload 的摘要说明。
/// 实现多文件上传
/// </summary>
public class upload : system.web.ui.page
{
protected system.web.ui.webcontrols.button uploadbutton;
protected system.web.ui.webcontrols.label strstatus;
private void page_load(object sender, system.eventargs e)
{
/// 在此处放置用户代码以初始化页面
if (this.ispostback) this.saveimages();
}
private boolean saveimages()
{
///'遍历file表单元素
httpfilecollection files = httpcontext.current.request.files;
/// '状态信息
system.text.stringbuilder strmsg = new system.text.stringbuilder();
strmsg.append("上传的文件分别是:<hr color=red>");
try
{
for(int ifile = 0; ifile < files.count; ifile++)
{
///'检查文件扩展名字
httppostedfile postedfile = files[ifile];
string filename, fileextension;
filename = system.io.path.getfilename(postedfile.filename);
if (filename != "")
{
fileextension = system.io.path.getextension(filename);
strmsg.append("上传的文件类型:" + postedfile.contenttype.tostring() + "<br>");
strmsg.append("客户端文件地址:" + postedfile.filename + "<br>");
strmsg.append("上传文件的文件名:" + filename + "<br>");
strmsg.append("上传文件的扩展名:" + fileextension + "<br><hr>");
///'可根据扩展名字的不同保存到不同的文件夹
///注意:可能要修改你的文件夹的匿名写入权限。
postedfile.saveas(system.web.httpcontext.current.request.mappath("images/") + filename);
}
}
strstatus.text = strmsg.tostring();
return true;
}
catch(system.exception ex)
{
strstatus.text = ex.message;
return false;
}
}
#region web 窗体设计器生成的代码
override protected void oninit(eventargs e)
{
//
// codegen: 该调用是 asp.net web 窗体设计器所必需的。
//
initializecomponent();
base.oninit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void initializecomponent()
{
this.id = "upload";
this.load += new system.eventhandler(this.page_load);
}
#endregion
}
}