Upload multiple files in asp.net 4.5 or above with C#
Many times we want to upload multiple files so this
code will help you
ASP.Net
4.5 added a new feature to FileUpload control i.e. AllowMultiple which allows
user to select multiple files in browsers that support HTML5. ASP.Net versions
2.0, 3.0, 3.5 and 4.0 does not support AllowMultiple feature.
Source Code:-
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="fileuploader.aspx.cs" Inherits="fileuploader" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:FileUpload ID="FileUpload1" runat="server" multiple = "multiple" />
<asp:Button ID="btnupload" Text="Upload" runat="server" OnClick="btnupload_Click" />
<asp:Label ID="lblmsg" runat="server" Text="File
uploaded successfully." ForeColor="Green"
Visible="false" />
</div>
</form>
</body>
</html>
Code behind(c#):-
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Web;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using
System.IO;
public partial class fileuploader :
System.Web.UI.Page
{
protected void
Page_Load(object sender, EventArgs e)
{
FileUpload1.Attributes["multiple"] = "multiple";
}
protected void
btnupload_Click(object sender, EventArgs e)
{
for (int i =
0; i < Request.Files.Count; i++)
{
HttpPostedFile
postedFile = Request.Files[i];
if
(postedFile.ContentLength > 0)
{
string
fileName = Path.GetFileName(postedFile.FileName);
postedFile.SaveAs(Server.MapPath("~/images/") +
fileName);
lblmsg.Text += string.Format("<b>{0}</b>
uploaded.<br />", fileName);
}
}
}
}
Out-Put:-
Upload multiple files in asp.net 4.5 or above with C#
Reviewed by NEERAJ SRIVASTAVA
on
5:20:00 PM
Rating:
No comments: