Ejemplo n.º 1
0
        public IActionResult AddPosts([FromForm] APostWithMultipleImageModel model)
        {
            List <MultipleImageData> multipleImageDataList = new List <MultipleImageData>();
            var files    = HttpContext.Request.Form.Files;
            var formData = Request.Form["formData"];
            var mpModel  = JsonConvert.DeserializeObject <APostWithMultipleImageModel>(formData);

            // mpModel.ImagePath = new List<string>();
            foreach (var item in files)
            {
                if (item != null)
                {
                    var dir = Path.Combine(env.WebRootPath, "Resource");
                    if (!Directory.Exists(dir))
                    {
                        Directory.CreateDirectory(dir);
                    }
                    var fileName = Guid.NewGuid().ToString() + "" + Path.GetFileName(item.FileName);
                    var filePath = Path.Combine(dir, fileName);
                    using (var stream = new FileStream(filePath, FileMode.Create))
                    {
                        item.CopyToAsync(stream).GetAwaiter().GetResult();
                        stream.FlushAsync().GetAwaiter().GetResult();
                    }

                    MultipleImageData multipleImageData = new MultipleImageData
                    {
                        ImagePathData = fileName
                    };
                    // mpModel.ImagePath.Add(fileName);
                    multipleImageDataList.Add(multipleImageData);
                    imageData.InsertIntoMultipleImageDataTable(multipleImageData);
                    imageData.Save();
                }
            }
            mpModel.MultipleImageData = multipleImageDataList;
            iposts.InsertIntoPostsTable(mpModel);
            iposts.Save();
            return(Ok(HttpStatusCode.OK));
        }