Пример #1
0
        public static Task <string> GetUploadedFilePath(IFormFile mediaUrl, bool IsUseRoot = false)
        {
            string ymdPath, getExt, fileName, dirPath, filePath;

            getExt = Path.GetExtension(mediaUrl.FileName);
            var date = Env.GetTimeZoneInfo(DateTime.Now);

            ymdPath = "/" + date.Year + "/" + date.ToString("MMM") + "/" + date.Day + "/";
            //ymdPath = Path.Combine(date.Year.ToString(), date.ToString("MMM"), date.Day.ToString());
            fileName = date.ToString("ddMMMyyyyhhmmss") + getExt;
            dirPath  = "wwwroot/uploads" + ymdPath;
            //dirPath = Path.Combine("wwwroot", "uploads", ymdPath);
            var serverMap = AppDomain.CurrentDomain.BaseDirectory;

            if (Startup.StaticConfig["ApplicationSettings:IsDevelopment"].ToString() == "Yes")
            {
                serverMap = Directory.GetCurrentDirectory();
            }

            string dirCreatePath = Path.Combine(serverMap, dirPath);

            if (!Directory.Exists(dirCreatePath) && IsUseRoot == false)
            {
                Directory.CreateDirectory(dirCreatePath);
            }

            filePath = "wwwroot/uploads" + ymdPath + fileName;
            //filePath = Path.Combine("wwwroot", "uploads", ymdPath, fileName);

            var path = Path.Combine(serverMap, filePath);

            if (IsUseRoot)
            {
                path = Path.Combine(serverMap, "wwwroot/uploads", mediaUrl.FileName);
            }

            using (var stream = new FileStream(path, FileMode.Create))
            {
                mediaUrl.CopyTo(stream);
            }
            if (IsUseRoot)
            {
                return(Task.FromResult(mediaUrl.FileName));
            }
            else
            {
                return(Task.FromResult((ymdPath + fileName)));
            }
            //return Task.FromResult(Path.Combine(ymdPath,fileName));
        }