Add File Control Dynamically and Upload File To The Server-ASP.NET

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="MultipleUploads.aspx.cs"
   Inherits="MultipleUploads" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="">
<head runat="server">
   <title>Untitled Page</title>
   <form id="form1" runat="server">
           <p id="upload-area">
               <input id="File1" type="file" runat="server" size="60" />
           <input id="AddFile" type="button" value="Add file" onclick="addFileUploadBox()" />
               <asp:Button ID="btnSubmit" runat="server" Text="Upload Now" OnClick="btnSubmit_Click" /></p>
           <span id="Span1" runat="server" />

   <script type="text/javascript">
function addFileUploadBox()
    if (!document.getElementById || !document.createElement)
        return false;
    var uploadArea = document.getElementById ("upload-area");

    if (!uploadArea)

    var newLine = document.createElement ("br");
    uploadArea.appendChild (newLine);

    var newUploadBox = document.createElement ("input");

    // Set up the new input for file uploads
    newUploadBox.type = "file";
    newUploadBox.size = "60";

    // The new box needs a name and an ID
    if (!addFileUploadBox.lastAssignedId)
        addFileUploadBox.lastAssignedId = 100;
    newUploadBox.setAttribute ("id", "dynamic" + addFileUploadBox.lastAssignedId);
    newUploadBox.setAttribute ("name", "dynamic:" + addFileUploadBox.lastAssignedId);
    uploadArea.appendChild (newUploadBox);

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;

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

       String UpPath;
       UpPath = "C:\\UploadedUserFiles";

       if (!Directory.Exists(UpPath))
   protected void btnSubmit_Click(object sender, EventArgs e)
       HttpFileCollection uploads = HttpContext.Current.Request.Files;
       for (int i = 0; i < uploads.Count; i++)
           HttpPostedFile upload = uploads[i];

           if (upload.ContentLength == 0)

           string c = System.IO.Path.GetFileName(upload.FileName); // We don't need the path, just the name.

               upload.SaveAs("C:\\UploadedUserFiles\\" + c);
               Span1.InnerHtml = "Upload(s) Successful.";
           catch (Exception Exp)
               Span1.InnerHtml = "Upload(s) FAILED.";
Next Post Previous Post
No Comment
Add Comment
comment url