Adsence750x90

Monday, July 5, 2010

Multiple File Upload in ASP.Net

ASP.Net Multiple File Upload

Using HttpFileCollection class provide  to access and manage file uploaded from client. But still have the problem of multiple selection of files. With over limitation am am going to explain multiple  uploading by multiple FileUploader Controls. HttpPostedFile class is used to provide properties and methods if single files in HttpFileCollection class.



<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    
    <asp:FileUpload ID="FileUpload1" runat="server" />
    <asp:FileUpload ID="FileUpload2" runat="server" />
    <asp:Button ID="btnUpload" runat="server"
        Text="Upload" onclick="btnUpload_Click" />
    </form>
</body>
</html>





 using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Collections;
public partial class generic : System.Web.UI.Page
{
    
    protected void Page_Load(object sender, EventArgs e)
    {
       
    }
    protected void btnUpload_Click(object sender, EventArgs e)
    {
        //Post file Collection Class
        HttpFileCollection files = Request.Files;
        if (files.Count > 0)
        {
            for (int i = 0; i < files.Count; i++)
            {
                //read Individual files 
                HttpPostedFile p = files[i];
                string path = Server.MapPath(Guid.NewGuid().ToString() + ".jpg");
                p.SaveAs(path);
            }
        }
    }
}

No comments: