public ActionResult AddPhoto(string itemcode, HttpPostedFileBase files) { ViewBag.Message = ""; if (files != null) { tbl_images im = new tbl_images(); string path = System.IO.Path.Combine(Server.MapPath("~/fileImages"), System.IO.Path.GetFileName(files.FileName)); path = Server.MapPath("~/fileImages/" + itemcode); if (!Directory.Exists(path)) { DirectoryInfo di = Directory.CreateDirectory(path); } //var im = new tbl_images(); //db.images.Add(im); string ext = System.IO.Path.GetExtension(files.FileName); string newfilename = itemcode; string tosave = path + "/" + newfilename + ext; int i = 0; while (System.IO.File.Exists(tosave)) { i++; newfilename = itemcode + "_" + i; tosave = path + "/" + newfilename + ext; } files.SaveAs(tosave); im = new tbl_images(itemcode, itemcode + "/" + newfilename + ext, itemcode + "/" + newfilename + ext); dbi.images.Add(im); dbi.SaveChanges(); } return(RedirectToAction("ManagePhoto", new { @id = itemcode })); }
public APIResult <bool> Delete(int id) { //var accs = new BL.Accounts.Accounts(); using (MainEntities ctx = new MainEntities()) { tbl_images img = ctx.tbl_images.Where(a => a.id == id).FirstOrDefault(); if (img != null) { try { ctx.Entry(img).State = System.Data.Entity.EntityState.Deleted; System.IO.File.Delete(HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings["mediaServer_Path"] + img.large)); System.IO.File.Delete(HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings["mediaServer_Path"] + img.thumb)); System.IO.File.Delete(HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings["mediaServer_Path"] + img.meduim)); System.IO.File.Delete(HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings["mediaServer_Path"] + img.original)); } catch (Exception) { } return(APIResult <bool> .Success(true, "API_SUCCESS")); } } return(APIResult <bool> .Error(ResponseCode.BackendInternalServer, "API_ERROR_BAD")); }
public ActionResult DeleteConfirmed(int id) { tbl_images tbl_images = db.tbl_images.Find(id); db.tbl_images.Remove(tbl_images); db.SaveChanges(); return(RedirectToAction("Index")); }
public ActionResult Edit([Bind(Include = "id,model_name,model_id,model_tag,thumb,meduim,large,original,created_at,created_by")] tbl_images tbl_images) { if (ModelState.IsValid) { db.Entry(tbl_images).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } return(View(tbl_images)); }
public ActionResult Create([Bind(Include = "id,model_name,model_id,model_tag,thumb,meduim,large,original,created_at,created_by")] tbl_images tbl_images) { if (ModelState.IsValid) { db.tbl_images.Add(tbl_images); db.SaveChanges(); return(RedirectToAction("Index")); } return(View(tbl_images)); }
/// <summary> /// Get one image by ID /// </summary> /// <param name="id">Image ID</param> /// <returns>List Of tbl_mages</returns> public APIResult <tbl_images> Get(int id) { using (MainEntities ctx = new MainEntities()) { tbl_images img = ctx.tbl_images.Where(a => a.id == id).FirstOrDefault(); if (img != null) { return(APIResult <tbl_images> .Success(img, "API_SUCCESS")); } } return(APIResult <tbl_images> .Error(ResponseCode.BackendInternalServer, "Bad Request!")); }
// GET: tbl_images/Delete/5 public ActionResult Delete(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } tbl_images tbl_images = db.tbl_images.Find(id); if (tbl_images == null) { return(HttpNotFound()); } return(View(tbl_images)); }
public ActionResult Images(int id, HttpPostedFileBase file) { string path = System.IO.Path.Combine("~/Content/Images/" + file.FileName); file.SaveAs(Server.MapPath(path)); tbl_images obj = new tbl_images(); obj.iname = file.FileName.ToString(); obj.pid = id; db.tbl_images.Add(obj); db.SaveChanges(); return(RedirectToAction("Index")); }
public async Task <APIResult <LoginResponse> > Auth(string email, string password, string first_name, string last_name, HttpContext http, string pic = "", string network = "", string FirebaseUID = "") { using (DAL.MainEntities ctx = new DAL.MainEntities()) { //try //{ tbl_accounts dbuser = null; try { if (email != "") { dbuser = ctx.tbl_accounts.Include("sec_users").Where(a => a.email == email).FirstOrDefault(); } else if (FirebaseUID != "") { dbuser = ctx.tbl_accounts.Include("sec_users").Where(a => a.sec_users.firebase_uid == FirebaseUID).FirstOrDefault(); } if (dbuser == null) { dbuser = new tbl_accounts(); dbuser.email = email; dbuser.first_name = first_name; dbuser.last_name = last_name; dbuser.register_time = DateTime.Now; ctx.tbl_accounts.Add(dbuser); try { ctx.SaveChanges(); sec_users sec_user = new sec_users(); sec_user.pwd = password; sec_user.id = dbuser.id; sec_user.mail_verified = true; if (FirebaseUID != "") { sec_user.firebase_uid = FirebaseUID; } ctx.sec_users.Add(sec_user); ctx.SaveChanges(); } //catch (DbEntityValidationException e) //{ // return new APIResult<LoginResponse>(ResultType.fail, null, General.fetchEntityError(e)); //} catch (Exception ex) { return(APIResult <LoginResponse> .Error(ResponseCode.BackendDatabase, ex.Message + "save changes1")); } } } catch (Exception ex) { return(APIResult <LoginResponse> .Error(ResponseCode.BackendDatabase, ex.Message + "get dbuser")); } tbl_images img = ctx.tbl_images.Where(a => a.model_name == "tbl_accounts" && a.model_id == dbuser.id && a.model_tag == "main").FirstOrDefault(); if (pic != "" && img == null) { img = new tbl_images(); try { img.original = "/Storage/Original/" + DateTime.Now.ToString("yyyyMMddhhmmss") + "_" + network + ".jpg"; string imgPath = ConfigurationManager.AppSettings["mediaServer_Path"] + img.original.Replace("/", "\\"); img.large = img.original; img.thumb = img.original; img.meduim = img.original; img.model_id = dbuser.id; img.model_name = "tbl_accounts"; img.model_tag = "main"; System.Net.WebClient webClient = new System.Net.WebClient(); webClient.Encoding = System.Text.Encoding.UTF8; webClient.DownloadFile(pic, imgPath); ctx.tbl_images.Add(img); } catch (Exception ex) { // return APIResult<LoginResponse>.(ResultType.fail, null, ex.Message + "Save Image"); } try { ctx.SaveChanges(); } //catch (DbEntityValidationException e) //{ // return new APIResult<LoginResponse>(ResultType.fail, null, General.fetchEntityError(e)); //} catch (Exception ex) { // return new APIResult<LoginResponse>(ResultType.fail, null, ex.Message + "save changes2"); } } var returned = new LoginResponse { account = dbuser }; IPResult s = new IPResult(); string ip = ""; string agent = ""; IPResult iploc = new IPResult(); //if(HttpContext.Current == null) return new APIResult<LoginResponse>(ResultType.fail, null, "Null HTTPContext"); //if (http.Request == null) return APIResult<LoginResponse>.Error(ResponseCode., null, "Null HTTPRequest"); //if (http.Request.ServerVariables == null) return new APIResult<LoginResponse>(ResultType.fail, null, "Null ServerVariables"); //if (http.Request.ServerVariables.Count == 0) return new APIResult<LoginResponse>(ResultType.fail, null, "Empty ServerVariables"); //if (!http.Request.ServerVariables.AllKeys.Contains("REMOTE_ADDR")) return new APIResult<LoginResponse>(ResultType.fail, null, "REMOTE_ADDR Not in ServerVariables"); //if (!http.Request.ServerVariables.AllKeys.Contains("HTTP_USER_AGENT")) return new APIResult<LoginResponse>(ResultType.fail, null, "HTTP_USER_AGENT No in ServerVariables"); try { ip = http.Request.ServerVariables.Get("REMOTE_ADDR"); agent = http.Request.ServerVariables.Get("HTTP_USER_AGENT"); iploc = General.GetResponse("http://ip-api.com/json/" + ip); } catch (Exception ex) { return(APIResult <LoginResponse> .Error(ResponseCode.BackendServerRequest, ex.Message + "get location ip:" + ip + " agent:" + agent)); } try { //&& a.ip == ip && a.agent == agent var userSessions = ctx.sec_sessions.Where(a => a.user_id == dbuser.id && a.end_time == null).FirstOrDefault(); if (userSessions == null) { Sessions ses = new Sessions(); ses.Entity.user_id = dbuser.id; ses.Entity.ip = ip; ses.Entity.isp = iploc.isp; ses.Entity.lat = iploc.lat; ses.Entity.lon = iploc.lon; ses.Entity.timezone = iploc.timezone; ses.Entity.city = iploc.city; ses.Entity.country = iploc.country; ses.Entity.country_code = iploc.countryCode; ses.Entity.agent = agent; ctx.sec_sessions.Add(ses.Entity); ctx.SaveChanges(); dbuser.sec_users.sec_sessions = new List <sec_sessions>() { ses.Entity }; returned.token = ses.Entity.id; } else { returned.token = userSessions.id; } returned.roles = ctx.sec_users_roles.Include("sec_roles").Where(a => a.user_id == dbuser.id).Select(b => b.sec_roles.role_key).ToArray(); return(APIResult <LoginResponse> .Success(returned, "Login Success")); } catch (DbEntityValidationException e) { return(APIResult <LoginResponse> .Error(ResponseCode.BackendDatabase, General.fetchEntityError(e))); } catch (Exception ex) { return(APIResult <LoginResponse> .Error(ResponseCode.BackendDatabase, ex.Message + " Save Session")); } //} //catch (Exception ex) //{ // throw new Exception( ex.Message + "Auth"); //} } }
public ActionResult AddActions(string empno, string actionitemcode, HttpPostedFileBase imagefile, string remarks = "", string requestforreview = "") { string actioncode = get_acode(); string actionimage = ""; ////////////////////// if (imagefile != null) { if (imagefile.ContentLength > 0) { try { tbl_images im = new tbl_images(); string path = System.IO.Path.Combine(Server.MapPath("~/fileImages"), System.IO.Path.GetFileName(imagefile.FileName)); path = Server.MapPath("~/fileImages/" + actionitemcode + "/" + actioncode); if (!Directory.Exists(path)) { DirectoryInfo di = Directory.CreateDirectory(path); } //var im = new tbl_images(); //db.images.Add(im); string ext = System.IO.Path.GetExtension(imagefile.FileName); string newfilename = actioncode; string tosave = path + "/" + newfilename + ext; int i = 0; while (System.IO.File.Exists(tosave)) { i++; newfilename = actioncode + "_" + i; tosave = path + "/" + newfilename + ext; } actionimage = actionitemcode + "/" + actioncode + "/" + newfilename + ext; imagefile.SaveAs(tosave); im = new tbl_images(newfilename, actionimage, actionimage); dbi.images.Add(im); dbi.SaveChanges(); } catch { } } } //////////////// var ac = new tbl_actions(); ac.remarks = remarks; ac.itemcode = actionitemcode; ac.actioncode = actioncode; ac.actionimage = actionimage; ac.p_actioncode = "00000"; ac.empno = empno; db.d_actions.Add(ac); db.SaveChanges(); //check if request for review if (requestforreview == "forreview") { tbl_dataFindings d_item = db.dataFindings.Single(p => p.itemcode == actionitemcode); d_item.status = 2; db.SaveChanges(); } // get gi_code || areaincharge string gicode = db.dataFindings.Where(s => s.itemcode == actionitemcode).Select(s => s.areaincharge).Single(); //get list of incharge var loi = db.emaildept.Where(s => s.gi_code == gicode).ToList(); string[] eset; eset = new string[loi.Count]; if (loi.Count > 0) { int x = 0; foreach (var emp in loi) { try { string email = curuser.users.Single(p => p.empno == emp.empno).email.ToString(); eset[x] = email; x++; } catch (Exception e) { Response.Write(e.Message); } } } // get_emails if (eset.Length > 0) { SendEmail(eset, actionitemcode, remarks); } return(RedirectToAction("Details", new { @id = actionitemcode })); }
public ActionResult NewFindings(tbl_dataFindings tbl_datafindings, IEnumerable <HttpPostedFileBase> files) { if (ModelState.IsValid) { tbl_images im = new tbl_images(); string month = DateTime.Now.ToString("MM"); string year = String.Format("{0:X}", System.Convert.ToInt32(DateTime.Now.ToString("yy"))); string day = DateTime.Now.ToString("dd"); string reccount = db.dataFindings.Count().ToString(); if (System.Convert.ToInt32(reccount) < 10) { reccount = "00" + reccount; } else if (System.Convert.ToInt32(reccount) < 100) { reccount = "0" + reccount; } else if (System.Convert.ToInt32(reccount) >= 1000) { reccount = "0" + reccount; } reccount = reccount.Substring(reccount.Length - 3, 3); string ditemcode = year + month + day + reccount; tbl_datafindings.itemcode = ditemcode; if (files != null) { foreach (HttpPostedFileBase file in files) { if (file != null) { string path = System.IO.Path.Combine(Server.MapPath("~/fileImages"), System.IO.Path.GetFileName(file.FileName)); path = Server.MapPath("~/fileImages/" + ditemcode); if (!Directory.Exists(path)) { DirectoryInfo di = Directory.CreateDirectory(path); DirectoryInfo dthumb = Directory.CreateDirectory(path + "/thumb/"); } string ext = System.IO.Path.GetExtension(file.FileName); string newfilename = ditemcode; string tosave = path + "/" + newfilename + ext; int i = 0; while (System.IO.File.Exists(tosave)) { i++; newfilename = ditemcode + "_" + i; tosave = path + "/" + newfilename + ext; } file.SaveAs(tosave); string thumbdir; thumbdir = path + "/thumb/thumb_" + newfilename + ext; GenerateThumbnails(0.5, file.InputStream, thumbdir); tbl_datafindings.imagePath = ditemcode + "/" + newfilename + ext; im = new tbl_images(ditemcode, ditemcode + "/" + newfilename + ext, ditemcode + "/" + newfilename + ext); dbi.images.Add(im); dbi.SaveChanges(); } } } var department = db.areadept.Where(r => r.areaname == tbl_datafindings.areacode).Single(); tbl_datafindings.areaincharge = department.gi_code; tbl_datafindings.department = department.department; db.dataFindings.Add(tbl_datafindings); db.SaveChanges(); //email Incharge // get gi_code || areaincharge string gicode = department.gi_code; //get list of incharge var loi = db.emaildept.Where(s => s.gi_code == gicode).ToList(); string[] eset; eset = new string[loi.Count]; if (loi.Count > 0) { int x = 0; foreach (var emp in loi) { try { string email = curuser.users.Single(p => p.empno == emp.empno).email.ToString(); eset[x] = email; x++; } catch (Exception e) { Response.Write(e.Message); } } } // get_emails string mailmsg = "<strong>A new finding was found in your Area</strong>"; mailmsg = mailmsg + "<p>Please Click on the Link below to respond with the Finding</p>"; if (eset.Length > 0) { SendEmail(eset, ditemcode, mailmsg, "newFindings"); } ViewBag.Message = "Findings added successfully!"; return(RedirectToAction("Index")); } else { var classification = new SelectList(db.classification.Select(r => r.classification).Distinct().ToList()); ViewBag.classification = classification; var area = new SelectList(db.areadept.Select(r => r.areaname).Distinct().ToList()); ViewBag.areanames = area; ViewBag.Message = "Please Complete required fields"; return(View(tbl_datafindings)); } }
public static APIResult <tbl_images> SaveImageFromFile(HttpPostedFile postedFile, string model, int model_id, string model_tag = "main") { var u = APIRequest.User(HttpContext.Current.Request); if (postedFile != null && postedFile.ContentLength > 0) { int MaxContentLength = 1024 * 1024 * 1; //Size = 1 MB List <string> AllowedFileExtensions = new List <string> { ".jpg", ".gif", ".png", ".jpeg", ".bmp" }; var ext = postedFile.FileName.Substring(postedFile.FileName.LastIndexOf('.')); var extension = ext.ToLower(); if (!AllowedFileExtensions.Contains(extension)) { var message = string.Format("Please Upload image of type .jpg,.gif,.png."); return(APIResult <tbl_images> .Error(ResponseCode.UserNotAcceptable, message)); } else if (postedFile.ContentLength > MaxContentLength) { var message = string.Format("Please Upload a file upto 1 mb."); return(APIResult <tbl_images> .Error(ResponseCode.UserNotAcceptable, message)); } else { Random random = new Random(); var code = random.Next(100000, 999999); string fname = DateTime.Now.ToString("yyyyMMddHHmmss-") + code + postedFile.FileName.Replace(" ", "-"); string online_original = "/Storage/Original/" + fname; string online_thumb = online_original; // "/Storage/Thumb/" + fname; string online_medium = online_original; // "/Storage/Medium/" + fname; string online_large = online_original; // "/Storage/Large/" + fname; string original = ConfigurationManager.AppSettings["mediaServer_Path"] + online_original.Replace("/", "\\"); // HttpContext.Current.Server.MapPath("~" + online_original); postedFile.SaveAs(original); postedFile = null; //string thumb = BLL.Images.resize(original, 120, 120, "Thumb"); //string medium = BLL.Images.resize(original, 400, 400, "Medium"); //string large = BLL.Images.resize(original, 800, 800, "Large"); using (MainEntities ctx = new MainEntities()) { tbl_images img = new tbl_images(); img.large = online_large; img.meduim = online_medium; img.original = online_original; img.thumb = online_thumb; img.model_name = model; img.model_id = model_id; img.model_tag = model_tag; img.created_at = DateTime.Now; img.created_by = u.Entity.id; ctx.tbl_images.Add(img); ctx.SaveChanges(); return(APIResult <tbl_images> .Success(img, "Uploaded success.")); } } } return(APIResult <tbl_images> .Error(ResponseCode.BackendInternalServer, "Error while uploading!")); }
public ActionResult BatchUpload() { bool isSavedSuccessfully = true; int count = 0; string msg = ""; string fileName = ""; string fileExtension = ""; string filePath = ""; string fileNewName = ""; // here is obtain strong //int albumId = string.IsNullOrEmpty(Request.Params["hidAlbumId"]) // 0 : int.Parse(Request.Params["hidAlbumId"]); tbl_images ItmImg = new tbl_images(); try { string directoryPath = Server.MapPath("~/uploads/images"); if (!Directory.Exists(directoryPath)) { Directory.CreateDirectory(directoryPath); } foreach (string f in Request.Files) { HttpPostedFileBase file = Request.Files[f]; if (file != null && file.ContentLength > 0) { fileName = file.FileName; fileExtension = Path.GetExtension(fileName); fileNewName = Guid.NewGuid().ToString() + fileExtension; filePath = Path.Combine(directoryPath, fileNewName); file.SaveAs(filePath); Stream strm = file.InputStream; string path_Thumb = System.IO.Path.Combine(Server.MapPath("~/uploads/Thumbs"), fileNewName); ItmImg.imgL = "/uploads/Images/" + fileNewName; //path to large images ItmImg.imgS = "/uploads/Thumbs/" + fileNewName; // path to thumbnail images GenerateThumbnails(0.5, strm, path_Thumb); //here reducing the image by 50% db.tbl_images.Add(ItmImg); db.SaveChanges(); count++; } } } catch (Exception ex) { msg = ex.Message; isSavedSuccessfully = false; } return(Json(new { Result = isSavedSuccessfully, Count = count, Message = msg })); }