Esempio n. 1
0
        public async Task <IActionResult> Create([FromForm] Other other)
        {
            try
            {
                if (other.File == null || other.File.Length <= 0)
                {
                    return(BadRequest("Image file is required"));
                }

                IList <string> allowedFileExtensions = new List <string> {
                    ".jpg", ".gif", ".png"
                };
                string extension = other.File.FileName.Substring(other.File.FileName.LastIndexOf('.')).ToLower();

                if (!allowedFileExtensions.Contains(extension))
                {
                    return(BadRequest("Not image file"));
                }

                string imageFolderPath = Path.Combine(Directory.GetCurrentDirectory(), "Images");
                string now             = DateTime.Now.ToString("yyyyMMdd-hhmmss.fff");
                string storedFileName  = $"{now}_{other.File.FileName}";

                using (FileStream fileStream = System.IO.File.Create(imageFolderPath + "/" + storedFileName))
                {
                    other.File.CopyTo(fileStream);
                    await fileStream.FlushAsync();
                }

                other.ImageUri = $"images/{storedFileName}";
                var createdOther = await otherServ.Create(other);

                return(Ok(createdOther));
            }
            catch (Exception ex)
            {
                Console.Error.WriteLine(ex.Message);
                return(StatusCode(500));
            }
        }