public Application.Images.ImageUploadResult AddImage(IFormFile file) { var cloudinaryUploadResult = new CloudinaryDotNet.Actions.ImageUploadResult(); if (file.Length > 0) { using (var stream = file.OpenReadStream()) { var uploadParams = new ImageUploadParams { File = new FileDescription(file.FileName, stream), Transformation = new Transformation().Height(500).Width(500).Crop("fill").Gravity("face") }; cloudinaryUploadResult = _cloudinary.Upload(uploadParams); } } if (cloudinaryUploadResult.Error != null) { throw new Exception(cloudinaryUploadResult.Error.Message); } return(new Application.Images.ImageUploadResult() { PublicId = cloudinaryUploadResult.PublicId, Url = cloudinaryUploadResult.SecureUrl.AbsoluteUri }); }
public async Task <IActionResult> peopleDetail(PeopleDetail model) { var user = await GetCurrentUserAsync(); if (user != null) { var role = await _userManager.GetRolesAsync(user); TempData["userRole"] = role[0]; string rid = HttpContext.Request.Form["restId"]; string sortOrder = HttpContext.Request.Form["sortOrder"]; string firstName = HttpContext.Request.Form["fname"]; string lastname = HttpContext.Request.Form["lname"]; string filepath = HttpContext.Request.Form["filePath"]; if (!string.IsNullOrEmpty(filepath)) { CloudinaryDotNet.Account account = new CloudinaryDotNet.Account("hkm2gz727", "654416183426452", "AZJIv_WvBo1Z7gkzN-uXFVg2_BE"); Cloudinary cloudinary = new Cloudinary(account); CloudinaryDotNet.Actions.ImageUploadParams uploadParams = new CloudinaryDotNet.Actions.ImageUploadParams(); uploadParams.File = new CloudinaryDotNet.Actions.FileDescription(filepath); CloudinaryDotNet.Actions.ImageUploadResult uploadResult = await cloudinary.UploadAsync(uploadParams); string url = cloudinary.Api.UrlImgUp.BuildUrl(String.Format("{0}.{1}", uploadResult.PublicId, uploadResult.Format)); model.imageUrl = url; } else { string imageurl = HttpContext.Request.Form["imageurl"]; model.imageUrl = imageurl; } model.userId = user.Id; model.firstName = firstName; model.lastName = lastname; model.restaurantId = Convert.ToInt32(rid); model.createdDate = DateTime.Now; model.modifiedDate = DateTime.Now; if (model.id != 0) { _context.PeopleDetail.Update(model); _context.SaveChanges(); } else { model.sortOrder = _context.PeopleDetail.Where(a => a.restaurantId == Convert.ToInt32(rid)).Max(a => a.sortOrder) + 1; _context.PeopleDetail.Add(model); _context.SaveChanges(); } return(Redirect("~/Admin/people?resturant=" + model.restaurantId)); } else { return(RedirectToAction("QuickRegister", "Account")); } }
public async Task <IActionResult> MenuItemDetails(MenuItemDetail model) { string restaurantId = HttpContext.Request.Form["restId"]; string menuCategoryId = HttpContext.Request.Form["menuCategoryId"]; var user = await GetCurrentUserAsync(); if (user != null) { var role = await _userManager.GetRolesAsync(user); TempData["userRole"] = role[0]; string filepath = HttpContext.Request.Form["filePath"]; if (!string.IsNullOrEmpty(filepath)) { CloudinaryDotNet.Account account = new CloudinaryDotNet.Account("hkm2gz727", "654416183426452", "AZJIv_WvBo1Z7gkzN-uXFVg2_BE"); Cloudinary cloudinary = new Cloudinary(account); CloudinaryDotNet.Actions.ImageUploadParams uploadParams = new CloudinaryDotNet.Actions.ImageUploadParams(); uploadParams.File = new CloudinaryDotNet.Actions.FileDescription(filepath); CloudinaryDotNet.Actions.ImageUploadResult uploadResult = await cloudinary.UploadAsync(uploadParams); string url = cloudinary.Api.UrlImgUp.BuildUrl(String.Format("{0}.{1}", uploadResult.PublicId, uploadResult.Format)); model.menuItemImageUrl = url; } else { string imageurl = HttpContext.Request.Form["imageurl"]; model.menuItemImageUrl = imageurl; } model.menuCategoryId = Convert.ToInt32(menuCategoryId); model.restaurantId = Convert.ToInt32(restaurantId); // if (ModelState.IsValid) // { if (model.menuItemId != 0) { model.createdDate = DateTime.Now; _context.MenuItemDetail.Update(model); _context.SaveChanges(); } else { model.createdDate = DateTime.Now; model.sortOrder = _context.MenuItemDetail.Where(a => a.restaurantId == Convert.ToInt32(restaurantId) && a.menuCategoryId == Convert.ToInt32(menuCategoryId)).Max(a => a.sortOrder) + 1; _context.MenuItemDetail.Add(model); _context.SaveChanges(); } // } return(Redirect("~/Admin/MenuItems" + "?resturant=" + model.restaurantId + "&itemtype=" + model.menuCategoryId)); //return RedirectToAction("MenuItems", "Admin"); } else { return(RedirectToAction("QuickRegister", "Account")); } }
public string Upload(string data) { Account account = new Account("dgy6x5krf", "949232162798767", "oxvzYd03K1i8lEIi5MA2ByIf590"); Cloudinary cloudinary = new Cloudinary(account); CloudinaryDotNet.Actions.ImageUploadParams uploadParams = new CloudinaryDotNet.Actions.ImageUploadParams() { File = new CloudinaryDotNet.Actions.FileDescription(data) }; CloudinaryDotNet.Actions.ImageUploadResult uploadResult = cloudinary.Upload(uploadParams); return(cloudinary.Api.UrlImgUp.BuildUrl(String.Format("{0}.{1}", uploadResult.PublicId, uploadResult.Format))); }
public async Task <IActionResult> AddPhotoForUser(int userId, [FromForm] PhotoForCreationDto photoForCreation) { if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } var user = await _datingRepository.GetUser(userId); var file = photoForCreation.File; var uploadResult = new CloudinaryDotNet.Actions.ImageUploadResult(); if (file != null && file.Length > 0) { using (var stream = file.OpenReadStream()) { var uploadParams = new CloudinaryDotNet.Actions.ImageUploadParams() { File = new FileDescription(file.Name, stream), Transformation = new Transformation().Width(500).Height(500).Crop("fill").Gravity("face") }; uploadResult = _cloudinary.Upload(uploadParams); } photoForCreation.Url = uploadResult.Uri.ToString(); photoForCreation.PublicId = uploadResult.PublicId.ToString(); var photo = _mapper.Map <Photo>(photoForCreation); if (!user.Photos.Any(u => u.IsMain)) { photo.IsMain = true; } user.Photos.Add(photo); if (await _datingRepository.SaveAll()) { return(CreatedAtRoute("GetPhoto", new { userId = userId, id = photo.Id }, _mapper.Map <PhotoForReturnDto>(photo))); } return(BadRequest("Could not add the photo")); } throw new System.Exception("No file has been received"); }
public async Task <IActionResult> AddPhotoForUser(int userId, PhotoForCreationDto photoForCreationDto) { var ids = int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value); if (userId != ids) { return(Unauthorized()); } var userFromRepo = await _repo.GetUser(userId); var file = photoForCreationDto.File; var uploadResult = new CloudinaryDotNet.Actions.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); } } photoForCreationDto.Url = uploadResult.Uri.ToString(); photoForCreationDto.PublicId = uploadResult.PublicId; var photo = _mapper.Map <Photo>(photoForCreationDto); if (!userFromRepo.Photos.Any(u => u.IsMain)) { photo.IsMain = true; } userFromRepo.Photos.Add(photo); if (await _repo.SaveAll()) { var photoToReturn = _mapper.Map <PhotoForReturnDto>(photo); return(CreatedAtRoute("GetPhoto", new { photo.Id }, photoToReturn)); } return(BadRequest("Could not add the photo")); }
public JsonResult Upload() { List<ImageUploadResult> list = new List<ImageUploadResult>(); JsonResult trem = new JsonResult(); string fileName = ""; ImageUploadResult uploadResult = new ImageUploadResult(); ImageUploadResult uploadResult2 = new ImageUploadResult(); ImageUploadParams uploadParams = new ImageUploadParams(); ImageUploadParams uploadParams2 = new ImageUploadParams(); Account account = new Account( "aniknaemm", "173434464182424", "p3LleRLwWAxpm9yU3CHT63qKp_E"); CloudinaryDotNet.Cloudinary cloudinary = new CloudinaryDotNet.Cloudinary(account); foreach (string file in Request.Files) { var upload = Request.Files[file]; if (upload != null) { fileName = System.IO.Path.GetFileName(upload.FileName); upload.SaveAs(Server.MapPath("~/" + fileName)); uploadParams = new ImageUploadParams() { File = new FileDescription(Server.MapPath("~/" + fileName)), PublicId = User.Identity.Name + fileName, }; } } foreach (string file in Request.Form) { var upload = Request.Form[file]; if (upload != null) { string x = upload.Replace("data:image/png;base64,", ""); byte[] imageBytes = Convert.FromBase64String(x); MemoryStream ms = new MemoryStream(imageBytes, 0, imageBytes.Length); ms.Write(imageBytes, 0, imageBytes.Length); System.Drawing.Image image = System.Drawing.Image.FromStream(ms, true); image.Save(Server.MapPath("~/img.png"), System.Drawing.Imaging.ImageFormat.Png); uploadParams2 = new ImageUploadParams() { File = new FileDescription(Server.MapPath("~/img.png")), PublicId = User.Identity.Name + fileName +"demotevators" }; } } uploadResult = cloudinary.Upload(uploadParams); list.Add(uploadResult); uploadResult2 = cloudinary.Upload(uploadParams2); list.Add(uploadResult2); System.IO.File.Delete(Server.MapPath("~/" + fileName)); System.IO.File.Delete(Server.MapPath("~/img.png")); return Json(list, JsonRequestBehavior.AllowGet); }
public ActionResult Save(string t, string l, string h, string w, string fileName) { try { // Calculate dimensions var top = Convert.ToInt32(t.Replace("-", "").Replace("px", "")); var left = Convert.ToInt32(l.Replace("-", "").Replace("px", "")); var height = Convert.ToInt32(h.Replace("-", "").Replace("px", "")); var width = Convert.ToInt32(w.Replace("-", "").Replace("px", "")); // Get file from temporary folder var fn = Path.Combine(Server.MapPath(MapTempFolder), Path.GetFileName(fileName)); // ...get image and resize it, ... var img = new WebImage(fn); img.Resize(width, height); // ... crop the part the user selected, ... img.Crop(top, left, img.Height - top - AvatarStoredHeight, img.Width - left - AvatarStoredWidth); // ... delete the temporary file,... System.IO.File.Delete(fn); // ... and save the new one. var newFileName = Path.GetFileName(fn); img.Save(Server.MapPath("~/" + newFileName)); Account account = new Account( "lifedemotivator", "366978761796466", "WMYLmdaTODdm4U6VcUGhxapkcjI" ); ImageUploadResult uploadResult = new ImageUploadResult(); CloudinaryDotNet.Cloudinary cloudinary = new CloudinaryDotNet.Cloudinary(account); var uploadParams = new ImageUploadParams() { File = new FileDescription(Server.MapPath("~/" + newFileName)), PublicId = User.Identity.Name + newFileName, }; uploadResult = cloudinary.Upload(uploadParams); System.IO.File.Delete(Server.MapPath("~/" + newFileName)); UrlAvatar = uploadResult.Uri.ToString(); return Json(new { success = true, avatarFileLocation = UrlAvatar }); } catch (Exception ex) { return Json(new { success = false, errorMessage = "Unable to upload file.\nERRORINFO: " + ex.Message }); } }
public JsonResult Upload() { JsonResult trem = new JsonResult(); ImageUploadResult uploadResult = new ImageUploadResult(); foreach (string file in Request.Files) { var upload = Request.Files[file]; if (upload != null) { Account account = new Account( "demcloud", "713675365492318", "1mGY2GOZR0FQ-qivH-p8BRsUZCs"); CloudinaryDotNet.Cloudinary cloudinary = new CloudinaryDotNet.Cloudinary(account); // получаем имя файла string fileName = System.IO.Path.GetFileName(upload.FileName); // сохраняем файл в папку Files в проекте upload.SaveAs(Server.MapPath("~/" + fileName)); var uploadParams = new ImageUploadParams() { File = new FileDescription(Server.MapPath("~/" + fileName)), PublicId = User.Identity.Name + fileName, Tags = "special, for_homepage" }; uploadResult = cloudinary.Upload(uploadParams); System.IO.File.Delete(Server.MapPath("~/" + fileName)); } } return Json(uploadResult, JsonRequestBehavior.AllowGet); }
public async Task<ActionResult> SaveDemotivatorToCloud(string tag, Demotivators model) { string fileName = ""; ImageUploadResult uploadResult = new ImageUploadResult(); ImageUploadParams uploadParams = new ImageUploadParams(); Account account = new Account( "lifedemotivator", "366978761796466", "WMYLmdaTODdm4U6VcUGhxapkcjI" ); CloudinaryDotNet.Cloudinary cloudinary = new CloudinaryDotNet.Cloudinary(account); var upload = Request.Files[0]; fileName = System.IO.Path.GetFileName(upload.FileName); upload.SaveAs(Server.MapPath("~/" + fileName)); var TargetPath = Server.MapPath("~/" + fileName); uploadParams = new ImageUploadParams() { File = new FileDescription(Server.MapPath("~/" + fileName)), PublicId = User.Identity.Name + fileName + DateTime.Now.Hour + "" + DateTime.Now.Minute + "" + DateTime.Now.Second, }; uploadResult = cloudinary.Upload(uploadParams); System.IO.File.Delete(Server.MapPath("~/" + fileName)); System.IO.File.Delete(Server.MapPath("~/img.png")); var OriginalUrl = uploadResult.Uri.ToString(); uploadParams = new ImageUploadParams() { File = new FileDescription(path + "MyPicture.png"), PublicId = User.Identity.Name + DateTime.Now.Hour + "" + DateTime.Now.Minute + "" + DateTime.Now.Second, }; uploadResult = cloudinary.Upload(uploadParams); System.IO.File.Delete(path + "MyPicture.png"); var ModifiedUrl = uploadResult.Uri.ToString(); SaveDemotivatorToDatabase(model.Name, model.FirstString , model.SecondString, OriginalUrl, ModifiedUrl); SaveTag(tag, db.Demotivators.ToList().Last().Id); return Redirect(Request.UrlReferrer.ToString()); }
public IHttpActionResult PutImage(int id) { if (HttpContext.Current.Request.Files.AllKeys.Any()) { var httpPostedFile = HttpContext.Current.Request.Files["file"]; bool folderExists = Directory.Exists(HttpContext.Current.Server.MapPath("~/UploadedDocuments")); if (!folderExists) Directory.CreateDirectory(HttpContext.Current.Server.MapPath("~/UploadedDocuments")); var fileSavePath = Path.Combine(HttpContext.Current.Server.MapPath("~/UploadedDocuments"), httpPostedFile.FileName); httpPostedFile.SaveAs(fileSavePath); if (File.Exists(fileSavePath)) { Account account = new Account("dvnnqotru", "252251816985341", "eqxRrtlVyiWA5-WCil_idtLzP6c"); Cloudinary cloudinary = new Cloudinary(account); var uploadParams = new ImageUploadParams() //upload to cloudinary { File = new FileDescription(fileSavePath), Transformation = new Transformation().Crop("fill").Width(720).Height(480) }; var uploadResult = new ImageUploadResult(); try { uploadResult = cloudinary.Upload(uploadParams); } catch { return StatusCode(HttpStatusCode.ExpectationFailed); } System.IO.File.Delete(fileSavePath); try { var coupon = db.Coupons.Find(id); coupon.PictureUrl = "http://res.cloudinary.com" + uploadResult.Uri.AbsolutePath; db.Entry(coupon).State = EntityState.Modified; db.SaveChanges(); return Ok(coupon.PictureUrl); } catch (Exception e) { } } //// http://www.codeproject.com/Tips/900200/SFTP-File-Upload-Using-ASP-NET-Web-API-and-Angular } return BadRequest(); }
//Post : Admin/RestaurantDetails/ public async Task <IActionResult> RestaurantDetails(RestaurantDetail model) { string ddBrakTimeFrom = HttpContext.Request.Form["ddBrakTimeFrom"]; string ddBrakTimeTo = HttpContext.Request.Form["ddBreakTimingTO"]; var user = await GetCurrentUserAsync(); if (user != null) { var role = await _userManager.GetRolesAsync(user); TempData["userRole"] = role[0]; string aa = HttpContext.Request.Form["cats"]; string filename = HttpContext.Request.Form["file"]; string filepath = HttpContext.Request.Form["filePath"]; if (!string.IsNullOrEmpty(filepath)) { CloudinaryDotNet.Account account = new CloudinaryDotNet.Account("hkm2gz727", "654416183426452", "AZJIv_WvBo1Z7gkzN-uXFVg2_BE"); Cloudinary cloudinary = new Cloudinary(account); CloudinaryDotNet.Actions.ImageUploadParams uploadParams = new CloudinaryDotNet.Actions.ImageUploadParams(); uploadParams.File = new CloudinaryDotNet.Actions.FileDescription(filepath); CloudinaryDotNet.Actions.ImageUploadResult uploadResult = await cloudinary.UploadAsync(uploadParams); string url = cloudinary.Api.UrlImgUp.BuildUrl(String.Format("{0}.{1}", uploadResult.PublicId, uploadResult.Format)); model.mainImageUrl = url; } else { string imageurl = HttpContext.Request.Form["imageurl"]; model.mainImageUrl = imageurl; } model.timing1 = model.timing1; model.timing2 = model.timing2; if (model.id != 0) { model.userId = model.userId; model.categoryIds = aa; model.modifiedDate = DateTime.Now; _context.RestaurantDetail.Update(model); _context.SaveChanges(); } else { model.userId = user.Id; model.categoryIds = aa; model.createdDate = DateTime.Now; model.modifiedDate = DateTime.Now; model.sortOrder = _context.RestaurantDetail.Max(a => a.sortOrder) + 1; _context.RestaurantDetail.Add(model); _context.SaveChanges(); } var restId = _context.RestaurantDetail.Max(a => a.id); MenuCategoryDetail objmenucats = new MenuCategoryDetail(); objmenucats.createdDate = DateTime.Now; objmenucats.isEnabled = true; objmenucats.parentMenuCategoryId = null; objmenucats.menuCategoryDesc = null; objmenucats.menuCategoryName = "Breakfast"; objmenucats.modifiedDate = DateTime.Now; objmenucats.restaurantId = restId; objmenucats.sortOrder = 1; _context.MenuCategoryDetail.Add(objmenucats); _context.SaveChanges(); MenuCategoryDetail objmenucats1 = new MenuCategoryDetail(); objmenucats1.createdDate = DateTime.Now; objmenucats1.isEnabled = true; objmenucats.parentMenuCategoryId = null; objmenucats1.menuCategoryDesc = null; objmenucats1.menuCategoryName = "Lunch"; objmenucats1.modifiedDate = DateTime.Now; objmenucats1.restaurantId = restId; objmenucats1.sortOrder = 2; _context.MenuCategoryDetail.Add(objmenucats1); _context.SaveChanges(); MenuCategoryDetail objmenucats2 = new MenuCategoryDetail(); objmenucats2.createdDate = DateTime.Now; objmenucats2.isEnabled = true; objmenucats2.menuCategoryDesc = null; objmenucats.parentMenuCategoryId = null; objmenucats2.menuCategoryName = "Dinner"; objmenucats2.modifiedDate = DateTime.Now; objmenucats2.restaurantId = restId; objmenucats2.sortOrder = 3; _context.MenuCategoryDetail.Add(objmenucats2); _context.SaveChanges(); return(RedirectToAction("Index", "Admin")); } else { return(RedirectToAction("QuickRegister", "Account")); } }