public async Task <IActionResult> Index() { ImageUploads imgUploads = new ImageUploads(); IndexVm listOfImages = new IndexVm() { Images = await _imageRepo.GetAllImages(StaticDetails.GetImages) //HttpContext.User.Identity.IsAuthenticated ? await _imageRepo.GetAllImageByIdAsync //(StaticDetails.GetSingleImage, UserId(), HttpContext.Session.GetString("JWToken")) : }; imgUploads.IndexViewModel = listOfImages; ViewBag.ImageSuccess = TempData["ImageSuccess"]; ViewBag.ImageExist = TempData["ImageExist"]; ViewBag.Deleted = TempData["ImageDeleted"]; if (User.Identity.IsAuthenticated) { //ViewData["ReturnUrl"] = returnUrl; //HttpContext.Session.Clear(); if (listOfImages.Images != null) { TempData["Authenticated"] = User.Identity.IsAuthenticated ? "Authenticated" : null; return(View(imgUploads)); } return(View(imgUploads)); } return(View(imgUploads)); }
public async Task <ImageUploads> UploadImageAsync(string url, T objToCreate, string token = "") { var request = new HttpRequestMessage(HttpMethod.Post, url); var image = new ImageUploads(); if (objToCreate != null) { request.Content = new StringContent(JsonConvert.SerializeObject(objToCreate), Encoding.UTF8, "application/json"); } else { image.imageExist.Add("Error Processing Request"); return(image); } var client = _clientFactory.CreateClient(); if (token != null && token.Length != 0) { client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token); } HttpResponseMessage response = await client.SendAsync(request); if (response.StatusCode == System.Net.HttpStatusCode.OK) { image.imageExist.Add("Success"); return(image); } var jsonString = await response.Content.ReadAsStringAsync(); return(JsonConvert.DeserializeObject <ImageUploads>(jsonString));; }
public bool DeleteImageById(ImageUploads image) { if (image != null) { _db.ImageUploads.Remove(image); return(Save()); } return(false); }
public ImageUploads ImageUploadExists(ImageUploadsDtos image, string username) { try { string execStatus = ""; var context = _db; var command = context.Database.GetDbConnection().CreateCommand(); { command.CommandText = "STP_IMAGE_EXISTS"; command.CommandType = System.Data.CommandType.StoredProcedure; var value = new ImageUploads(); foreach (var img in image.fileImages) { command.Parameters.Add(new SqlParameter("@Images", img.Image)); command.Parameters.Add(new SqlParameter("@Username", username)); command.Parameters.Add(new SqlParameter("@Status", SqlDbType.VarChar, 4000)); command.Parameters["@Status"].Direction = ParameterDirection.Output; context.Database.OpenConnection(); command.ExecuteNonQuery(); execStatus = Convert.ToString(command.Parameters["@Status"].Value); command.Parameters.Clear(); if (execStatus != null && execStatus != "") { value.imageExist.Add(execStatus); } } return(value); } } catch (Exception ex) { throw ex; } }
public bool CreateImage(ImageUploads imageUploads) { try { string execStatus = ""; using (var context = _db) using (var command = context.Database.GetDbConnection().CreateCommand()) { command.CommandText = "STP_ADD_IMAGE"; command.CommandType = System.Data.CommandType.StoredProcedure; foreach (var image in imageUploads.fileImages) { var imageUploadToDb = new ImageUploads() { Images = image.Image, ImageClassification = imageUploads.ImageClassification, UserId = imageUploads.UserId, ImageDescription = imageUploads.ImageDescription, DateCreated = imageUploads.DateCreated, ImageName = image.FileName, FileExtension = image.FileExtension, FileType = image.FileType, }; command.Parameters.Add(new SqlParameter("@Images", imageUploadToDb.Images)); command.Parameters.Add(new SqlParameter("@UserId", imageUploadToDb.UserId)); command.Parameters.Add(new SqlParameter("@ImageDescription", imageUploadToDb.ImageDescription)); command.Parameters.Add(new SqlParameter("@DateCreated", imageUploadToDb.DateCreated)); command.Parameters.Add(new SqlParameter("@ImageClassification", imageUploadToDb.ImageClassification)); command.Parameters.Add(new SqlParameter("@ImageName", imageUploadToDb.ImageName)); command.Parameters.Add(new SqlParameter("@FileExtension", imageUploadToDb.FileExtension)); command.Parameters.Add(new SqlParameter("@FileType", imageUploadToDb.FileType)); command.Parameters.Add(new SqlParameter("@Status", SqlDbType.VarChar, 4000)); command.Parameters["@Status"].Direction = ParameterDirection.Output; context.Database.OpenConnection(); command.ExecuteNonQuery(); execStatus = Convert.ToString(command.Parameters["@Status"].Value); //using (var result = command.ExecuteReader()) //{ // // do something with result //} //context.Database.CloseConnection(); command.Parameters.Clear(); } return(execStatus != "" ? true : false); } //using (var context = _db) //{ // foreach (var image in imageUploads.ImageUploadList) // { // var imageUploadToDb = new ImageUploads() // { // Images = image, // UserId = imageUploads.UserId, // ImageDescription = imageUploads.ImageDescription, // DateCreated = imageUploads.DateCreated, // ImageName = imageUploads.ImageName, // FileExtension = imageUploads.FileExtension, // FileType = imageUploads.FileType, // }; // context.Database.ExecuteSqlRaw("SET IDENTITY_INSERT [dbo].[ImageUploads] ON"); // _db.ImageUploads.Add(imageUploads); // _db.SaveChanges(); // // _db.Dispose(); // //Save(); // //var commandText = "INSERT Categories (CategoryName) VALUES (@CategoryName)"; // var commandText = "insert into ImageUploads(DateCreated,ImageClassification,FileExtension,ImageDescription,ImageId,UserId,Images,FileType) Values(@DateCreated,@ImageClassification,@FileExtension,@ImageDescription,@ImageId,@UserId,@Images,@FileType)"; // var name = new SqlParameter("@DateCreated", imageUploads.DateCreated); // var name = new SqlParameter("@DateCreated", imageUploads.DateCreated); // //var name = new SqlParameter("@CategoryName", "Test"); // //context.Database.ExecuteSqlCommand(commandText, name); // } //} } catch (Exception ex) { throw ex; } }
public bool UpdateImage(ImageUploads imageUploads) { _db.ImageUploads.Update(imageUploads); return(Save()); }
//public void Dispose() //{ // _db.Dispose(); // _db = null; //} public bool DeleteImage(ImageUploads imageUploads) { _db.ImageUploads.Remove(imageUploads); return(Save()); }
public async Task <IActionResult> Index(ImageUploads imageUploads) { try { var ms1 = new MemoryStream(); var files = HttpContext.Request.Form.Files; int count = 0; imageUploads.Username = HttpContext.Session.GetString("Username"); if (imageUploads.Username == null || imageUploads.Username == "") { return(View(nameof(AccessDenied))); } if (files.Count > 0) { count = files.Count; for (int i = 0; i < files.Count; i++) { using (var fs1 = files[i].OpenReadStream()) { switch (files[i].Name) { case "ImagesUploads": fs1.CopyTo(ms1); imageUploads.FileExtension = Path.GetExtension(files[i].FileName).ToLowerInvariant(); imageUploads.ImageName = files[i].FileName; imageUploads.FileType = files[i].ContentType; imageUploads.DateCreated = DateTime.Now; imageUploads.UserId = UserId(); imageUploads.ImagesUploads = ms1.ToArray(); imageUploads.Images = imageUploads.ImagesUploads; var fileImage = new FileImage() { FileName = imageUploads.ImageName, Image = imageUploads.Images, FileType = imageUploads.FileType, FileExtension = imageUploads.FileExtension }; imageUploads.fileImages.Add(fileImage); ms1 = new MemoryStream(); break; } } } } var uploadImageToDb = await _imageRepo.UploadImageAsync(StaticDetails.UploadImagePath, imageUploads, Token().ToString()); if (uploadImageToDb.imageExist[0] == "Success") { TempData["ImageSuccess"] = (count > 1) ? "Images uploaded successfully" : "Image uploaded successfully"; } TempData["ImageExist"] = uploadImageToDb.imageExist; return(RedirectToAction("Index")); } catch (Exception ex) { throw ex; } }