public static string ConvertImageToBase64(string srcImage, CommonConstants.DocumentType documentType, string documentId) { try { var imgBase64 = ""; var fullPath = GetFullPath(GetOriginalPath(srcImage, documentType)); if (File.Exists(fullPath)) { using (Image img = Image.FromFile(fullPath)) { using (MemoryStream ms = new MemoryStream()) { img.Save(ms, img.RawFormat); byte[] byteArr = ms.ToArray(); imgBase64 = Convert.ToBase64String(byteArr); } } } return(imgBase64); } catch (Exception ex) { throw ex; } }
public IList <FileInfoModel> GetAllFiles(string rootName, CommonConstants.DocumentType documentType = CommonConstants.DocumentType.Product, string documentId = null) { var result = new List <FileInfoModel>(); var rootPath = GetOriginalPath(rootName, documentType, documentId); var rootFolderInfo = GetFolderInfo(rootPath); var fullRootPath = GetDefaultPath(); var folders = rootFolderInfo.GetDirectories(); var files = rootFolderInfo.GetFiles(); foreach (var folder in folders) { result.AddRange(GetAllFiles(folder.FullName, documentType, documentId)); } foreach (var file in files) { result.Add(new FileInfoModel { FileName = file.Name, Path = file.FullName.Replace(fullRootPath, ""), DocumentType = documentType, DocumentId = documentId, Size = file.Length, Extension = file.Extension, CreatedDate = file.CreationTimeUtc, UpdatedDate = file.LastAccessTimeUtc }); } return(result); }
public string ProcessAndSaveBase64File(string base64String, CommonConstants.DocumentType documentType, string fileName) { try { var path = GetFullPath(GetOriginalPath("/", documentType)); string converted = base64String.Split(',')[1]; byte[] bytes = Convert.FromBase64String(converted); CreateFolderIfNotExist(path); var pathName = Path.Combine(path, string.Format("{0}_{1}", DateTime.UtcNow.ToString("ddMMyyyyHHmmss"), fileName)); File.WriteAllBytes(pathName, bytes); return(ReplaceDefaultPath(pathName).Replace("\\", "/")); } catch (Exception ex) { throw ex; } }
public static string GetOriginalPath(string path, CommonConstants.DocumentType documentType = CommonConstants.DocumentType.Product, string documentId = null) { var comparePath = FixPathBeforeCombine(path); if (comparePath.Contains(AppSettings.DefaultDocumentFolder) || comparePath.Contains(AppSettings.DefaultAvatarFolder)) { return(path); } var folder = AppSettings.DefaultDocumentFolder; if (documentType.Equals(CommonConstants.DocumentType.Avatar)) { folder = AppSettings.DefaultAvatarFolder + "/" + documentId; } return(Path.Combine(FixPathBeforeCombine(folder), FixPathBeforeCombine(path))); }
public string ProcessAndSaveBase64Img(string base64Img, CommonConstants.DocumentType documentType, string documentId) { try { var path = GetFullPath(GetOriginalPath("/", documentType)); byte[] bytes = Convert.FromBase64String(base64Img); using (MemoryStream ms = new MemoryStream(bytes, 0, bytes.Length)) { CreateFolderIfNotExist(path); var img = Image.FromStream(ms, true); path = Path.Combine(path, documentId + ".png"); img.Save(path, ImageFormat.Png); } return(ReplaceDefaultPath(path).Replace("\\", "/")); } catch (Exception ex) { throw ex; } }