示例#1
0
        public async Task <IActionResult> OnPostAsync(IFormFile[] images)
        {
            var results = new List <Dictionary <string, string> >();

            if (images == null || images.Length == 0)
            {
                return(RedirectToPage("Upload"));
            }

            IFormatProvider provider = CultureInfo.CreateSpecificCulture("en-US");

            foreach (var image in images)
            {
                if (image.Length == 0)
                {
                    return(RedirectToPage("Upload"));
                }

                var result = await _cloudinary.UploadAsync(new ImageUploadParams
                {
                    File = new FileDescription(image.FileName,
                                               image.OpenReadStream()),
                    Tags = Tags
                }).ConfigureAwait(false);

                var imageProperties = new Dictionary <string, string>();
                foreach (var token in result.JsonObj.Children())
                {
                    if (token is JProperty prop)
                    {
                        imageProperties.Add(prop.Name, prop.Value.ToString());
                    }
                }

                results.Add(imageProperties);

                await _context.Photos.AddAsync(new Photo
                {
                    Bytes        = (int)result.Bytes,
                    CreatedAt    = DateTime.Now,
                    Format       = result.Format,
                    Height       = result.Height,
                    Path         = result.Url.AbsolutePath,
                    PublicId     = result.PublicId,
                    ResourceType = result.ResourceType,
                    SecureUrl    = result.SecureUrl.AbsoluteUri,
                    Signature    = result.Signature,
                    Type         = result.JsonObj["type"]?.ToString(),
                    Url          = result.Url.AbsoluteUri,
                    Version      = int.Parse(result.Version, provider),
                    Width        = result.Width
                }).ConfigureAwait(false);
            }

            await _context.UploadResults.AddAsync(new UploadResult { UploadResultAsJson = JsonConvert.SerializeObject(results) }).ConfigureAwait(false);

            await _context.SaveChangesAsync().ConfigureAwait(false);

            return(RedirectToPage("UploadSucceeded"));
        }
示例#2
0
        public async Task OnPostAsync()
        {
            string content = null;

            using (var reader = new StreamReader(HttpContext.Request.Body))
            {
                content = await reader.ReadToEndAsync().ConfigureAwait(false);
            }

            if (IsNullOrEmpty(content))
            {
                return;
            }

            var             parsedResult = JsonConvert.DeserializeObject <ImageUploadResult>(content);
            IFormatProvider provider     = CultureInfo.CreateSpecificCulture("en-US");
            await _context.Photos.AddAsync(new Photo
            {
                CreatedAt    = parsedResult.CreatedAt,
                Format       = parsedResult.Format,
                Height       = parsedResult.Height,
                PublicId     = parsedResult.PublicId,
                ResourceType = parsedResult.ResourceType,
                SecureUrl    = parsedResult.SecureUrl.ToString(),
                Signature    = parsedResult.Signature,
                Type         = parsedResult.Type,
                Url          = parsedResult.Url.ToString(),
                Version      = int.Parse(parsedResult.Version, provider),
                Width        = parsedResult.Width
            }).ConfigureAwait(false);

            await _context.SaveChangesAsync().ConfigureAwait(false);
        }