Copied to clipboard

Flag this post as spam?

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


  • Darryl Godden 145 posts 197 karma points
    Jul 27, 2010 @ 10:16
    Darryl Godden
    0

    Delete File Upload Programmatically

    Hi all,

    I have a media type of 'Meeting' with several properties including 2 uploads. I want to programmatically remove from the media type and the disk. I can get the media type node easily:

    Media m = new Media(Convert.ToInt32(ddlDocList.SelectedValue));

    Can I now use m to delete the property?

    Thanks,

    Darryl

  • Darryl Godden 145 posts 197 karma points
    Jul 27, 2010 @ 11:56
    Darryl Godden
    0

    Hmm bit confused now, I've looked up the property delete in the Umbraco source:

    foreach (cms.businesslogic.property.Property p in this.getProperties)
                        if (p.PropertyType.DataTypeDefinition.DataType.Id == uploadField.Id &&
                            p.Value.ToString() != "" &&
                            System.IO.File.Exists(System.Web.HttpContext.Current.Server.MapPath(p.Value.ToString()))
                            )
                        {
    
                            System.IO.File.Delete(System.Web.HttpContext.Current.Server.MapPath(p.Value.ToString()));

    From which I created my code to this:

    if (System.IO.File.Exists(System.Web.HttpContext.Current.Server.MapPath(ddlDocList.SelectedValue.ToString())))
            {
                System.IO.File.Delete(System.Web.HttpContext.Current.Server.MapPath(ddlDocList.SelectedValue.ToString()));
            }   

    However this does not seem to do anything at all.

    Any help?

  • Darryl Godden 145 posts 197 karma points
    Jul 27, 2010 @ 12:26
    Darryl Godden
    0

    Cos the file didn't exist, duh!, Anyway I updated the code to this:

    string docPath = Server.MapPath("/media/" + ddlDocList.SelectedValue + "/" + ddlDocList.SelectedItem.ToString());
    
            if (System.IO.File.Exists(docPath))
            {
                System.IO.File.Delete(docPath);
            }             
    
            string docName = ddlDocList.SelectedItem.ToString();
    
            messages.Text = "The document <i>" + docName + "</i> was deleted successfully.";
            umbraco.library.RefreshContent();

    Which successfully removes the file from the file system, of course, but the filename remains on the media content. I tried p.delete on the property, but that appeared to delete the property node and caused a problem with that media. 

  • Darryl Godden 145 posts 197 karma points
    Jul 27, 2010 @ 13:44
    Darryl Godden
    0

    Still not working :(

    if (System.IO.File.Exists(docPath))
            {
                System.IO.File.Delete(docPath);
                Media m = new Media(Convert.ToInt32(HttpContext.Current.Session["nodeid"]));
                m.getProperty(HttpContext.Current.Session["property"].ToString()).Value = String.Empty;
                umbraco.library.UpdateDocumentCache(m.Id);            
            }   
  • Chris Gaskell 59 posts 142 karma points
    Jul 27, 2010 @ 16:28
    Chris Gaskell
    0

    Darryl,


    Whats the exception? (If any)

     

    Chris

  • Darryl Godden 145 posts 197 karma points
    Jul 27, 2010 @ 16:34
    Darryl Godden
    0

    Working now Chris, thanks for your response. Here's the code I managed to get it working with:

    if (System.IO.File.Exists(docPath))
            {
                System.IO.File.Delete(docPath);            
                Media m = new Media(Convert.ToInt32(ddlDocList.SelectedValue));
    
                nodeId = Convert.ToInt32(HttpContext.Current.Session["nodeid"]);
    
                if (nodeId == 1303 || nodeId == 1740 || nodeId == 1745 || nodeId == 1183 || nodeId == 1752)
                {
                    m.delete();
                }
                else
                {
                    m.getProperty(HttpContext.Current.Session["property"].ToString()).Value = String.Empty;
                    m.Save();
                }
                m.XmlGenerate(new System.Xml.XmlDocument());           
            }
  • Chris Gaskell 59 posts 142 karma points
    Jul 27, 2010 @ 17:10
    Chris Gaskell
    0

    Darryl,

    Not sure if you have multiple environments, if you do be careful the nodeId's in your if statement remain constant.

    We use courier here, so the nodeId's on the production box do not match that of the dev box.

    Thanks,

    Chris.

  • Darryl Godden 145 posts 197 karma points
    Jul 27, 2010 @ 17:12
    Darryl Godden
    0

    Thanks for the advice. They are the same fortunately. The reason for the programmatic deletion is we do not give the users access to the Umbraco back-end.

Please Sign in or register to post replies

Write your reply to:

Draft