Initial Chamber
Step 1
Open Visual Studio 2010 and Create an Empty Website and give it a suitable name (FileUpload_demo).
Step 2
In Solution Explorer you will get your empty website. Now, add a web form, SQL Database and 3 CSS files as in the following.
For the Web Form
FileUpload_demo (your empty website), then right-click and Add New Item. Now, select Web Form and name it Fileupload_demo.aspx.
Get back to your website in Solution Explorer and Add New Folder and name it Images.
Design Chamber
In the design I did not include any CSS, if you want to design it then you can do that by making the Style in the <head></head> tag. I have just included how it is done to upload multiple files in the fileupload control. If anyone wants the CSS of the file upload then just comment me and I'll arrange it and get back to you.
This is the design code for the File Upload Control:
- <%@ 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></title>
- <style type="text/css">
- .style1
- {
- width: 258px;
- }
- .style2
- {
- width: 168px;
- }
- .style3
- {
- text-decoration: underline;
- text-align: center;
- color: #9966FF;
- }
- </style>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <br />
-
- </div>
- <p class="style3">
- <strong style="font-size: large">Multiple FileUpload in Asp.Net</strong></p>
- <table style="width:100%;">
- <tr>
- <td class="style1">
- <asp:FileUpload ID="FileUpload1" runat="server" />
- </td>
- <td class="style2">
- <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Upload" />
- </td>
- <td>
- <asp:Label ID="lblmsg" runat="server"></asp:Label>
- </td>
- </tr>
- <tr>
- <td class="style1">
- </td>
- <td class="style2">
- </td>
- <td>
- </td>
- </tr>
- </table>
- </form>
- </body>
- </html>
Or you can make it by dragging the control from the toolbox and placing it into your .aspx page. Try to drag all the controls in one HTML table and in that way your design looks good. Yeah! Probably my design is not so worthy right now, but someday I'll start some CSS articles too.
Your design looks as in the following:
Code Chamber
Now we are entering into our Code Zone. Let's begin by adding a namespace as in the following:
Open your Fileupload_demo.aspx.cs file and add this code.
Here is the code:
- 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 _Default : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- FileUpload1.Attributes["multiple"] = "multiple";
- }
-
- protected void Button1_Click(object sender, EventArgs e)
- {
- for (int i = 0; i < Request.Files.Count; i++)
- {
- HttpPostedFile fu = Request.Files[i];
- if (fu.ContentLength > 0)
- {
- try
- {
- string fileName = Path.GetFileName(fu.FileName);
- fu.SaveAs(Server.MapPath("~/Images/") + fileName);
-
- lblmsg.Text = "Files have been Uploaded";
- lblmsg.ForeColor = System.Drawing.Color.Green;
- }
- catch (Exception ex)
- {
- lblmsg.Text = "File Could not be uploaded" + ex.Message;
- lblmsg.ForeColor = System.Drawing.Color.Red;
- }
- }
-
- }
- }
- }
Output ChamberAs you browse you can select as many files you want to upload. Select it and click on Upload.
You can see the files in your images folder that you made in the initial steps.
I hope you will enjoy this. Thank You for reading.
Have a Nice Day.