public ActionResult ImportB(HttpPostedFileBase files) { try { DataTable dt = new DataTable(); if (ModelState.IsValid) { List <DataLogMonitouch> listcsvfiles = new List <DataLogMonitouch>(); for (int i = 0; i <= Request.Files.Count; i++) { var file = Request.Files[i]; if (file != null && file.ContentLength > 0 && file.ContentLength <= 52428800) { var fileName = Path.GetFileName(file.FileName); var path = Path.Combine(Server.MapPath("~/App_Data/Monitouch/"), fileName); file.SaveAs(path); dt = ProcessCSV(path); ViewBag.Message = ProcessBulkCopy(dt); DataLogMonitouch upload = new DataLogMonitouch(); listcsvfiles.Add(upload); db.datalogmonitouchs.Add(upload); db.SaveChanges(); } ViewBag.Message = "File Uploaded successfully"; return(RedirectToAction("displayMonitouchFile")); } } //ViewBag.Success = "File Uploaded successfully."; return(View()); } catch (Exception ex) { ViewBag.Error = "Error while uploading the files."; return(View(ex)); } }
public ActionResult Index(IEnumerable <HttpPostedFileBase> fileNames, string rbGrp) { string UserName = Session["UserName"].ToString(); string name = rbGrp.ToString(); if (name == "Delayed") { foreach (HttpPostedFileBase fileAB in fileNames) { if (fileAB != null && fileAB.ContentLength > 0) { String FileExtn = System.IO.Path.GetExtension(fileAB.FileName); if (!(FileExtn == ".csv" || FileExtn == ".CSV")) { ViewBag.Error = "Only CSV are allowed!"; return(View()); } else { DataTable dt = new DataTable(); if (ModelState.IsValid) { List <FileDetail> fileDetails = new List <FileDetail>(); List <DataLogMonitouch> listcsvfiles = new List <DataLogMonitouch>(); List <DataLogMonitouch> Csvfiles = new List <DataLogMonitouch>(); string root = "~/App_Data/Delayed/DataLogMonitouches/UserName-" + UserName + "/"; var dirName = "~/App_Data/Delayed/DataLogMonitouches/UserName-" + UserName + "/Date-" + string.Format("{0:dd-MM-yyyy}", DateTime.Now) + "/"; if (!Directory.Exists(root)) { System.IO.Directory.CreateDirectory(Server.MapPath(root)); } if (!Directory.Exists(dirName)) { System.IO.Directory.CreateDirectory(Server.MapPath(dirName)); } var fileName = Path.GetFileNameWithoutExtension(fileAB.FileName); var fileNameWithExt = Path.GetFileNameWithoutExtension(fileAB.FileName) + DateTime.Now.ToString("dd-MM-yyyy_hh-mm-ss") + ".csv"; var path = Path.Combine(Server.MapPath(dirName), fileName + DateTime.Now.ToString("dd-MM-yyyy_hh-mm-ss") + ".csv"); fileAB.SaveAs(path); FileDetail fileDetail = new FileDetail() { FileName = fileNameWithExt, Extension = Path.GetExtension(fileNameWithExt), Id = Guid.NewGuid() }; fileDetails.Add(fileDetail); DelayedUpload upload = new DelayedUpload(); upload.FileDetails = fileDetails; db.DelayedUploads.Add(upload); db.SaveChanges(); ViewBag.Message = "Sccessfully upload files on server."; } } } else { ViewBag.Error = "Please Select CSV files only."; return(View()); } } } else { foreach (HttpPostedFileBase fileAB in fileNames) { if (fileAB != null && fileAB.ContentLength > 0) { String FileExtn = System.IO.Path.GetExtension(fileAB.FileName); if (!(FileExtn == ".csv" || FileExtn == ".CSV")) { ViewBag.Message = "Only CSV are allowed!"; return(View()); } else { DataTable dt = new DataTable(); if (ModelState.IsValid) { List <FileDetail> fileDetails = new List <FileDetail>(); List <DataLogMonitouch> listcsvfiles = new List <DataLogMonitouch>(); string root = "~/App_Data/Instant/DataLogMonitouches/UserName-" + UserName + "/"; var dirName = "~/App_Data/Instant/DataLogMonitouches/UserName-" + UserName + "/Date-" + string.Format("{0:dd-MM-yyyy}", DateTime.Now) + "/"; if (!Directory.Exists(root)) { System.IO.Directory.CreateDirectory(Server.MapPath(root)); } if (!Directory.Exists(dirName)) { System.IO.Directory.CreateDirectory(Server.MapPath(dirName)); } var fileName = Path.GetFileNameWithoutExtension(fileAB.FileName); var fileNameWithExt = Path.GetFileNameWithoutExtension(fileAB.FileName) + DateTime.Now.ToString("dd-MM-yyyy_hh-mm-ss") + ".csv"; FileDetail fileDetail = new FileDetail() { FileName = fileNameWithExt, Extension = Path.GetExtension(fileNameWithExt), Id = Guid.NewGuid() }; fileDetails.Add(fileDetail); var path = Path.Combine(Server.MapPath(dirName), fileName + DateTime.Now.ToString("dd-MM-yyyy_hh-mm-ss") + ".csv"); fileAB.SaveAs(path); dt = ProcessCSV(path); fileAB.SaveAs(path); DelayedUpload upload1 = new DelayedUpload(); upload1.FileDetails = fileDetails; db.DelayedUploads.Add(upload1); dt = ProcessCSV(path); ViewBag.Message = ProcessBulkCopy(dt); DataLogMonitouch upload = new DataLogMonitouch(); listcsvfiles.Add(upload); db.datalogmonitouchs.Add(upload); db.SaveChanges(); ViewBag.Message = "Successfully Uploaded DataLogMonitouch files !!!"; //} } } } else { ViewBag.Message = "Please Select file within 20 MB."; return(View()); } } } //return RedirectToAction("MonitouchFileUpload"); return(View()); }