Skip to content

Instantly share code, notes, and snippets.

@AlexShkor
Created November 4, 2013 14:32
Show Gist options
  • Save AlexShkor/7303317 to your computer and use it in GitHub Desktop.
Save AlexShkor/7303317 to your computer and use it in GitHub Desktop.
[POST]
public JsonResult UploadImage(HttpPostedFileBase uploadedFile)
{
var cloudinary = new CloudinaryDotNet.Cloudinary(ConfigurationManager.AppSettings.Get("cloudinary_url"));
bool isValidImage;
if (uploadedFile != null)
{
isValidImage = uploadedFile.IsValidImage(250, 250);
}
else
{
// TODO: actions if close button pressed
return null;
}
if (isValidImage)
{
uploadedFile.InputStream.Seek(0, SeekOrigin.Begin);
var uploadParams = new CloudinaryDotNet.Actions.ImageUploadParams()
{
File = new CloudinaryDotNet.Actions.FileDescription("filename", uploadedFile.InputStream),
};
var uploadResult = cloudinary.Upload(uploadParams);
string avatarUrl = cloudinary.Api.UrlImgUp.Transform(new CloudinaryDotNet.Transformation().Width(500).Height(500).Crop("limit")).BuildUrl(String.Format("{0}.{1}", uploadResult.PublicId, uploadResult.Format));
return Json(new { avatarUrl = avatarUrl, avatarId = uploadResult.PublicId, avatarFormat = uploadResult.Format });
}
return Json();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment