Exemplo n.º 1
0
        public ActionResult SaveReferralDocument(ReferralDocument referralDocument)
        {
            try
            {
                using (var db = new StoreContext())
                {
                    int?cnt = db.ReferralDocuments.Where(p => p.ReferralDocumentId == referralDocument.ReferralDocumentId).Count();

                    string[] files = Directory.GetFiles(Server.MapPath("~/FileStorage/ReferralDocument/" + referralDocument.ReferralId.ToString()), referralDocument.DocumentGuid + ".*");
                    if (files != null)
                    {
                        foreach (var file in files)
                        {
                            referralDocument.DocumentURL = "~/FileStorage/ReferralDocument/" + referralDocument.ReferralId.ToString() + "/" + Path.GetFileName(file);
                        }
                    }

                    if (cnt == 0)
                    {
                        db.ReferralDocuments.Add(referralDocument);
                    }
                    else
                    {
                        db.ReferralDocuments.Attach(referralDocument);
                        db.Entry(referralDocument).State = EntityState.Modified;
                    }

                    db.SaveChanges();

                    //Clear folder from unbound files
                    string[] allFiles = Directory.GetFiles(Server.MapPath("~/FileStorage/ReferralDocument/" + referralDocument.ReferralId.ToString()));
                    foreach (string fileName in allFiles)
                    {
                        string a = Path.GetFileNameWithoutExtension(fileName);

                        int fileCnt = db.ReferralDocuments.Where(p => p.DocumentGuid.ToString() == a).Count();
                        if (fileCnt == 0)
                        {
                            if (System.IO.File.Exists(fileName))
                            {
                                System.IO.File.Delete(fileName);
                            }
                        }
                    }

                    return(Json("1", JsonRequestBehavior.AllowGet));
                }
            }
            catch (Exception ex)
            {
                return(Json(ex.Message, JsonRequestBehavior.AllowGet));
            }
        }
Exemplo n.º 2
0
 public ActionResult UpdateReferralDocument(int?id)
 {
     try
     {
         using (var db = new StoreContext())
         {
             this.OrganizeViewBugs(db);
             ReferralDocument item = db.ReferralDocuments.Find(id);
             ViewBag.fileName = Guid.NewGuid().ToString();
             return(View("ReferralDocumentTemplate", item));
         }
     }
     catch (Exception ex)
     {
         return(HttpNotFound(ex.Message));
     }
 }