Esempio n. 1
0
        private static string CreateDirectory(FilesSaveOptions options)
        {
            if (string.IsNullOrWhiteSpace(options.Directory))
            {
                throw new($"{tipTitle} Directory cant be empty");
            }
            string savePath = options.CreateDateDirectory
                ? Path.Combine(Path.DirectorySeparatorChar.ToString(), FilesSaveSettings.RootFloder, options.Directory, DateTime.Now.ToString("yyyyMMdd"))
                : Path.Combine(Path.DirectorySeparatorChar.ToString(), FilesSaveSettings.RootFloder, options.Directory);
            var absolutePath = FilesSaveSettings.WebRootPath + savePath;

            if (!Directory.Exists(absolutePath))
            {
                _ = Directory.CreateDirectory(absolutePath);
            }
            return(absolutePath);
        }
Esempio n. 2
0
        public static string SaveFrom(string base64String, FilesSaveOptions options)
        {
            SettingsCheck();
            byte[] imgBytes;
            try
            {
                imgBytes = Convert.FromBase64String(base64String);
            }
            catch
            {
                throw new("cant convert to byte[] from this string,need Base64String");
            }
            ValidateSize(imgBytes.Length);
            string directory = CreateDirectory(options);
            string filePath  = !string.IsNullOrWhiteSpace(options.FileName)
                ? directory + Path.DirectorySeparatorChar.ToString() + options.FileName
                : $"{directory}{Path.DirectorySeparatorChar}{Guid.NewGuid()}.{options.FileExtension.Replace(".", "")}";

            using var stream = new FileStream(filePath, FileMode.OpenOrCreate);
            stream.Write(imgBytes, 0, imgBytes.Length);
            return(filePath);
        }