Exemplo n.º 1
0
        public async Task <IActionResult> Put([FromForm] NationalstrategyViewModel model, long id)
        {
            var nationalstrategy = _context.Nationalstrategies.Find(id);

            nationalstrategy.Title = model.Title;

            var filesname = model.namefile;
            var random    = RandomString(15);

            System.Console.WriteLine("1 : " + model.namefile);
            //ตรวจสอบว่ามี Folder Upload ใน wwwroot มั้ย
            if (!Directory.Exists(_environment.WebRootPath + "/assets" + "//NationalstrategyFile//"))
            {
                Directory.CreateDirectory(_environment.WebRootPath + "/assets" + "//NationalstrategyFile//"); //สร้าง Folder Upload ใน wwwroot
            }

            ////var BaseUrl = url.ActionContext.HttpContext.Request.Scheme;
            //// path ที่เก็บไฟล์
            var filePath = _environment.WebRootPath + "/assets" + "//NationalstrategyFile//";

            if (model.files != null)
            {
                System.Console.WriteLine("1.2 : " + model.namefile);
                foreach (var formFile in model.files.Select((value, index) => new { Value = value, Index = index }))
                ////foreach (var formFile in data.files)
                {
                    string filePath2 = formFile.Value.FileName;
                    string filename  = Path.GetFileName(filePath2);
                    string ext       = Path.GetExtension(filename);

                    if (formFile.Value.Length > 0)
                    {
                        // using (var stream = System.IO.File.Create(filePath + formFile.Value.FileName))
                        using (var stream = System.IO.File.Create(filePath + random + ext))
                        {
                            await formFile.Value.CopyToAsync(stream);

                            filesname = random + ext;
                        }
                        System.Console.WriteLine("2 : " + filesname);
                    }
                }
            }
            nationalstrategy.File = filesname;
            _context.Entry(nationalstrategy).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
            _context.SaveChanges();
            System.Console.WriteLine("3 : ");
            return(Ok(nationalstrategy));
        }
Exemplo n.º 2
0
        public async Task <IActionResult> Post([FromForm] NationalstrategyViewModel model)
        {
            var date      = DateTime.Now;
            var filesname = "null";
            var random    = RandomString(15);

            //ตรวจสอบว่ามี Folder Upload ใน wwwroot มั้ย
            if (!Directory.Exists(_environment.WebRootPath + "/assets" + "//NationalstrategyFile//"))
            {
                Directory.CreateDirectory(_environment.WebRootPath + "/assets" + "//NationalstrategyFile//"); //สร้าง Folder Upload ใน wwwroot
            }

            ////var BaseUrl = url.ActionContext.HttpContext.Request.Scheme;
            //// path ที่เก็บไฟล์
            var filePath = _environment.WebRootPath + "/assets" + "//NationalstrategyFile//";


            foreach (var formFile in model.files.Select((value, index) => new { Value = value, Index = index }))
            ////foreach (var formFile in data.files)
            {
                string filePath2 = formFile.Value.FileName;
                string filename  = Path.GetFileName(filePath2);
                string ext       = Path.GetExtension(filename);

                if (formFile.Value.Length > 0)
                {
                    // using (var stream = System.IO.File.Create(filePath + formFile.Value.FileName))
                    using (var stream = System.IO.File.Create(filePath + random + ext))
                    {
                        await formFile.Value.CopyToAsync(stream);

                        filesname = random + ext;
                    }
                }
            }
            var nationalstrategydata = new Nationalstrategy
            {
                //Position = model.Position,
                //Prefix = model.Prefix,
                File      = filesname,
                Title     = model.Title,
                CreatedAt = date
            };

            _context.Nationalstrategies.Add(nationalstrategydata);
            _context.SaveChanges();
            return(Ok(nationalstrategydata));
        }