public void SaveFiles(string token, string subdirectory, int itemId) { string fileUrl = Request.Url.GetLeftPart(UriPartial.Authority) + "/Attachments/AttachedFiles"; string tempDirectory = Server.MapPath("~/Attachments/TempFiles"); StudentContext db = new StudentContext(); tempDirectory = tempDirectory + "/" + token; if (Directory.Exists(tempDirectory)) { string destDirectory = Server.MapPath("~/Attachments/AttachedFiles"); destDirectory = Path.Combine(destDirectory, subdirectory, itemId.ToString()); if (!Directory.Exists(destDirectory)) { Directory.CreateDirectory(destDirectory); } foreach (var file in Directory.GetFiles(tempDirectory)) { FileInfo fileInfo = new FileInfo(file); Attachment att = new Attachment(); att.Filename = fileInfo.Name; att.ParentType = subdirectory; att.FilePath = Path.Combine(fileUrl, itemId.ToString(), fileInfo.Name); att.ItemId = itemId; db.Attachments.Add(att); System.IO.File.Move(file, Path.Combine(destDirectory, fileInfo.Name)); } Directory.Delete(tempDirectory); } db.SaveChanges(); }
public string FileImportExcel(IEnumerable<HttpPostedFileBase> file, string importId, string type) { string message = string.Empty; foreach (string upload in Request.Files) { try { FileInfo fileinfo = new FileInfo(Request.Files[upload].FileName); if (fileinfo.Extension == ".xlsx") { string fileUrl = Request.Url.GetLeftPart(UriPartial.Authority) + "/Attachments/ImportedFiles"; StudentContext db = new StudentContext(); string destDirectory = Server.MapPath("~/Attachments/ImportedFiles"); destDirectory = Path.Combine(destDirectory, type, importId); if (!Directory.Exists(destDirectory)) { Directory.CreateDirectory(destDirectory); } string path = AppDomain.CurrentDomain.BaseDirectory + "UploadedFiles/"; string filename = Path.GetFileName(Request.Files[upload].FileName); filename = Regex.Replace(filename, @"\s+", ""); //fetch path of the local directory from iCATStaticItemClass. //string targetFolderPath = Path.GetTempPath();//Server.MapPath("~/Administrator/TempFiles/" + iCATGlobal.CurrentTenantInfo.TenantName); // This is the part Im wondering about. Will this still function the way it should on the webserver after upload? //create target filePath string targetFilePath = Path.Combine(destDirectory, filename); //Check if directory exists. if (Directory.Exists(destDirectory)) { //if file with the same name exist in the directory. if (System.IO.File.Exists(targetFilePath)) { while (System.IO.File.Exists(targetFilePath))//while file exist in the directory. { try { //delete file from with target filepath. System.IO.File.Delete(targetFilePath); } catch (Exception) { } } // save file Request.Files[upload].SaveAs(targetFilePath); } else { //save file Request.Files[upload].SaveAs(targetFilePath); } } else { //if directory doesn't exist create a new directory. Directory.CreateDirectory(destDirectory); //save file Request.Files[upload].SaveAs(targetFilePath); } Attachment att = new Attachment(); att.Filename = fileinfo.Name; att.ParentType = type; att.FilePath = Path.Combine(fileUrl, type, importId, filename); att.ItemId = Convert.ToInt64(importId); db.Attachments.Add(att); db.SaveChanges(); string worksheetName = "Sheet1"; //Initilze the Excel handler constructor with filepath and sheet name to fetch. ExcelHandler objExcel = new ExcelHandler(targetFilePath, worksheetName); DataTable table = new DataTable(); //create a list of a dynamic type object. //List<ExpandoObject> result = new List<ExpandoObject>(); //call function from excelhandler class to get all the rows in the excel file. bool test = objExcel.ReadDocument(0, ref table); var userId = _userStatistics.UserId; var organizationId = _userStatistics.OrganizationId; if (type == "student") { List<Student> target = table.AsEnumerable().Skip(1) .Select(row => new Student { // assuming column 0's type is Nullable<long> RollNo = String.IsNullOrEmpty(row.Field<string>(0)) ? "not found" : row.Field<string>(0), FullName = String.IsNullOrEmpty(row.Field<string>(1)) ? "not found" : row.Field<string>(1), CourseName = String.IsNullOrEmpty(row.Field<string>(2)) ? "not found" : row.Field<string>(2), ClassName = String.IsNullOrEmpty(row.Field<string>(3)) ? "not found" : row.Field<string>(3), SectionName = String.IsNullOrEmpty(row.Field<string>(4)) ? "not found" : row.Field<string>(4), DepartmentName = String.IsNullOrEmpty(row.Field<string>(5)) ? "not found" : row.Field<string>(5), Email = String.IsNullOrEmpty(row.Field<string>(6)) ? "not found" : row.Field<string>(6), Remarks = String.IsNullOrEmpty(row.Field<string>(7)) ? "not found" : row.Field<string>(7), InsertedOn = DateTime.Now, ImportId = importId, }).ToList(); SaveStudentsInDB(target, importId); } else if (type == "staff") { List<Staff> target = table.AsEnumerable().Skip(1) .Select(row => new Staff { // assuming column 0's type is Nullable<long> FullName = String.IsNullOrEmpty(row.Field<string>(0)) ? "not found" : row.Field<string>(0), Email = String.IsNullOrEmpty(row.Field<string>(1)) ? "not found" : row.Field<string>(1), Remarks = String.IsNullOrEmpty(row.Field<string>(3)) ? "not found" : row.Field<string>(3), InsertedOn = DateTime.Now, ImportId = importId, StaffTypeName = String.IsNullOrEmpty(row.Field<string>(2)) ? "not found" : row.Field<string>(2), }).ToList(); SaveStaffInDB(target, importId); } } else { message += "File {" + fileinfo.FullName + "} is not supported."; } } catch (Exception) { message += "File(s) does not support."; } } return importId; }