单一文件上传
下面是实现单一文件上传的完整代码:
1<%@ Import Namespace="System" %>
2<%@ Import Namespace="System.Web.HttpPostedFile" %>
3<%@ Import Namespace="System.Web.UI.HtmlControls.HtmlInputFile" %>
4<script language="VB" runat="server">
5Sub UpLoad(Src As Object, E As EventArgs)
6 If UploadFile.PostedFile.ContentLength=0 then
7 ShowUpLoadFile.innerText="上传失败或文件不存在!"
8 Else
9 '获取文件名
10 dim Temp() as String=Split(UploadFile.PostedFile.FileName,"\")
11 dim FileName as String=Temp(Temp.Length-1)
12 '保存文件
13 UploadFile.PostedFile.SaveAs(Server.MapPath(".") & "\Files\" & FileName)
14 '显示上传结果
15 ShowUpLoadFile.InnerHtml="文件上传成功!<br>上传文件名:" & FileName
16 End If
17End Sub
18</script>
19<html>
20<body>
21 <form runat="server" enctype="multipart/form-data">
22 <input type="file" id="UploadFile" runat="server" size="50"><br>
23 <asp:button runat="server" Text="立即上传" onClick="Upload" />
24 </form>
25 <hr><br>
26 <span id="ShowUpLoadFile" runat="server"></span>
27</body>
28</html>
29
2<%@ Import Namespace="System.Web.HttpPostedFile" %>
3<%@ Import Namespace="System.Web.UI.HtmlControls.HtmlInputFile" %>
4<script language="VB" runat="server">
5Sub UpLoad(Src As Object, E As EventArgs)
6 If UploadFile.PostedFile.ContentLength=0 then
7 ShowUpLoadFile.innerText="上传失败或文件不存在!"
8 Else
9 '获取文件名
10 dim Temp() as String=Split(UploadFile.PostedFile.FileName,"\")
11 dim FileName as String=Temp(Temp.Length-1)
12 '保存文件
13 UploadFile.PostedFile.SaveAs(Server.MapPath(".") & "\Files\" & FileName)
14 '显示上传结果
15 ShowUpLoadFile.InnerHtml="文件上传成功!<br>上传文件名:" & FileName
16 End If
17End Sub
18</script>
19<html>
20<body>
21 <form runat="server" enctype="multipart/form-data">
22 <input type="file" id="UploadFile" runat="server" size="50"><br>
23 <asp:button runat="server" Text="立即上传" onClick="Upload" />
24 </form>
25 <hr><br>
26 <span id="ShowUpLoadFile" runat="server"></span>
27</body>
28</html>
29
把上面的代码保存成.aspx文件,然后在该文件所在目录下创建一个存放文件的新目录Files,运行,先感受一下效果,然后再继续看下面的讲解
使用ASP.NET上传文件,需要用到.NET框架的两个类:HttpPostedFile和HtmlInputFile,这两个类所在的命名空间分别是System.Web.HttpPostedFile和System.Web.UI.HtmlControls.HtmlInputFile,所以我们要在文件开头先导入这两个命名空间,
其中的PostedFile表示上传到服务器的文件,它包含几个常用的属性:
ContentLength:文件大小;
FileName :上传文件的详细路径及文件名;
ContentType :上传文件的文件类型。
字符分割函数Split是用来取得文件名的,因为通过PostedFile.FileName获得的是详细的路径及文件名。
多文件上传
所谓的多文件上传就是同时上传多个文件,这个跟单一文件上传大多是相同的,不同的是多文件上传是把所有文件作为一个文件集合一起上传到服务器的,我们需要的是把这个文件集合分解成一个个单一的文件,剩下的处理方法就跟单一文件上传一样了。
首先你要知道要最多同时上传多少个文件,然后你就在form之间放多少个如下的HtmlInput控件:
<input type="file" runat="server" size="50">
注意:这里的HtmlInput控件控件是不需要设置ID的
那怎么在上传到服务器的文件集合中取出一个个的文件呢?看下面的代码:
dim i as integer
For i=0 to Request.Files.Count-1
‘使用Request.Files()来逐个获取上传的文件
dim myFile as HttpPostedFile=Request.Files(i)
'这里的myFile就相当于上例中的PostedFile,可以用myFile.FileName获得文件名,etc
'这里的处理代码就跟单一文件上传的一样了
Next
For i=0 to Request.Files.Count-1
‘使用Request.Files()来逐个获取上传的文件
dim myFile as HttpPostedFile=Request.Files(i)
'这里的myFile就相当于上例中的PostedFile,可以用myFile.FileName获得文件名,etc
'这里的处理代码就跟单一文件上传的一样了
Next
C#
<%@Page Language="C#"%>
<HTML>
<HEAD>
<TITLE>
Multiple Files Uploading Using C# - Demo
</TITLE>
</HEAD>
<BODY>
<script language="C#" runat="Server">
//Event handler for the upload button
void UploadFile(object Sender,EventArgs E)
{
int IntLoop=0;
//Iterating through the Request.Files collection
for(IntLoop=0;IntLoop<Request.Files.Count;IntLoop++)
{
if (Request.Files[IntLoop] !=null) //Checking for valid file
{
// Since the FileName gives the entire path we use Substring function to
rip of the filename.
string StrFileName
=Request.Files[IntLoop].FileName.Substring(Request.Files[IntLoop].FileName.L
astIndexOf("\\") + 1) ;
string StrFileType = Request.Files[IntLoop].ContentType ;
int IntFileSize =Request.Files[IntLoop].ContentLength;
//Checking for the file length. If length is 0 then file is not
uploaded.
if (IntFileSize <=0)
Response.Write(" <font color='Red' size='2'>Uploading of file " +
StrFileName + " failed. </font><br>");
else
{
//Saving the file to the web server
Request.Files[IntLoop].SaveAs(Server.MapPath(".\\" + StrFileName));
Response.Write( "<font color='green' size='2'>Your file " + StrFileName
+ " of type " + StrFileType + " and size " + IntFileSize.ToString() + " was
uploaded successfully.</font><br>");
}
}
}
Response.Write("<br>Click <a href='MultipleFileUploadDemo.aspx'>here</a>
to upload more files");
}
</script>
<%if(!Page.IsPostBack)
{
%>
<H2 align="center">Multiple files uploading in ASP.Net using C# - Demo</H2>
<!-- Declaration of server side form.Note the enctype attribute of the form
has to be set to multipart/form-data -->
<basefont size="2">
<form id="FrmFileUploadDemo" name="FrmFileUploadDemo" method="post"
enctype="multipart/form-data" runat="server">
<TABLE align="center" bgcolor="lightyellow" cellspacing="5">
<TR>
<TD>
<font size="2">Select a file to upload</font> <input type="file"
id="File1" name="File1" runat="server">
</TD>
<TR>
<TR>
<TD>
<font size="2">Select a file to upload</font> <input type="file"
id="File2" name="File2" runat="server">
</TD>
<TR>
<TR>
<TD>
<font size="2">Select a file to upload</font> <input type="file"
id="File3" name="File3" runat="server">
</TD>
<TR>
<TR>
<TD align="center">
<asp:button value="Upload" Text="Upload" runat="server" id="CmdUpload"
onClick="UploadFile" />
</TD>
</TR>
</TABLE>
</form>
<%}%>
<BR><BR><BR><BR><BR><BR><BR><BR><BR>
</BODY>
</HTML>
<HTML>
<HEAD>
<TITLE>
Multiple Files Uploading Using C# - Demo
</TITLE>
</HEAD>
<BODY>
<script language="C#" runat="Server">
//Event handler for the upload button
void UploadFile(object Sender,EventArgs E)
{
int IntLoop=0;
//Iterating through the Request.Files collection
for(IntLoop=0;IntLoop<Request.Files.Count;IntLoop++)
{
if (Request.Files[IntLoop] !=null) //Checking for valid file
{
// Since the FileName gives the entire path we use Substring function to
rip of the filename.
string StrFileName
=Request.Files[IntLoop].FileName.Substring(Request.Files[IntLoop].FileName.L
astIndexOf("\\") + 1) ;
string StrFileType = Request.Files[IntLoop].ContentType ;
int IntFileSize =Request.Files[IntLoop].ContentLength;
//Checking for the file length. If length is 0 then file is not
uploaded.
if (IntFileSize <=0)
Response.Write(" <font color='Red' size='2'>Uploading of file " +
StrFileName + " failed. </font><br>");
else
{
//Saving the file to the web server
Request.Files[IntLoop].SaveAs(Server.MapPath(".\\" + StrFileName));
Response.Write( "<font color='green' size='2'>Your file " + StrFileName
+ " of type " + StrFileType + " and size " + IntFileSize.ToString() + " was
uploaded successfully.</font><br>");
}
}
}
Response.Write("<br>Click <a href='MultipleFileUploadDemo.aspx'>here</a>
to upload more files");
}
</script>
<%if(!Page.IsPostBack)
{
%>
<H2 align="center">Multiple files uploading in ASP.Net using C# - Demo</H2>
<!-- Declaration of server side form.Note the enctype attribute of the form
has to be set to multipart/form-data -->
<basefont size="2">
<form id="FrmFileUploadDemo" name="FrmFileUploadDemo" method="post"
enctype="multipart/form-data" runat="server">
<TABLE align="center" bgcolor="lightyellow" cellspacing="5">
<TR>
<TD>
<font size="2">Select a file to upload</font> <input type="file"
id="File1" name="File1" runat="server">
</TD>
<TR>
<TR>
<TD>
<font size="2">Select a file to upload</font> <input type="file"
id="File2" name="File2" runat="server">
</TD>
<TR>
<TR>
<TD>
<font size="2">Select a file to upload</font> <input type="file"
id="File3" name="File3" runat="server">
</TD>
<TR>
<TR>
<TD align="center">
<asp:button value="Upload" Text="Upload" runat="server" id="CmdUpload"
onClick="UploadFile" />
</TD>
</TR>
</TABLE>
</form>
<%}%>
<BR><BR><BR><BR><BR><BR><BR><BR><BR>
</BODY>
</HTML>
转自:http://www.517sou.net/blogview.asp?logID=399