Monday, June 6, 2011

Adding More than one File Upload control by button event

The steps are:
1. At first as usual we need to create entity for that and then to create an strongly typed dataset using xsd
2. In the repository to save the files to DB we need to code the following:

public static void InsertFile(string FileName)
        {
            using (FilesTableAdapter ta = new FilesTableAdapter())
            {
                if (FileName != null)
                {
                    ta.InsertFile(FileName);
                }
            }
        }

3. In controller the following code is done to insert the file
Also shld add the system.io header file

[HttpGet]
        public ActionResult Fileupload()
        {
            return View();
        }

[HttpPost]
        public ActionResult Fileupload(FormCollection frm, string save)
//Here save is the value of the “Submit” button
        {
            if (save.Equals("save")==true)
            {       
                foreach (string file in Request.Files)
// Request.Filesรจis the pre defined function
                {
                    var postedfile = Request.Files[file];
                    if (postedfile.ContentLength != 0)
                    {
                    postedfile.SaveAs(Server.MapPath("~/Files/") + Path.GetFileName(postedfile.FileName));
                    string Filename =Path.GetFileNameWithoutExtension(postedfile.FileName) + Path.GetExtension(postedfile.FileName);
// Path.GetFileName(),GetFileNameWithoutExtension(),GetExtension are the built-in methods for retrieving the file name
                     FileRepository.InsertFile(Filename);                       
//As we know “InsertFile” is an DB function 
                    }                   
                }
                return View();
              }
            return View();
        }

4. Finally in View,

<%using (Html.BeginForm("Fileupload", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
          {%>
    <script type="text/javascript" language="javascript">

        var Filecount = 0;
        function AddFileControl() {
            if (Filecount <= 4) {
                var div = document.createElement('DIV');
                div.innerHTML = '<input id="file' + Filecount + '"name="file' + Filecount + '"type="file" style=width:50%/>' + " " + " " + " " + '<input id="button' + Filecount + '"type="button"''value="Remove" onclick="RemoveFileControl(this)" style=widht:"15%" />' + '<br/>';
                div.style.padding = "0.5em";
                document.getElementById("FileControl").appendChild(div);
                Filecount++;
            }
            else
                alert('Only 5 files can be uploaded');
        }


        function RemoveFileControl(div) {
            document.getElementById("FileControl").removeChild(div.parentNode);
            Filecount--;
        }
       
    </script>

    <table>
    <tr>
    <td>
    <div id="FileControl"></div>
    <input type="button" name="AddMoreFiles" value="AddMoreFiles" onclick="AddFileControl()" />
    <script type="text/javascript" language="javascript">
        AddFileControl();
    </script>
    </td>
    </tr>
    <tr>
    <td>
    <input type="submit" value="save" name="save"/>   
    </td>   
    </tr>
    </table>  
    <% } %>


No comments:

Post a Comment