コード例 #1
0
        [RequestSizeLimit(4194304)] //4MB Upload limit
        public async Task <IActionResult> CreateFossilImage(FossilImage newImage, int fossilid)
        {
            //Check if our submit actually has an image, otherwise redirect back to site.
            if (newImage.image != null)
            {
                string wwwRootPath = _hostEnvironment.WebRootPath;
                string fileName    = Path.GetFileNameWithoutExtension(newImage.image.ImageFile.FileName); //Grabs string of file name
                string extension   = Path.GetExtension(newImage.image.ImageFile.FileName);                //Grabs string of file extension
                //Create new file name based on DateTime to avoid duplicate file names.
                fileName = fileName + DateTime.Now.ToString("yyyyMMddmmss") + extension;
                //Get path image will be saved to
                string path = Path.Combine(wwwRootPath + "/Image", fileName);
                //Put image in wwwroot folder
                using (var fileStream = new FileStream(path, FileMode.Create))
                {
                    await newImage.image.ImageFile.CopyToAsync(fileStream);
                }
                ImageModel addImage = new ImageModel {
                    FossilID  = fossilid,
                    ImageName = fileName,
                    ImageFile = newImage.image.ImageFile
                };
                dbContext.Add(addImage);
                await dbContext.SaveChangesAsync();

                return(Redirect($"../{fossilid}"));
            }
            else
            {
                return(Redirect($"../{fossilid}"));
            }
        }
コード例 #2
0
        public IActionResult GetFossil(int fossilid)
        {
            Fossil displayFossil = dbContext.Fossils
                                   .Include(fossil => fossil.AddedBy)
                                   .Include(fossil => fossil.FossilImages)
                                   .Include(fossil => fossil.UnearthedAt)
                                   .Include(fossil => fossil.LocatedAt)
                                   .FirstOrDefault(fossil => fossil.FossilID == fossilid);

            FossilImage displayData = new FossilImage {
                fossil = displayFossil
            };

            return(View("FossilDisplay", displayData));
        }