{"id":78,"date":"2019-09-15T06:45:35","date_gmt":"2019-09-15T06:45:35","guid":{"rendered":"http:\/\/sumitjangid.com\/?p=78"},"modified":"2019-09-15T06:53:46","modified_gmt":"2019-09-15T06:53:46","slug":"saving-uploading-files-through-form-and-creating-xml-in-database","status":"publish","type":"post","link":"http:\/\/sumitjangid.com\/index.php\/2019\/09\/15\/saving-uploading-files-through-form-and-creating-xml-in-database\/","title":{"rendered":"Saving\/uploading files through form and creating XML in database"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>Model Definations:\npublic HttpPostedFileBase[] files { get; set; }\n\n\nView:\n@using (Html.BeginForm(\"newBen1\", \"Home\", FormMethod.Post, new { enctype = \"multipart\/form-data\" }))\n{\n&lt;div class=\"form-group\">\n        &lt;div class=\"col-md-4\">\n            @Html.LabelFor(model => model.ben.ben_supp_docs, htmlAttributes: new { @class = \"control-label\" })\n\n            @Html.TextBoxFor(model => model.files, new { type = \"file\", @multiple = \"multiple\" })\n            @Html.ValidationMessageFor(model => model.ben.ben_supp_docs, \"\", new { @class = \"text-danger\" })\n        &lt;\/div>\n    &lt;\/div>\n}\n\n\nPost Action Method:\n[HttpPost]\n        public ActionResult newben1(SuperModel model){\nif (model.files[0] != null)\n{\n  if (!System.IO.Directory.Exists(Server.MapPath(\"~\/UploadedFiles\")))  \/\/creating a new directory if not exists\n     {                 System.IO.Directory.CreateDirectory(Server.MapPath(\"~\/UploadedFiles\"));\n      }\n XElement[] xes = new XElement[10];\n\nvar xel = new XElement(\"Roots\",\n                            from files in model.files\n                            select new XElement(\"RootFiles\",\n                            new XElement(\"File\", new XAttribute(\"path\", \"~\/UploadedFiles\/Beneficiaries\/\" + ab1 + \"\/\" + bid + \"\/\"), new XAttribute(\"file\", files.FileName)\n                            )));   \/\/creating the xml file structure with data in it\n\nforeach (HttpPostedFileBase str in model.files)\n  {\n      var filename = Path.GetFileName(str.FileName);\n      var ServerSavePath = Path.Combine(Server.MapPath(\"~\/UploadedFiles\/Beneficiaries\/\" + ab1 + \"\/\" + bid + \"\/\") + filename);\n      str.SaveAs(ServerSavePath);     \/\/ copying the files in the uploaded folder\n    }\n    model.ben.ben_supp_docs = xel.ToString(); \/\/saving the list of file names and path in the database\n    }\n}<\/code><\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[3,5,4,2],"tags":[],"_links":{"self":[{"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/posts\/78"}],"collection":[{"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/comments?post=78"}],"version-history":[{"count":2,"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/posts\/78\/revisions"}],"predecessor-version":[{"id":84,"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/posts\/78\/revisions\/84"}],"wp:attachment":[{"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/media?parent=78"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/categories?post=78"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/tags?post=78"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}