Thursday, July 5, 2012

Uploading Multiple Files using JQuery in Asp.Net

Download 2 javascript files (jquery-1.3.2.js) and (jquery.MultiFile.js)

codes
.aspx page
-------------------------------------------------------
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Upload Multiple Files in ASP.NET Using jQuery</title>
        <script src="Scripts/jquery-1.3.2.js" type="text/javascript"></script>
        <script src="Scripts/jquery.MultiFile.js" type="text/javascript"></script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:FileUpload ID="FileUpload1" runat="server" class="multi" />
        <br />
        <asp:Button ID="btnUpload" runat="server" Text="Upload All"
            onclick="btnUpload_Click" />
    </div>
   
    </form>
</body>
</html>
======================================
.aspx.cs page
---------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnUpload_Click(object sender, EventArgs e)
    {
        try
        {
            HttpFileCollection hfc = Request.Files;
            for (int i = 0; i < hfc.Count; i++)
            {
                HttpPostedFile hpf = hfc[i];
                if (hpf.ContentLength > 0)
                {
                    hpf.SaveAs(Server.MapPath("docs") + "\\" +
                      System.IO.Path.GetFileName(hpf.FileName));

                }
            }
            Response.Write(hfc.Count + " files are uploaded successfully.");
        }
        catch (Exception ex)
        {
            Response.Write("Error in Uploading !!!");
        }
    }
}

Download Code

No comments:

Post a Comment