Upload Multiple Attachement File on Button Click MVC - B M SOLUTION
  • Upload Multiple Attachement File on Button Click MVC

     

    <h1>GIVEN BELOW Demo 1</h1>

    <script type="text/javascript">

        function GetDynamicTextbox() {

            //  return '  <div><input type="text" class="form-control" style="width:250px;" name="txttest" id="Amount" /> <input type="button" onclick="RemoveTextBox(this)" value="Remove" />  </div>'

            return '  <div> <input type="file" id="myfile" name="myfile"> <input type="button" onclick="RemoveTextBox(this)" value="Remove" />  </div>'

        }

        function AddTextBox() {

            var div = document.createElement('DIV');

            div.innerHTML = GetDynamicTextbox("");

            document.getElementById("divCount").appendChild(div);

        }

        function RemoveTextBox(div) {

            document.getElementById("divCount").removeChild(div.parentNode.parentNode);

        }

    </script>


    @using (Html.BeginForm("SaveFile", "Encrochment", FormMethod.Post, new { enctype = "multipart/form-data", @class = "form-horizontal", @id = "form_sample_2" }))

    {

        <div id="divCount">

            <div>

                @*<input type="text" class="form-control" name="txttest" style="width:250px;" id="Amount" />*@

                <input type="file" id="myfile" name="myfile">


                <input type="button" onclick="AddTextBox()" class="btn btn-danger" value="Add" />

            </div>

        </div>

        <br />

        <input type="submit" value="submit" />

    }


    <h1>GIVEN BELOW Demo 2</h1>


    @using (Html.BeginForm("imageUpload", "Encrochment", FormMethod.Post, new { @enctype = "multipart/form-data" }))

    {


        @Html.AntiForgeryToken()



        <div id="uploads">

            <div id="uploadtemplate">

                <label for="file1">Filename:</label>

                <input type="file" name="files" id="file1" />

            </div>

            <a href="#" id="addFile" class="btn btn-primary">Add More Image</a>

        </div>

        <input type="submit" />


    }


    <script src="~/Scripts/jquery-1.7.1.min.js"></script>

    <script>

        $(document).ready(function () {

            debugger;

            $('#addFile').data('uploadtemplate', $('#uploadtemplate').attr('id', '').clone());


            $('#addFile').click(function (e) {

                $(this).data('uploadtemplate').clone().insertBefore($(this));


                e.preventDefault();

            });

        });

    </script>  


    Controller

    <!-- DEMO 1 EVENT-->

     [HttpPost]

            public ActionResult SaveFile(FormCollection frm, IEnumerable<HttpPostedFileBase> myfile)

            {

                MainModel itemnew = new MainModel();


                string sid = Request.Cookies["6767686"].Value;

                string[] AllArray = sid.Split(',');

                ViewBag.UserName = AllArray[1];

                ViewBag.Role = AllArray[2];

                ViewBag.id = AllArray[0];


                string WqfId = frm["File1"];

                Guid WId = Guid.Empty;

                Guid.TryParse(WqfId, out WId);


                string multiplefile = frm["myfile"];


                foreach (var file in myfile)

                {

                    if (file.ContentLength > 0)

                    {

                        var fileName = Path.GetFileName(file.FileName);

                        var path = Path.Combine(Server.MapPath("~/IdProofDoc"), fileName);

                        var sqlpath = "~/IdProofDoc" + fileName;

                        file.SaveAs(path);

                        /* write here your sql code to save image path into database */

                    }

                }


                return RedirectToAction("WaqfEncrochment");

            }



    <!-- DEMO 2 EVENT-->

            [HttpPost]

            [AllowAnonymous]

            [ValidateAntiForgeryToken]


            public ActionResult imageUpload(IEnumerable<HttpPostedFileBase> files)

            {

                foreach (var file in files)

                {

                    if (file.ContentLength > 0)

                    {

                        var fileName = Path.GetFileName(file.FileName);

                        var path = Path.Combine(Server.MapPath("~/IdProofDoc"), fileName);

                        var sqlpath = "~/IdProofDoc" + fileName;

                        file.SaveAs(path);

                        /* write here your sql code to save image path into database */

                    }

                }

                return RedirectToAction("");

            }



  • You might also like

    No comments :

    Post a Comment