Copied to clipboard

Flag this post as spam?

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


  • v456 2 posts 22 karma points
    Nov 02, 2011 @ 12:14
    v456
    0

    Writing an image to an image media element using the MediaService

    Hello,

    I already found these two topics on how umbraco stores media elements. [1] [2]

    I'm able to create a new media element (Type: Image) using the Umbraco MediaService with the following code:

    MediaService.mediaServiceSoapClient MediaClient = new MediaService.mediaServiceSoapClient("mediaServiceSoap", string.Format("{0}umbraco/webservices/api/MediaService.asmx", ConnectionURL));
    MediaService.mediaCarrier item = new MediaService.mediaCarrier();
    /* set item properties like ParentId and TypeId ... */
    int newId = MediaClient.create(item, ConnectionUser, ConnectionPassword);

    Now I want to write the content of the media element, which should be an image.

    Image img = Image.FromFile(@"C:\myimage.png");
    MemoryStream ms = new MemoryStream();
    img.Save(ms, ImageFormat.Png);
    MediaClient.writeContents(newId, "myimage.png", ms.ToArray(), ConnectionUser, ConnectionPassword);

    The media element is created, but after I write the content to the element, the property item.MediaProperties[0].PropertyValue (should be "umbracoFile") shows "'/media/myfile.png", but when I check the file system, I can't locate myfile.png at this location. Also, Umbraco is not able to show/load the image in the web interface.

    When I upload files using the web interface, Umbraco stores the image to such locations: "/media/xxx/myfile.png" where xxx is a generated (?) number.

    Now, how can I upload images correctly ("write content") "into" a media element using the MediaService?

    Thanks in advance!

  • Rich Green 2246 posts 4006 karma points
    Nov 02, 2011 @ 12:52
    Rich Green
    0

    Hey,

    When Media gets added to Umbraco alot happens behind the scenes (creating a thumbnail, adding the directories etc.)

    Something like this:

    // Adds to media section in umbraco
                    string fileId = doc.getProperty("imageAlias").Id.ToString();
                    string directory = Server.MapPath(string.Format("/media/" + fileId + "/"));
                    string filef = fileUpload.FileName;
                    var ext = filef.Substring(filef.LastIndexOf(".") + 1, filef.Length - filef.LastIndexOf(".") - 1).ToLower();
                    var fileNameWOExt = filef.Substring(0, filef.Length - (ext.Length + 1));
                    string fullFilePath = directory + filename;
                    //Adds a thumb for viewing in umbraco
                    string fileNameThumb = _fullFilePath.Replace("." + ext, "thumb");
                    thumbBMP.Save(directory + fileNameWOExt + "thumb.jpg");
     
                    System.IO.FileInfo fi = new FileInfo(fullFilePath);
     
                    Media m = null;
                    m = Media.MakeNew(
                        fileNameWOExt, MediaType.GetByAlias("image"), User.GetUser(0), 1377); //last property is the id of parent folder
                    string mediaPath = "/media/" + fileId + "/" + fileUpload.FileName;
                    m.getProperty("umbracoWidth").Value = newWidth.ToString();
                    m.getProperty("umbracoHeight").Value = newHeight.ToString();
                    m.getProperty("umbracoFile").Value = mediaPath;
                    m.getProperty("umbracoExtension").Value = ext;
                    m.getProperty("umbracoBytes").Value = fi.Length.ToString();
     
    Does that help?
     
    Rich

  • v456 2 posts 22 karma points
    Nov 08, 2011 @ 14:39
    v456
    0

    Hi Rich, thank you very much for your response!

    My problem is probably the fileId mentioned in your example above. When I want to write content via the MediaService, I can just specify the following parameters:

    writeContents(int id, string filename, byte[] contents, string username, string password)

    In my example, this would look like this:

    MediaClient.writeContents(newId,"myimage.png", ms.ToArray(),ConnectionUser,ConnectionPassword);

    I assumed that this method changes the content of the media element with the Id "newId", but it does not. When I look up the media element using the web interface, it says that the content (value of umbracoFile) is located at "/media/myimage.png" (not like usually at "/media/xxx/myimage.png"), but it's not.

    Generally, I'm talking about the WebService API, not the API used internally by Umbraco.

    Thanks in advance!

  • Santanaf 6 posts 26 karma points
    Jan 26, 2012 @ 22:48
    Santanaf
    0

    We are having this same issue, any resolution to this? 

  • Jeroen Breuer 4861 posts 12138 karma points MVP 3x admin c-trib
    Jan 26, 2012 @ 22:55
    Jeroen Breuer
    1

    Hmm I've never tried the webservice API. I do know that the folder name is actually the propertyId. Here is what I use when I create media myself:

    Media media = Media.MakeNew(name, mediaType, adminUser, parentId);
    
    string sql = string.Format(@"
            select cpd.id as id from cmsPropertyData cpd
                inner join cmsPropertyType cpt on cpd.propertytypeid = cpt.Id
                inner join cmsDataType cdt on cpt.dataTypeId = cdt.nodeId
            where cpd.contentNodeId = {0}
            and cdt.controlId = '{1}'", media.Id, uploadField.Id);
    
    int propertyId = SqlHelper.ExecuteScalar<int>(sql);
    
    FileInfo file = new FileInfo(IOHelper.MapPath(SystemDirectories.Media + "/" + propertyId + "/" + fileName));
    
    if (!file.Directory.Exists)
    {
        //If the directory doesn't exist then create it.
        file.Directory.Create();
    }
    
    //Write the file to the media folder.
    File.WriteAllBytes(file.FullName, bytes);

    For a complete example have a look at the CreateMedia method here: http://damp.codeplex.com/SourceControl/changeset/view/81602#1967018

    Jeroen

  • Santanaf 6 posts 26 karma points
    Jan 27, 2012 @ 01:15
    Santanaf
    0

    Actually discovered a bug in the umbraco.webservices project under the MediaService.asmx.cs class.

    Line 143 currently reads:

    m.getProperty("umbracoFile").Value = SystemDirectories.Media + "/" + filename;'

    and should read:

    m.getProperty("umbracoFile").Value = SystemDirectories.Media + "/" + m.getProperty("umbracoFile").Id + "/" + filename;

                

                

  • Jeroen Breuer 4861 posts 12138 karma points MVP 3x admin c-trib
    Jan 27, 2012 @ 10:38
    Jeroen Breuer
    0

    Yes that seems to be the problem. Maybe you should report it on codeplex: http://umbraco.codeplex.com/WorkItem/Create

    Jeroen

  • Santanaf 6 posts 26 karma points
    Jan 27, 2012 @ 17:41
    Santanaf
    0

    Submitted, feel free to vote up :-) http://umbraco.codeplex.com/workitem/30692

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies