public ActionResult AddUpdateNews(DAL.News n)
        {
            try
            {
                NewsDAL obj    = new NewsDAL();
                int     newsId = 0;
                obj.AddUpdate(n, out newsId);
                HttpFileCollectionBase files = Request.Files;

                for (int i = 0; i < files.Count; i++)
                {
                    HttpPostedFileBase file             = files[i];
                    string             fileExt          = file.FileName.Split('.').LastOrDefault();
                    string             attachmentFolder = "doc";
                    if (fileExt == "jpg" || fileExt == "jpeg" || fileExt == "png")
                    {
                        attachmentFolder = "Images";
                    }
                    else
                    {
                        attachmentFolder = "Attachments";
                    }
                    string path = Server.MapPath("~/Files/News/" + attachmentFolder + "/" + newsId);
                    if (!Directory.Exists(path))
                    {
                        Directory.CreateDirectory(path);
                    }
                    var InputFileName  = Path.GetFileName(file.FileName);
                    var ServerSavePath = Path.Combine(path + "\\" + InputFileName);
                    //Save file to server folder
                    int count = 1;
checkIfFileExists:
                    if (System.IO.File.Exists(ServerSavePath))
                    {
                        ServerSavePath = Path.Combine(path + "\\" + InputFileName.Substring(0, InputFileName.LastIndexOf('.')) + count++ + InputFileName.Substring(InputFileName.IndexOf('.')));
                        goto checkIfFileExists;
                    }
                    else
                    {
                        file.SaveAs(ServerSavePath);
                    }
                }
                return(Json(new { result = "success", images = GetEventPhotos(newsId), newsId = newsId }, JsonRequestBehavior.AllowGet));
            }
            catch (Exception ex)
            {
                WriteLogs.Write(ex);
                return(Json(new { result = "error" }, JsonRequestBehavior.AllowGet));
            }
        }
Exemplo n.º 2
0
 public object AddUpdate(DAL.News obj, ref int newsId)
 {
     return(news.AddUpdate(obj, out newsId));
 }