//-> SaveUploadFiles public static async Task <List <sm_doc> > SaveUploadFiles(LoanEntities db, int tableID, int recordID, HttpRequestBase Request) { var files = Request.Files; List <sm_doc> documents = new List <sm_doc>(); if (files != null) { for (int i = 0; i < files.Count; i++) { if (files[i].ContentLength == 0) { continue; } string pathForSavingToDB = "", imageNameForSavingToDB = ""; string path = ""; string uploadFolderName = "uploads"; path = HttpContext.Current.Server.MapPath(@"~\" + uploadFolderName); path = System.Configuration.ConfigurationManager.AppSettings["mediaURL"] + @"\" + uploadFolderName; if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } string currentYear = DateTime.Now.Year.ToString(); path += @"\" + currentYear; if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } string currentMonth = DateTime.Now.Month > 9 ? DateTime.Now.Month.ToString() : "0" + DateTime.Now.Month; path += @"\" + currentMonth; if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } var createImageUniqueName = tableID + "_" + recordID + "_" + DateTime.Now.ToString("yyyy-MM-dd_HHmmssfff") + "_" + i + ".jpg";//in order to avoid redundant image path , use i (variable) files[i].SaveAs(path + @"\" + createImageUniqueName); pathForSavingToDB = uploadFolderName + "/" + currentYear + "/" + currentMonth + "/" + createImageUniqueName; var document = new sm_doc(); document.docs_Name = createImageUniqueName; document.docs_TableID = tableID; document.docs_CreatedDate = DateTime.Now; document.docs_Value = recordID.ToString(); document.docs_FilePath = pathForSavingToDB; db.sm_doc.Add(document); await db.SaveChangesAsync(); documents.Add(document); } } return(documents); }
//-- Save upload document --// public static async Task <List <sm_doc> > SaveUploadImage(THEntities db, string documentName, int tableID, int recordID, List <string> base64) { List <sm_doc> documents = new List <sm_doc>(); if (base64 != null) { foreach (var image in base64) { using (MemoryStream ms = new MemoryStream(Convert.FromBase64String(image))) { string pathForSavingToDB = "", imageNameForSavingToDB = ""; using (Bitmap bm = new Bitmap(ms)) { string path = ""; string uploadFolderName = "uploads"; path = HttpContext.Current.Server.MapPath(@"~\" + uploadFolderName); if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } string currentYear = DateTime.Now.Year.ToString(); path += @"\" + currentYear; if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } string currentMonth = DateTime.Now.Month > 9 ? DateTime.Now.Month.ToString() : "0" + DateTime.Now.Month; path += @"\" + currentMonth; if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } var createImageUniqueName = recordID + "_" + DateTime.Now.ToString("yyyy-MM-dd_HHmmssfff") + ".jpg"; bm.Save(path + @"\" + createImageUniqueName); imageNameForSavingToDB = documentName + "_" + createImageUniqueName; pathForSavingToDB = uploadFolderName + "/" + currentYear + "/" + currentMonth + "/" + createImageUniqueName; } var document = new sm_doc(); document.name = imageNameForSavingToDB; document.tableID = tableID; document.docs_CreatedDate = DateTime.Now; document.value = recordID.ToString(); document.path = pathForSavingToDB; db.sm_doc.Add(document); await db.SaveChangesAsync(); documents.Add(document); } } } return(documents); }
public static async Task <List <sm_doc> > SaveUploadImage(THEntities db, int tableID, int recordID, List <string> base64) { List <sm_doc> documents = new List <sm_doc>(); if (base64 != null) { foreach (var image in base64) { using (MemoryStream ms = new MemoryStream(Convert.FromBase64String(image))) { string pathForSavingToDB = "", imageNameForSavingToDB = ""; using (Bitmap bm = new Bitmap(ms)) { //string path = ""; string year = DateTime.Now.Year.ToString(); string month = DateTime.Now.Month > 9 ? DateTime.Now.Month.ToString() : "0" + DateTime.Now.Month; /* * path = ConstantHelper.UPLOAD_FOLDER + @"\" + year + @"\" + month; * path = HttpContext.Current.Server.MapPath(@"~\" + path); * if (!Directory.Exists(path)) * Directory.CreateDirectory(path); * var createImageUniqueName = $"{tableID}_{recordID}_{DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss-fff")}.jpg"; * bm.Save(path + @"\" + createImageUniqueName); * bm.Save(@"C:\uploads\" + createImageUniqueName); */ var createImageUniqueName = $"{tableID}_{recordID}_{DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss-fff")}.jpg"; var uploadPath = WebConfigurationManager.AppSettings["uploadPath"].ToString(); uploadPath += ConstantHelper.UPLOAD_FOLDER + @"\" + year + @"\" + month; if (!Directory.Exists(uploadPath)) { Directory.CreateDirectory(uploadPath); } bm.Save(uploadPath + @"\" + createImageUniqueName); imageNameForSavingToDB = createImageUniqueName; pathForSavingToDB = $"{ConstantHelper.UPLOAD_FOLDER}/{year}/{month}/{createImageUniqueName}"; } var document = new sm_doc(); document.name = imageNameForSavingToDB; document.tableID = tableID; document.docs_CreatedDate = DateTime.Now; document.value = recordID.ToString(); document.path = pathForSavingToDB; db.sm_doc.Add(document); await db.SaveChangesAsync(); documents.Add(document); } } } return(documents); }
public static async Task <List <sm_doc> > SaveUploadImage(marielEntities db, int tableID, int recordID, List <string> base64) { List <sm_doc> documents = new List <sm_doc>(); if (base64 != null) { var i = 0; foreach (var image in base64) { var document = new sm_doc(); using (MemoryStream ms = new MemoryStream(Convert.FromBase64String(image))) { string pathForSavingToDB = "", imageNameForSavingToDB = ""; using (Bitmap bm = new Bitmap(ms)) { string path = ""; string year = DateTime.Now.Year.ToString(); string month = DateTime.Now.Month > 9 ? DateTime.Now.Month.ToString() : "0" + DateTime.Now.Month; path = ConstantHelper.UPLOAD_FOLDER + @"\" + year + @"\" + month; path = HttpContext.Current.Server.MapPath(@"~\" + path); if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } var createImageUniqueName = $"{tableID}_{recordID}_{DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss-fff")}_{i}.jpg"; //user "i" in order to prevent the same name bm.Save(path + @"\" + createImageUniqueName); imageNameForSavingToDB = createImageUniqueName; pathForSavingToDB = $"{ConstantHelper.UPLOAD_FOLDER}/{year}/{month}/{createImageUniqueName}"; } document.name = imageNameForSavingToDB; document.tableID = tableID; document.createdDate = DateTime.Now; document.value = recordID.ToString(); document.filePath = pathForSavingToDB; db.sm_doc.Add(document); await db.SaveChangesAsync(); documents.Add(document); } i++; } } return(documents); }