x First time here? Check out the FAQ

Come work for Umbraco - The Umbraco HQ are hiring Project managers, .NET developers and DevOps people!

  • Avatar55posts77karma

    Create new media using API

    Carl started this topic January 15, 2011 @ 12:20, Go directly to the topic solution

    Hi All

    I've managed successfully to create a new media item using the media API but I have a quiestion about the media file structure on the file system

    All content seems to be in one folder per node like /media/NNN/ where NNN is some id. When I create with the API I use the node ID, but just for comparison I created media using the umbraco interface, the folder number was a 3 digit number and not the node id.

    Was I wrong? what is this NNN number that the umbraco screens use for media ID

     

    thanks,


  • Jan Skovgaard posted this reply January 15, 2011 @ 12:24

    Hi Carl

    Could you please share some of the code you're using to achieve this? Then it might be much more easy to help you out :-)

    /Jan


  • Richard Soeteman posted this reply January 16, 2011 @ 07:43

    Hi Carl,

    Umbraco is using the property Id instead of the media Id. Which makes sense because media is stored using the upload datatype and that datatype can be used in Documents also. In the example below I have the media object and get the id from the property and use that id to store the file in the media folder 

    media.getProperty(

     

    "umbracoFile").Id

    Hope this helps you,

    Richard


  • Avatar55posts77karma
    Comment with ID: 60818
    Carl posted this reply January 16, 2011 @ 05:35

    Hi Richard

    Thanks for answering - That seems a little strange to me though becuase when I create the media, I need to assign the property umbracoFile to contain the path to the file - and if the path contains the ID of that property, I will need to create the property without knowing the path, then assign it and change it to include the ID. I can probably explain with a snippet below

     

                    Media newMedia;
                    newMedia = Media.MakeNew(filename, new MediaType(mediaType), admUser, folderNodeId);

                    string newName = filename; // This is calcualted earlier when file is uploaded
                    string newFolder = context.Server.MapPath("/media") + "\\" + newMedia.Id;
                    string newFile = newFolder + "\\" + newName;

                    // This is where I copy the file to the final location. At this stage I've not created the umbracoFile property as I need to know the path before hand

                    Directory.CreateDirectory(newFolder);

                    File.Move(savepath + "\\" + filename, newFile);

                    // Save filename and path to Umrbaco File
                    newMedia.getProperty("umbracoFile").Value = "/media/" + newMedia.Id + "/" + newName;

                    // Save type
                    newMedia.getProperty("umbracoExtension").Value = extension.Replace(".", "");

                    // Save size
                    FileInfo f = new FileInfo(newFile);
                    newMedia.getProperty("umbracoBytes").Value = f.Length;

                    // Generate Thumbnail for images - isImage is a bool set earlier in the processing, worked out based on extension
                    if (isImage)
                    {

                        // Resize the image here to create the thumbnails and calculate image height

                        newMedia.getProperty("umbracoWidth").Value = image.Width.ToString();
                        newMedia.getProperty("umbracoHeight").Value = image.Height.ToString();

                        // Generate thumbnails
                        string fileNameThumb = context.Server.MapPath("/media/") + newMedia.Id + "/" + newName.Replace(extension, "_thumb");
                    }

    Hope that makes sense. Essentially I can't find the property ID before I know the path, but the path contains the ID

    I can probably do something along the lines of create the property with a dummy path, find the ID and then move it again to the correct path with the final ID but that seems counter productive

     

    thanks for your help


  • Avatar2485posts6210karma
    Comment with ID: 60872
    Richard Soeteman posted this reply January 17, 2011 @ 08:59

    Hi,

    The property is already available when you create the media item. Only the value isn't assigned.

    Try  newMedia.getProperty("umbracoFile").Id before you assign the filename and you will get the id of the property.

    Cheers,

    Richard


  • Avatar55posts77karma
    Comment with ID: 60901
    Carl posted this reply January 17, 2011 @ 12:05

    Hey Richard

    Thanks. I'll give it a go and let you know the outcome


  • Avatar55posts77karma
    Comment with ID: 60910
    Carl posted this reply January 17, 2011 @ 12:39

    Hi Richard

    That worked a treat - thanks for the info


  • Pramod posted this reply January 17, 2011 @ 08:10

    can anyone give me sample code to integrate umbraco api for uploading content items

    Thanks

    Pramod


  • Avatar2posts162karma
    Comment with ID: 61021
    Julio Moreno posted this reply January 17, 2011 @ 10:48

    Hi Pramod, 

    Here is a little code that we are using to upload attachments to some node created at the fly. Hope it helps

    protected void MyUploadButton_Click(object sender, EventArgs e)
    
    if (UploadFile())
    {
                Document docComent = Document.MakeNew("SomeName", DocumentType.GetByAlias("YourDTAlias"), User.GetUser(0), idTicket);
    docComent.getProperty("ticket").Value = idTicket.ToString(); FileInfo TheFile = new FileInfo(MapPath("/") + "\\TicketAttachments\\" + fuAttachment.FileName); if (TheFile.Exists) { File.Move(MapPath("/") + "\\TicketAttachments\\" + fuAttachment.FileName, MapPath("/") + "\\TicketAttachments\\" + docComent.Id.ToString() + fuAttachment.FileName.Substring(fuAttachment.FileName.LastIndexOf("."))); docComent.getProperty("adjuntos").Value = "/TicketAttachments/" + docComent.Id.ToString() + fuAttachment.FileName.Substring(fuAttachment.FileName.LastIndexOf(".")); } docComent.Publish(autor); umbraco.library.UpdateDocumentCache(docComent.Id); }
    public bool UploadFile()
            {
                bool subio = false;
    
                if (fuAttachment.HasFile)
                {
                    if (fuAttachment.PostedFile.ContentLength <= 10485760)
                    {
                        if (ExtencionPermitida())
                        {
                            if (!Directory.Exists(@"C:\\Inetpub\\wwwroot\\catarsyslab.com\\Umbraco\\TicketAttachments\\"))
                            {
                                try
                                { Directory.CreateDirectory(@"C:\\Inetpub\\wwwroot\\catarsyslab.com\\Umbraco\\TicketAttachments\\"); }
                                catch
                                { lblMensaje.Text = "Error al subir archivo:<br/><br/> No se puede crear la carpeta."; }
                            }
                            fuAttachment.SaveAs(@"C:\\Inetpub\\wwwroot\\catarsyslab.com\\Umbraco\\TicketAttachments\\" + fuAttachment.FileName);
                            subio = true;
                        }
                        else
                        {
                            lblMensaje.Text = "El tipo de archivo que desea adjuntar no es permitido."
                                + "<br/>Archivos Comprimidos: (.zip)(.rar)"
                                + "<br/>Documentos: (.doc)(.xsl)(.pdf)(.ppt)"
                                + "<br/>Imagenes: (.jpg)(.gif)(.png)";
                        }
                    }
                    else
                    {
                        lblMensaje.Text = "Error al subir archivo:<br/> Archivo muy grande, seleccione otro o comprimalo (.zip)(.rar)";
                    }
                }
                else
                {
                    subio = true;
                }
                return subio;
            }
    
            public bool ExtencionPermitida()
            {
                bool Permitido = false;
                String Extencion = fuAttachment.FileName.Substring(fuAttachment.FileName.LastIndexOf(".") + 1).ToUpper();
    
                switch (Extencion)
                {
                    case "ZIP":
                    case "RAR":
                    case "DOC":
                    case "XLS":
                    case "PDF":
                    case "PPT":
                    case "JPG":
                    case "GIF":
                    case "PNG":
                        Permitido = true;
                        break;
                    default:
                        Permitido = false;
                        break;
                }
                return Permitido;
            }

  • Pramod posted this reply January 18, 2011 @ 07:41

    Thanks Julio,

    i need add contents using api


  • Avatar2485posts6210karma
    Comment with ID: 61049
    Richard Soeteman posted this reply January 18, 2011 @ 08:11

    Hi,

    If you want to import content items you might want to checkout CmsImport, no need to program.

    Cheers,

    Richard


Pages:

Please login or Sign up To post replies