Monday, 31 December 2012

Multiple fileuploads in asp.net 3.5/4.0



multiple file upload in asp.net

Step 1)  Create a Webpage name it as MultipleFileUploadsDemo

Step 2)  Add FileUpload Controls and 1 Button control
<div>
        <asp:FileUpload ID="FileUpload1" runat="server"  /><br />
        <asp:FileUpload ID="FileUpload2" runat="server" />
        <br />
        <br />
        <asp:Button ID="btnUpload2" runat="server" Text="Upload files" OnClick="btnUpload_Click"  /><br />
    </div>

Step 3) Create a uploads folder in WebApplication

Step 4) If Website  is in IIS Directory(for ex:C:\inetpub\wwwroot\Test\WebApplication1) ,make sure all access permissions set   for
IIS_IUSRS(machinename\IIS_USRS) user i.e read and write permissions for uploads folder
.

Step 5) If Website is file based, no need to worry about permissions

Step 6) Once it is done, Add Event Handler for btnUpload shown above

     protected void btnUploads_Click(object sender, EventArgs e)
        {
                        try
            {
               
                if (FileUpload1.HasFile)
                {
                    Savefile(FileUpload1.FileContent, FileUpload1.FileName);
                }
                if (FileUpload2.HasFile)
                {
                    Savefile(FileUpload2.FileContent, FileUpload1.FileName);
                }

            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }

        }

    void Savefile(System.IO.Stream stream, String fileName)
        {
            using (System.IO.StreamReader reader = new System.IO.StreamReader(stream))
            {
                String content = reader.ReadToEnd();
                System.IO.File.AppendAllText(MapPath("./uploads/" fileName), content);
            }
        }
Tags:multiple file upload in asp.net , multiple file upload in asp.net 3.5/4.0,multiple file upload in asp.net using C#, multiple file upload in asp.net C#,multiple FileUpload in asp.net