Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Sean Valentine 11 posts 100 karma points
    Jan 13, 2016 @ 15:13
    Sean Valentine
    0

    MediaService.Delete(Id) is deleting all media items not just the Id passed into it.

    Hi, I'm currently scratching my head at this problem on my app.

    I have an action that is supposed to delete a specific Media item but it ends up deleting all media items in the folder.

    This is my create action

    [HttpPost]
            [ValidateAntiForgeryToken]
            public ActionResult UploadDocument(int Id = 0, int MemberId = 0)
            {
                //Find Ticket in Database
                var TicketInfo = _db.WishList_Items.Find(Id);
    
                //Create the Media Service and find the root folder
                var ms = Services.MediaService;
                var rootFolder = ms.GetRootMedia();
                var CustomerDocumentsFolder = rootFolder.Where(x => x.Name.ToLower().Equals("customer documents")).First();
    
                //Create the Centre Name for the Centre Folder
                int CentreId = TicketInfo.Centre_Code_Id.Value;
                var Centre = Members.GetById(CentreId).Name;
                var CentreFolder = ms.GetChildren(CustomerDocumentsFolder.Id).Where(x => x.Name == Centre).FirstOrDefault();
    
                //If the centre folder does not exist, create it.
                if (CentreFolder == null)
                {
                    CentreFolder = ms.CreateMedia(Centre, CustomerDocumentsFolder, "Folder");
                    ms.Save(CentreFolder);
                }
    
                //If available find the ticket folder
                var TicketFolder = ms.GetChildren(CentreFolder.Id).Where(x => x.Name == Id.ToString()).FirstOrDefault();
    
                //If the ticket folder does not exist, create it.
                if (TicketFolder == null)
                {
                    TicketFolder = ms.CreateMedia(Id.ToString(), CentreFolder, "Folder");
                    ms.Save(TicketFolder);
                }
    
    
                //Start Upload
                bool isSavedSuccessfully = true;
                string fName = "";
                try
                {
                    foreach (string fileName in Request.Files)
                    {
                        HttpPostedFileBase file = Request.Files[fileName];
                        //Save file content goes here
                        fName = file.FileName;
                        if (file != null && file.ContentLength > 0)
                        {
                            var Filename = Path.GetFileName(file.FileName);
                            var fileSavePath = Server.MapPath("~/media/" + Centre + "/" + TicketFolder.Name);
    
                            if (!Directory.Exists(fileSavePath))
                            {
                                Directory.CreateDirectory(fileSavePath);
                            }
    
                            //Save the file at the specified path
                            file.SaveAs(fileSavePath + "/" + Filename);
                            FileInfo info = new FileInfo(fileSavePath + "/" + Filename);
                            string[] ext = Filename.Split('.');
    
                            //Register the path and file with Umbracos back end
                            var uploadedMedia = ms.CreateMedia(Filename, TicketFolder, "File");
                            uploadedMedia.SetValue("umbracoBytes", info.Length);
                            uploadedMedia.SetValue("umbracoExtension", ext[1]);
                            uploadedMedia.SetValue("umbracoFile", "/media/"  + Centre + "/" + TicketFolder.Name + "/" + Filename);
                            ms.Save(uploadedMedia);
                        }
    
                    }
    
                }
                catch (Exception ex)
                {
                    isSavedSuccessfully = false;
                }
    
    
                if (isSavedSuccessfully)
                {
                    return Json(new { Message = fName });
                }
                else
                {
                    return Json(new { Message = "Error in saving file" });
                }
            }
    

    This works fine, for clarification i'm using dropzone.js to upload the media items (images and pdf's mainly)

    The get and update actions work fine but the delete action should only delete the item given to it, but instead it deletes all items.

    [HttpGet]
            public ActionResult DeleteDocument(int Id = 0, int TicketId = 0)
            {
                if (Id != 0)
                {
                    var ms = ApplicationContext.Services.MediaService;
                    var DocumentItem = ms.GetById(Id);
                    ms.Delete(DocumentItem, Members.GetCurrentMemberId());
    
                    //Set the Deleted flag to true
                    ViewData["DocDeleted"] = true;
                }
    
                return RedirectToUmbracoPage(1060, "ticket=" + TicketId + "&doc=" + Id + "&tab=2");
            }
    

    am i doing something wrong?

  • Sean Valentine 11 posts 100 karma points
    Jan 15, 2016 @ 09:34
    Sean Valentine
    0

    Sorry to bump this to the top but i'm absolutely stumped on this one. I've tried a few ways to resolve it.

    The easiest way is to give each media item it's own folder and this seems to work, but i can't understand why housing groups of media items in the same folder would cause all of the to be deleted if only one ID is passed through.

    Any ideas?

  • Tom Steer 161 posts 596 karma points
    Jan 15, 2016 @ 10:07
    Tom Steer
    0

    Hi Sean,

    Possibly a silly question but is the Id you pass into the DeleteDocument method defiantly the Id of the media item and not it's parent folder Id?

    Tom

  • Sean Valentine 11 posts 100 karma points
    Jan 15, 2016 @ 10:19
    Sean Valentine
    0

    Hi Tom,

    It's not a silly question as i did think that myself,

    i wrote a test and made sure it was the correct ID by checking the back office and passing through the ID from the Media Section of the back office.

    It still has the same result. Because my project is written in Visual Studio when it creates a folder it doesn't "include" the folder in the project. That won't have anything to do with it will it?

Please Sign in or register to post replies

Write your reply to:

Draft