浏览器大全:是一个提供流行浏览器教程、在线学习分享的学习平台!

一次一起上传多个文件

下面我将为大家讲解如何同时上传多个文件
我们在页面中添加5个<input type=file>上传的类型只限定在.gif和.jpg两种,并根据不同的类型放入不同的文件夹中。
我先给出upload.aspx源代码
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" > <HTML>
<HEAD>
<title>::: UPLOAD SAMPLE ::: </title>
</HEAD>
<body>
<center>
<form id="UPLOAD" method="post" runat="server" enctype="multipart/form-data">
<h3>Multiple File Upload Example</h3>
<P>
<INPUT type="file" runat="server" size="50" ID="File1" NAME="File1"></P>
<P>
<INPUT type="file" runat="server" size="50" ID="File2" NAME="File2"></P>
<P>
<INPUT type="file" runat="server" size="50" ID="File3" NAME="File3"></P>
<P>
<INPUT type="file" runat="server" size="50" ID="File4" NAME="File4"></P>
<P>
<INPUT type="file" runat="server" size="50" ID="File5" NAME="File5"></P>
<P><STRONG>::</STRONG>
<asp:LinkButton id="LinkButton1" runat="server" Font-Names="Verdana" Font-Bold="True" Font-Size="XX-Small">Upload Images</asp:LinkButton><STRONG>::
</STRONG><A href="javascript:document.forms[0].reset()" id="LinkButton2" style="FONT-WEIGHT:bold;FONT-SIZE:xx-small;FONT-FAMILY:verdana">
Reset Form</A> <STRONG>::</STRONG></P>
<P>
<asp:Label id="Label1" runat="server" Font-Names="verdana" Font-Bold="True" Font-Size="XX-Small" Width="400px" BorderStyle="None" BorderColor="White"></asp:Label></P>
<P> </P>
</form>
</center>
</body>
</HTML>
Update.aspx.cs 源代码
namespace HowTos.MultipleImageUpdate
{
public class UPLOAD : System.Web.UI.Page
{

#region User Defined Code

protected System.Web.UI.WebControls.LinkButton LinkButton1;

protected System.Web.UI.WebControls.Label Label1;

private void Page_Load(System.Object sender, System.EventArgs e)
{
if ( this.IsPostBack )
this.SaveImages();
}

private System.Boolean SaveImages() {
//文件上传时循环执行


System.Web.HttpFileCollection _files = System.Web.HttpContext.Current.Request.Files;

//发送给浏览器的信息
System.Text.StringBuilder _message = new System.Text.StringBuilder("Files Uploaded:<br>");

try
{
for ( System.Int32 _iFile = 0; _iFile < _files.Count; _iFile ++ )
{

// 检查上传文件是否为gif或jpg

System.Web.HttpPostedFile _postedFile = _files[_iFile];
System.String _fileName, _fileExtension;

_fileName = System.IO.Path.GetFileName(
_postedFile.FileName);

_fileExtension = System.IO.Path.GetExtension(
_fileName);

if ( _fileExtension == ".gif" )
{

//保存文件到指定文件夹
_postedFile.SaveAs(
System.Web.HttpContext.Current.Request.MapPath(
"gifs/") + _fileName);
_message.Append(_fileName + "<BR>");

}
else if ( _fileExtension == ".jpg" )
{

//保存文件到指定文件夹
_postedFile.SaveAs(
System.Web.HttpContext.Current.Request.MapPath(
"jpgs/") + _fileName);
_message.Append(_fileName + "<BR>");

}
else {

_message.Append(_fileName + " <font color=\"red\">failed!! Only .gif and .jpg images allowed!</font> <BR>");

}

}

Label1.Text = _message.ToString();
return true;
}
catch ( System.Exception Ex )
{

Label1.Text = Ex.Message ;
return false;

}

}
#endregion

#region Web Form Designer generated code
override protected void OnInit(System.EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion
}
}


还有vb.net代码但是我将不做解释
Public Class UPLOAD
Inherits System.Web.UI.Page

#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
Protected WithEvents LinkButton1 As System.Web.UI.WebControls.LinkButton
Protected WithEvents File1 As System.Web.UI.HtmlControls.HtmlInputFile
Protected WithEvents File2 As System.Web.UI.HtmlControls.HtmlInputFile
Protected WithEvents File3 As System.Web.UI.HtmlControls.HtmlInputFile
Protected WithEvents File4 As System.Web.UI.HtmlControls.HtmlInputFile
Protected WithEvents File5 As System.Web.UI.HtmlControls.HtmlInputFile

#End Region


Protected WithEvents Label1 As System.Web.UI.WebControls.Label

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If (Me.IsPostBack) Then Me.SaveImages()
End Sub

Private Function SaveImages() As System.Boolean



Dim _files As System.Web.HttpFileCollection = System.Web.HttpContext.Current.Request.Files


Dim _message As New System.Text.StringBuilder("Files Uploaded:<br>")
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)

_fileExtension = System.IO.Path.GetExtension( _
 _fileName)

If (_fileExtension = ".gif") Then



_postedFile.SaveAs( _
 System.Web.HttpContext.Current.Request.MapPath( _
 "gifs/") + _fileName)
_message.Append(_fileName + "<BR>")


ElseIf (_fileExtension = ".jpg") Then


_postedFile.SaveAs( _
 System.Web.HttpContext.Current.Request.MapPath( _
 "jpgs/") + _fileName)
_message.Append(_fileName + "<BR>")


Else

_message.Append(_fileName & " <font color=""red"">failed!! Only .gif and .jpg images allowed!</font> <BR>")

End If

Next

Label1.Text = _message.ToString()
Return True



Catch Ex As System.Exception


Label1.Text = Ex.Message
Return False

End Try



End Function

End Class

相关软件

2345加速浏览器官方版

2345加速浏览器官方版 | 56.2MB

2345加速浏览器官方版

新一代2345加速浏览器采用Chromium和IE双内核,主打极速与安全特性。基于Chromium深度定制,引入网页智能预加载技术,访问网页更快速..

QQ浏览器官方正式版

QQ浏览器官方正式版 | 49.67MB

QQ浏览器官方正式版

QQ浏览器秉承TT浏览器1-4系列方便易用的特点,但技术架构不同,交互和视觉表现也重新设计,采用Chromium内核+IE双内核,让浏览快速稳定...

百度浏览器最新版下载

百度浏览器最新版下载 | 13.3MB

百度浏览器最新版下载

q百度浏览器,是一款简洁轻快、智能懂你的浏览器。依靠百度强大的搜索平台,在满足用户浏览网页的基础上,它整合百度体系业务优势,带给用户更方便的浏览方式功能...

UC浏览器官方正式版

UC浏览器官方正式版 | 44.2MB

UC浏览器官方正式版

UC浏览器(UC Browser)是UC Mobile Limited在2004年8月开发的一款软件,分uc手机浏览器和uc浏览器电脑版。UC浏览器是全球使用量最大的第三方手机浏览器...

猎豹浏览器2022最新版下载

猎豹浏览器2022下载 | 45MB

猎豹浏览器2022最新版下载

猎豹安全浏览器对Chrome的Webkit内核进行了超过100项的技术优化,访问网页速度更快。其具有首创的智能切换引擎,动态选择内核匹配不同网页...

360安全浏览器官方版下载

360安全浏览器下载 | 21.4MB

360安全浏览器官方版下载

360安全浏览器拥有全国最大的恶意网址库,采用恶意网址拦截技术,可自动拦截挂马、欺诈、网银仿冒等恶意网址。独创沙箱技术,在隔离模式即使访问****也不会感染...