Copied to clipboard

Flag this post as spam?

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


  • Carl Sargunar 68 posts 90 karma points MVP 3x
    Jan 15, 2011 @ 12:20
    Carl Sargunar
    0

    Create new media using API

    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 11280 posts 23678 karma points MVP 10x admin c-trib
    Jan 15, 2011 @ 12:24
    Jan Skovgaard
    0

    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 4035 posts 12842 karma points MVP
    Jan 16, 2011 @ 07:43
    Richard Soeteman
    2

    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

  • Carl Sargunar 68 posts 90 karma points MVP 3x
    Jan 16, 2011 @ 17:35
    Carl Sargunar
    0

    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

  • Richard Soeteman 4035 posts 12842 karma points MVP
    Jan 17, 2011 @ 08:59
    Richard Soeteman
    1

    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

  • Carl Sargunar 68 posts 90 karma points MVP 3x
    Jan 17, 2011 @ 12:05
    Carl Sargunar
    0

    Hey Richard

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

  • Carl Sargunar 68 posts 90 karma points MVP 3x
    Jan 17, 2011 @ 12:39
    Carl Sargunar
    0

    Hi Richard

    That worked a treat - thanks for the info

  • Pramod 3 posts 23 karma points
    Jan 17, 2011 @ 20:10
    Pramod
    0

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

    Thanks

    Pramod

  • Julio Moreno 2 posts 163 karma points
    Jan 17, 2011 @ 22:48
    Julio Moreno
    1

    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 3 posts 23 karma points
    Jan 18, 2011 @ 07:41
    Pramod
    0

    Thanks Julio,

    i need add contents using api

  • Richard Soeteman 4035 posts 12842 karma points MVP
    Jan 18, 2011 @ 08:11
    Richard Soeteman
    0

    Hi,

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

    Cheers,

    Richard

  • Rob Watkins 369 posts 701 karma points
    Mar 03, 2011 @ 13:59
    Rob Watkins
    0

    This thread just saved me from early dementia, thankyou Richard!

Please Sign in or register to post replies

Write your reply to:

Draft