예제 #1
0
        public ActionResult Delete(int contentId)
        {
            CustomLogHelper logger = CustomLogHelper.logHelper;

            try
            {
                IContent parentContent = Services.ContentService.GetById(contentId);
                parentContent.Name = Guid.NewGuid().ToString();

                string fileFullName = parentContent.GetValue("attachmentUrl").ToString();

                var path = Server.MapPath("~" + fileFullName);


                if (System.IO.File.Exists(path) && Services.ContentService.UnPublish(parentContent))
                {
                    System.IO.File.Delete(path);
                    return(Json(new { status = "Success", message = "Attachment succesfuly deleted" }));
                }
                else
                {
                    return(Json(new { status = "Error", message = "Attachment could not be deleted" }));
                }
            }
            catch (Exception ex)
            {
                logger.Log(ex.ToString().Replace("\r\n", ""));
                return(Json(new { status = "Error", message = "Attachment(s) could not be deleted" }));
            }
        }
예제 #2
0
        public ActionResult Create(IEnumerable <HttpPostedFileBase> filesToUpload)
        {
            CustomLogHelper logger = CustomLogHelper.logHelper;

            //Taking request parameters from request
            parentId = Int32.Parse(Request["parentId"]);
            bool willExistedFileOverwrited = Convert.ToBoolean(Request["willExistedFileOverwrited"]);
            bool isThereExistedFile        = Convert.ToBoolean(Request["isThereExistedFile"]);


            //Iterating through files
            for (int i = 0; i < Request.Files.Count; i++)
            {
                // Individual file
                file     = Request.Files[i];
                fileName = Path.GetFileName(file.FileName);
                bool isFileValid = true;

                //File Size Validation
                if (file.ContentLength > 6000000)
                {
                    isFileValid = false;
                    failedFiles.Add(fileName, "Individual file size must be under 6MB !! ");
                }

                //File Type Validation
                if (!allowedFileTypes.Contains(file.ContentType))
                {
                    isFileValid = false;
                    failedFiles.Add(fileName, "File type is not supported, supported types: pdf, text, excel, csv, jpeg, png ");
                }


                //If folder does not exist for parentId then create it
                folder = Server.MapPath("~/App_Data/uploads/" + parentId);
                bool isFolderExists = Directory.Exists(folder);
                if (!isFolderExists)
                {
                    Directory.CreateDirectory(folder);
                }

                string path = Path.Combine(folder, fileName);

                if (isFileValid)
                {
                    try
                    {
                        //If there is existed attachment and the attachment exist in the path
                        if (isThereExistedFile && System.IO.File.Exists(path))
                        {
                            //attachment is going to be overwrited
                            if (willExistedFileOverwrited)
                            {
                                //Overwrite attachment
                                OverwriteAttachment();
                            }
                            else //Keeping existed attachment and creating new attachment
                            {
                                //Creating new attachment while keeping old attachment
                                KeepExistedAttachmentAddNew();
                            }
                        }
                        else //If attachment doesn't existed before
                        {
                            //Cretaing new attachment
                            AddNewAttachment();
                        }
                    }
                    catch (Exception ex)
                    {
                        logger.Log(ex.ToString().Replace("\r\n", ""));
                        string json = JsonConvert.SerializeObject(failedFiles, Formatting.Indented);
                        return(Json(new { status = "Error", message = "file(s) could not be uploaded", files = failedFiles }));
                    }
                }
            }

            if (failedFiles.Count > 0)
            {
                string json = JsonConvert.SerializeObject(failedFiles, Formatting.Indented);
                return(Json(new { status = "Error", message = "file(s) could not be uploaded", files = failedFiles }));
            }

            return(Json(new { status = "Success", message = "file(s) successfully uploaded" }));
        }