コード例 #1
0
        public async Task <IActionResult> AddPhoto([FromForm] CloudinaryPhoto cloudinaryPhoto)
        {
            var userId       = User.FindFirstValue(ClaimTypes.NameIdentifier);
            var file         = cloudinaryPhoto.File;
            var uploadResult = new ImageUploadResult();

            if (file.Length > 0)
            {
                using (var stream = file.OpenReadStream())
                {
                    var uploadParams = new ImageUploadParams()
                    {
                        File           = new FileDescription(file.Name, stream),
                        Transformation = new Transformation().Width(500).Height(500).Crop("fill").Gravity("face")
                    };

                    uploadResult = _cloudinary.Upload(uploadParams);
                }
            }

            cloudinaryPhoto.Url      = uploadResult.Uri.ToString();
            cloudinaryPhoto.PublicID = uploadResult.PublicId;
            UserPhoto userPhoto = new UserPhoto();

            userPhoto.ImgUrl    = cloudinaryPhoto.Url;
            userPhoto.PublicID  = cloudinaryPhoto.PublicID;
            userPhoto.UserID    = userId;
            userPhoto.IsDefault = true;
            await _context.UserPhotos.AddAsync(userPhoto);

            await _context.SaveChangesAsync();

            return(CreatedAtRoute("GetPhoto", new { id = userPhoto.UserPhotoID }, userPhoto));
        }
コード例 #2
0
ファイル: HomeController.cs プロジェクト: sangnvus/201609JS02
        public ActionResult Upload(HttpPostedFileBase file)
        {
            //Configuration
            Account account    = new Account("dsddvwiqz", "677568653855233", "_RCoQNjMqr8Nt7-FAgs5T_guiWM");
            var     cloudinary = new Cloudinary(account);

            if (file == null)
            {
                return(View());
            }
            //Upload Image
            var uploadParam = new ImageUploadParams()
            {
                File = new FileDescription(file.FileName, file.InputStream),
            };

            var uploadResult = cloudinary.Upload(uploadParam);

            CloudinaryPhoto cloudinaryPhoto = new CloudinaryPhoto()
            {
                Bytes        = (int)uploadResult.Length,
                CreatedAt    = DateTime.Now,
                Format       = uploadResult.Format,
                Height       = uploadResult.Height,
                Path         = uploadResult.Uri.AbsolutePath,
                PublicId     = uploadResult.PublicId,
                ResourceType = uploadResult.ResourceType,
                SecureUrl    = uploadResult.SecureUri.AbsoluteUri,
                Signature    = uploadResult.Signature,
                Type         = uploadResult.JsonObj["type"].ToString(),
                Url          = uploadResult.Uri.AbsoluteUri,
                Version      = Int32.Parse(uploadResult.Version),
                Width        = uploadResult.Width,
            };

            //Save photo (publicID) to MongoDB
            MongoPhoto mongoPhoto = new MongoPhoto();

            mongoPhoto.PublicId = uploadResult.PublicId;
            Context.Photos.Insert(mongoPhoto);

            //Display a full image
            return(PartialView("UploadSucceeded", new CloudinaryPhotoModel(cloudinary, cloudinaryPhoto)));
        }