public PartialViewResult Index(HttpPostedFileBase fileexcel) { BanTinTuVan bantintuvan = new BanTinTuVan(); if (fileexcel != null && fileexcel.ContentLength > 0) { try { string name = ReadNameFile(fileexcel.FileName); string path = "~/Files/somefiles/"; // path: đường dẫn lưu fileexcel tải lên Path.Combine(HostingEnvironment.MapPath(path)); String pathOnServer = Path.Combine(path); //var fullPath = Path.Combine(pathOnServer, Path.GetFileName(name)); var fullPath = Path.Combine(Server.MapPath(path), Path.GetFileName(name)); fileexcel.SaveAs(fullPath); // lua file vào server string url = path + name; // url: đường dẫn lưu file excel trên server ReadExcel excel = new ReadExcel(); url = Path.Combine(HostingEnvironment.MapPath(url)); List <DataTable> table = excel.makeDataTableFromSheetName(url, excel.MySheet(url)); bantintuvan = excel.BanTinTuVanExcel(table); } catch (Exception) { } return(PartialView("TuVan_ViewChung", bantintuvan)); } return(PartialView()); }
/// <summary> /// HieuTM: Upload file, phân tích trả về mẫu dữ liệu. /// </summary> /// <returns></returns> public PartialViewResult UploadExcel() { LogDb.WriteLogInfo("HieuTM", "UploadExcel", "TuVanController", "Vao Upload"); BanTinTuVan bttv = null; try { LogDb.WriteLogInfo("HieuTM", "UploadExcel", "TuVanController", "file count: " + Request.Files.Count.ToString()); string linkSaveServer = ""; for (int i = 0; i < Request.Files.Count; i++) { string directoryPath = Path.Combine((Server.MapPath("~/Files/tuvan/"))); var file = Request.Files[i]; string filename = file.FileName; if (!System.IO.Directory.Exists(directoryPath)) { System.IO.Directory.CreateDirectory(directoryPath); } string newname = Lib.Security.CurrentUserName + "_" + DateTime.Now.ToString("yyyyMMdd_HHmmss") + "_" + Path.GetFileName(file.FileName); linkSaveServer = Path.Combine(Server.MapPath("~/Files/tuvan/"), newname); ViewBag.LinkFile = linkSaveServer; file.SaveAs(linkSaveServer); ReadExcel excel = new ReadExcel(); List <DataTable> table = excel.makeDataTableFromSheetName(linkSaveServer, excel.MySheet(linkSaveServer)); bttv = table.Count == 0 ? null : excel.BanTinTuVanExcel(table); if (bttv != null) { bttv.Link = @"Files/tuvan/" + newname; } } } catch (Exception ex) { bttv = null; LogDb.WriteLogInfo("HieuTM", "UploadExcel", "TuVanController", "Error: " + ex.Message); LogDb.WriteLogError("HieuTM", "UploadExcel", "TuVanController", ex); } return(PartialView("TuVan_All_Par", bttv)); }