public List <DiaryAttachmentsModel> GetDiaryAttachments(long id) { List <DiaryAttachmentsModel> attachmentList = new List <DiaryAttachmentsModel>(); using (SqlConnection con = new SqlConnection(cs)) { con.Open(); string query = @"SELECT [AutoId] ,[EntryId] ,[Media Type Id] ,[Path] ,[Label] FROM [Diary].[dbo].[Diary Attachments] WHERE [EntryId] = @id "; using (SqlCommand command = new SqlCommand(query, con)) { command.Parameters.Add(new SqlParameter("@id", id)); using (SqlDataReader reader = command.ExecuteReader()) { while (reader.Read()) { DiaryAttachmentsModel attachment = new DiaryAttachmentsModel(); attachment.Id = Convert.ToInt64(reader["AutoId"]); attachment.EntryId = Convert.ToInt64(reader["EntryId"]); attachment.MediaType = Convert.ToInt32(reader["Media Type Id"]); attachment.Path = reader["Path"].ToString(); attachment.Label = reader["Label"].ToString(); attachmentList.Add(attachment); } } } } return(attachmentList); }
public bool NewDiaryAttachmentsHelper(HttpPostedFileBase[] files, long id) { int year = @DateTime.Now.Year; int month = @DateTime.Now.Month; if (files.Length > 0) { try { // Get all files from Request object for (int i = 0; i < files.Length; i++) { HttpPostedFileBase file = files[i]; var extension = System.IO.Path.GetExtension(file.FileName); var label = file.FileName; var dbFileName = Guid.NewGuid().ToString() + extension; string dirPath = HostingEnvironment.MapPath("~/Files/" + year + "/" + month + "/"); if (!System.IO.Directory.Exists(dirPath)) { System.IO.Directory.CreateDirectory(dirPath); } // Get the complete folder path and store the file inside it. var dbPath = "Files/" + year + "/" + month + "/" + dbFileName; var filePath = HostingEnvironment.MapPath("~/" + dbPath); file.SaveAs(filePath); var dbAttachment = new DiaryAttachmentsModel { Id = id, Label = label, MediaType = 1, Path = dbPath }; NewDiaryAttachment(dbAttachment); } return(true); // Returns message that successfully uploaded //return Json("File Uploaded Successfully!"); } catch (Exception ex) { return(false); } } else { return(false); } }
public long NewDiaryAttachment(DiaryAttachmentsModel attachment) { SqlConnection con = new SqlConnection(cs); con.Open(); string query = @"INSERT INTO [Diary].[dbo].[Diary Attachments] ( [EntryId] ,[Media Type Id] ,[Path] ,[Label] ) output INSERTED.AutoId VALUES(@entryid,@mediatype,@path,@label)"; SqlCommand command = new SqlCommand(query, con); command.Parameters.Add(new SqlParameter("@entryid", attachment.Id)); command.Parameters.Add(new SqlParameter("@mediatype", attachment.MediaType)); command.Parameters.Add(new SqlParameter("@path", attachment.Path)); command.Parameters.Add(new SqlParameter("@label", attachment.Label)); Int64 Id = (Int64)command.ExecuteScalar(); con.Close(); return(Id); }