private void saveListImages(long headerId, IEnumerable <HttpPostedFileBase> imageFiles) { int sequence = 1; if (imageFiles != null && imageFiles.Count() > 0) { foreach (HttpPostedFileBase uploadedImg in imageFiles) { byte[] imageBytes = new byte[uploadedImg.ContentLength]; uploadedImg.InputStream.Read(imageBytes, 0, uploadedImg.ContentLength); TrImageAttachment theImage = new TrImageAttachment(); theImage.HeaderId = headerId; theImage.Image = imageBytes; theImage.Sequence = sequence; theImage.cCreated = HttpContext.User.Identity.Name; theImage.dCreated = DateTime.Now; theImage.cLastUpdated = HttpContext.User.Identity.Name; theImage.dLastUpdated = DateTime.Now; theImage.RowState = System.Data.DataRowState.Added; trImageAttachmentDataAccess.Update(ref theImage); sequence++; } } }
public HttpResponseMessage GetArticleImage(long imageId) { using (PatuhEntities db = new PatuhEntities()) { TrImageAttachment image = db.TrImageAttachments.Where(x => x.Id == imageId).FirstOrDefault(); try { var response = new HttpResponseMessage(); //response.Content = new StreamContent(fileStream); response.Content = new ByteArrayContent(image.Image); response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("inline"); response.Content.Headers.ContentDisposition.FileName = "image"; response.Content.Headers.ContentType = new MediaTypeHeaderValue("image/jpg"); response.Content.Headers.ContentLength = image.Image.Length;//fileLength; return(response); } catch (Exception e) { Console.WriteLine(e); throw; } } }
public void GetSingleQueryUpdate(TrImageAttachment item, ref List <SqlCommand> ArraySQLCmd) { TrImageAttachment itm = item; //DALInfo.AssignedInfo(ref itm); UpdateQuery(itm, ArraySQLCmd); }
public void GetBatchQueryUpdate(List <TrImageAttachment> objDomain, ref List <SqlCommand> ArraySQLCmd) { foreach (TrImageAttachment item in objDomain) { TrImageAttachment itm = item; DALInfo.AssignedInfo(ref itm); UpdateQuery(itm, ArraySQLCmd); } }
public ActionResult downloadImage(int id) { TrImageAttachment theImage = trImageAttachmentDataAccess.GetAttachmentById(id); Image img = null; if (theImage != null) { return(File(theImage.Image, "image/jpg", "image.jpg")); } return(null); }
public void UpdateQuery(TrImageAttachment item, List <SqlCommand> ArraySQLCmd) { SqlCommand cmd = null; if (item.RowState == DataRowState.Added) { //using (SqlConnection sqlCon = new SqlConnection(DALInfo.ConnectionString)) //{ //sqlCon.Open(); cmd = new SqlCommand("up_InsertTrImageAttachment"); //cmd.Connection = sqlCon; cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.Add("@HeaderId", SqlDbType.BigInt).Value = item.HeaderId; cmd.Parameters.Add("@Image", SqlDbType.VarBinary).Value = item.Image ?? (object)DBNull.Value; cmd.Parameters.Add("@Sequence", SqlDbType.Int).Value = item.Sequence; cmd.Parameters.Add("@cCreated", SqlDbType.VarChar, 20).Value = item.cCreated ?? (object)DBNull.Value; cmd.Parameters.Add("@cLastUpdated", SqlDbType.VarChar, 20).Value = item.cLastUpdated == null ? (object)DBNull.Value : item.cLastUpdated; //cmd.ExecuteNonQuery(); //} } else if (item.RowState == DataRowState.Modified) { cmd = new SqlCommand("up_UpdateTrImageAttachment"); //cmd.Connection = new SqlConnection(DALInfo.ConnectionString); cmd.CommandType = CommandType.StoredProcedure; //cmd.Parameters.Add("@moduleid", SqlDbType.VarChar, 5).Value = item.ModuleID; cmd.Parameters.Add("@Id", SqlDbType.BigInt).Value = item.Id; cmd.Parameters.Add("@HeaderId", SqlDbType.BigInt).Value = item.HeaderId; cmd.Parameters.Add("@Image", SqlDbType.VarBinary).Value = item.Image ?? (object)DBNull.Value; cmd.Parameters.Add("@Sequence", SqlDbType.Int).Value = item.Sequence; cmd.Parameters.Add("@cLastUpdated", SqlDbType.VarChar, 20).Value = item.cLastUpdated == null ? (object)DBNull.Value : item.cLastUpdated; //cmd.ExecuteNonQuery(); } else if (item.RowState == DataRowState.Deleted) { cmd = new SqlCommand("up_DeleteTrImageAttachment"); cmd.CommandType = CommandType.StoredProcedure; //cmd.Parameters.Add("@moduleid", SqlDbType.VarChar, 5).Value = item.ModuleID; cmd.Parameters.Add("@Id", SqlDbType.BigInt).Value = item.Id; //cmd.Parameters.Add("@userroleid", SqlDbType.VarChar, 20).Value = item.UserRoleID; //cmd.Parameters.Add("@modusrid", SqlDbType.VarChar, 20).Value = item.ModUsrID == null ? (object)DBNull.Value : item.ModUsrID; //cmd.ExecuteNonQuery(); } if (cmd != null) { ArraySQLCmd.Add(cmd); } }
private void deleteListImages(long headerId, long[] retainedImageIds) { IList <TrImageAttachment> listImageObjs = trImageAttachmentDataAccess.GetListAttachmentByHeaderId(headerId); if (listImageObjs != null && listImageObjs.Count > 0) { foreach (TrImageAttachment img in listImageObjs) { if (retainedImageIds == null || !retainedImageIds.Contains(img.Id)) { TrImageAttachment theImage = img; theImage.RowState = System.Data.DataRowState.Deleted; trImageAttachmentDataAccess.Update(ref theImage); } } } }
public TransactionResult Update(ref TrImageAttachment item) { List <SqlCommand> ArraySQLCmd = new List <SqlCommand>(); TransactionDB TransDB = new TransactionDB(DALInfo); TransactionResult ObjTransResult = default(TransactionResult); GetSingleQueryUpdate(item, ref ArraySQLCmd); ObjTransResult = TransDB.BatchUpdate(ArraySQLCmd); if (ObjTransResult.Result == 1 && !item.RowState.Equals(DataRowState.Deleted)) { item.RowState = DataRowState.Unchanged; } return(ObjTransResult); }
public TrImageAttachment GetAttachmentById(long Id) { SqlConnection conn = new SqlConnection(DALInfo.ConnectionString); SqlCommand cmd = new SqlCommand("up_RetrieveTrImageAttachment", conn); TrImageAttachment objTbl = new TrImageAttachment(); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@Id", Id); cmd.Parameters.AddWithValue("@HeaderId", 0); SqlDataReader da = default(SqlDataReader); try { cmd.Connection.Open(); da = cmd.ExecuteReader(); if (da.HasRows) { objTbl = MoveDataToCollection(da)[0]; } else { return(objTbl); } } catch (Exception) { throw; } finally { da.Close(); conn.Close(); cmd.Dispose(); } return(objTbl); }
public List <TrImageAttachment> MoveDataToCollectionDomain(DataTable dataTable) { List <TrImageAttachment> TrImageAttachmentList = new List <TrImageAttachment>(); for (int i = 0; i <= dataTable.Rows.Count - 1; i++) { TrImageAttachment objTrImageAttachment = new TrImageAttachment(); objTrImageAttachment.Id = long.Parse(dataTable.Rows[i]["Id"].ToString().Trim()); objTrImageAttachment.HeaderId = long.Parse(dataTable.Rows[i]["HeaderId"].ToString().Trim()); objTrImageAttachment.Image = (byte[])dataTable.Rows[i]["Image"]; objTrImageAttachment.Sequence = int.Parse(string.IsNullOrEmpty(dataTable.Rows[i]["Story"].ToString()) ? "0" : dataTable.Rows[i]["Story"].ToString()); objTrImageAttachment.cCreated = dataTable.Rows[i]["cCreated"].ToString().Trim(); objTrImageAttachment.dCreated = Mapper <DateTime>(dataTable.Rows[i]["dCreated"]); objTrImageAttachment.cLastUpdated = dataTable.Rows[i]["cLastUpdated"].ToString().Trim(); objTrImageAttachment.dLastUpdated = Mapper <DateTime>(dataTable.Rows[i]["dLastUpdated"]); objTrImageAttachment.RowState = DataRowState.Unchanged; TrImageAttachmentList.Add(objTrImageAttachment); } return(TrImageAttachmentList); }
private List <TrImageAttachment> MoveDataToCollection(SqlDataReader MyReader, Boolean isCustom = false) { List <TrImageAttachment> msArticleList = new List <TrImageAttachment>(); while (MyReader.Read()) { var columns = new List <string>(); for (int i = 0; i < MyReader.FieldCount; i++) { columns.Add(MyReader.GetName(i)); } TrImageAttachment objTrImageAttachment = new TrImageAttachment(); objTrImageAttachment.Id = long.Parse(MyReader["Id"].ToString().Trim()); objTrImageAttachment.HeaderId = long.Parse(MyReader["HeaderId"].ToString().Trim());; objTrImageAttachment.Image = (byte[])MyReader["Image"]; objTrImageAttachment.Sequence = int.Parse(string.IsNullOrEmpty(MyReader["Sequence"].ToString()) ? "0" : MyReader["Sequence"].ToString()); objTrImageAttachment.cCreated = MyReader["cCreated"].ToString().Trim(); objTrImageAttachment.dCreated = Mapper <DateTime>(MyReader["dCreated"]); objTrImageAttachment.cLastUpdated = MyReader["cLastUpdated"].ToString().Trim(); objTrImageAttachment.dLastUpdated = Mapper <DateTime>(MyReader["dLastUpdated"]); objTrImageAttachment.RowState = DataRowState.Unchanged; int fields = MyReader.FieldCount; int visi = MyReader.VisibleFieldCount; if (isCustom == true) { objTrImageAttachment.RowNumber = Convert.ToInt64(MyReader["RowNumber"]); objTrImageAttachment.TotalRecord = Convert.ToInt64(MyReader["TotalRecord"]); } msArticleList.Add(objTrImageAttachment); } return(msArticleList); }
// POST api/values public object Post() { JsonResultViewModel result = new JsonResultViewModel(); result.status = true; result.message = "News successfully updated"; result.messageCode = "S"; var httpRequest = HttpContext.Current.Request; long id = string.IsNullOrEmpty(httpRequest["Id"]) ? 0 : long.Parse(httpRequest["Id"]); string title = httpRequest["title"]; string story = httpRequest["story"]; string location = httpRequest["location"]; double latitude = string.IsNullOrEmpty(httpRequest["latitude"]) ? 0 : double.Parse(httpRequest["latitude"]); double longitude = string.IsNullOrEmpty(httpRequest["longitude"]) ? 0 : double.Parse(httpRequest["longitude"]); string userId = httpRequest["userId"]; try { using (PatuhEntities db = new PatuhEntities()) { TrArticle article = db.TrArticles.Where(x => x.Id == id).FirstOrDefault(); if (article == null) { article = new TrArticle(); article.cCreated = userId; article.dCreated = DateTime.Now; db.TrArticles.AddObject(article); } article.Category = "ARTICLE"; article.Title = title; article.Story = story; article.GPSLocation = location; article.GPSLong = longitude; article.GPSLat = latitude; article.cLastUpdated = userId; article.dLastUpdated = DateTime.Now; db.SaveChanges(); try { if (httpRequest.Files.Count > 0) { IList <TrImageAttachment> currentImages = db.TrImageAttachments.Where(x => x.HeaderId == article.Id).ToList(); if (currentImages != null && currentImages.Count > 0) { foreach (TrImageAttachment dbImg in currentImages) { db.TrImageAttachments.DeleteObject(dbImg); } } string extention = ""; string guid = ""; string[] supportedTypes = new string[] { "jpg", "jpeg", "bmp", "png" }; int fileSequence = 0; foreach (string file in httpRequest.Files) { var postedFile = httpRequest.Files[file]; Type fileType = postedFile.GetType(); if (postedFile != null) { if (postedFile.FileName != "") { byte[] theImage = new byte[postedFile.ContentLength]; extention = (Path.GetExtension(postedFile.FileName).TrimStart('.')).ToLower(); if (supportedTypes.Contains(extention)) { postedFile.InputStream.Read(theImage, 0, postedFile.ContentLength); TrImageAttachment imageAtt = new TrImageAttachment();// db.TrImageAttachments.Where(x => x.Id == id).FirstOrDefault(); imageAtt = new TrImageAttachment(); imageAtt.HeaderId = article.Id; imageAtt.cCreated = userId; imageAtt.dCreated = DateTime.Now; imageAtt.Sequence = ++fileSequence; imageAtt.Image = theImage; imageAtt.cLastUpdated = userId; imageAtt.dLastUpdated = DateTime.Now; db.TrImageAttachments.AddObject(imageAtt); db.SaveChanges(); } } } } } } catch (Exception e) { result.status = false; result.message = e.Message; result.messageCode = "Error in saving article"; return(result); } } } catch (Exception e) { result.status = false; result.message = e.Message; result.messageCode = "Error in saving Article"; } return(result); }