public async Task <IActionResult> PostImageAsync([FromBody] LabImage labImage)
        {
            labImage.Id = Guid.NewGuid();
            dbContext.LabImages.Add(labImage);
            await dbContext.SaveChangesAsync();

            return(Ok(labImage));
        }
        public async Task <IActionResult> PostImagesAsync(List <IFormFile> files)
        {
            var file = files.FirstOrDefault();

            if (file != null)
            {
                using (var memoryStream = new MemoryStream())
                {
                    await file.CopyToAsync(memoryStream);

                    // Upload the file if less than 2 MB
                    if (memoryStream.Length < 2097152)
                    {
                        LabImage labImage = new LabImage()
                        {
                            Id          = Guid.NewGuid(),
                            Title       = Path.GetFileName(file.FileName),
                            ContentType = Path.GetExtension(file.FileName).Replace(".", ""),
                            ContentSize = file.Length,
                            Content     = memoryStream.ToArray()
                        };

                        dbContext.LabImages.Add(labImage);

                        await dbContext.SaveChangesAsync();

                        return(Ok(labImage));
                    }
                    else
                    {
                        return(StatusCode(StatusCodes.Status400BadRequest, "The file is too large."));
                    }
                }
            }
            else
            {
                return(StatusCode(StatusCodes.Status400BadRequest, "No file submitted."));
            }
        }
Exemple #3
0
        public async Task <LabImage> PostImageAsync(LabImage image)
        {
            try
            {
                string payload  = JsonSerializer.Serialize(image);
                var    content  = new StringContent(payload, Encoding.Default, "application/json");
                var    response = await httpClient.PostAsync($"api/image", content);

                response.EnsureSuccessStatusCode();
                using var responseContent = await response.Content.ReadAsStreamAsync();

                {
                    image = await JsonSerializer.DeserializeAsync <LabImage>(responseContent);
                }
            }
            catch (Exception ex)
            {
                // Log the error
                Console.WriteLine($"Errore: {ex.Message}");
            }

            return(image);
        }