简体   繁体   中英

Programmatically create and store images retrieved as byte[] data from a web service as media items in Umbraco

I've been able to integrate some scheduled web service calls into my Umbraco site that uses the response from the web service to update some of the content on my site. I can now handle text and some various other content but my main query is how should I deal with images that are delivered from the web service in byte[] format?

For a little context, the site I am developing uses web service calls to retreive the details of a product which users of our desktop software have created on their machine. Each of these products is pulled via a web service call into my Umbraco site and created as an individual product page under the parent node of products.

Products > Product

Each product has several properties such as an ID, a name, notes and a collection of images. Once I have called my web service I am creating these pages using the following code:

//Construct Service Request
    svc = new StaticDataWebServiceSoapClient();
    var response = svc.GetProducts(ref compressionSoapHeader,ref managedUserHeader);

    //Umbraco Content Services
    int rootID = 1117;
    IContentService cs = ApplicationContext.Current.Services.ContentService;
    var remove = cs.GetChildren(rootID).Where(x => x.ContentType.Alias == "product");
    foreach (var child in remove)
    {
      cs.Delete(child);
    }
    foreach(var product in response){
      var item = cs.CreateContent(product.ContinentName, rootID, "product");
      //Set the properties using the SetValue method, passing in the alias of the property and the value to assign to it
      item.SetValue("continentId", product.Id);
      item.SetValue("continentName", product.ProductName);
      item.SetValue("continentNotes", product.Notes);
      foreach (var image in product.Images)
      {
        ??? destination.SetValue("ProductImages", image._Image); ???
        image.Caption;
        image.FileName;
        image.ImageId;
        image.KeywordID;
      }
      cs.SaveAndPublishWithStatus(item);
    }

As you can see from the code, each product has several images associated with it that I would also like to pull into the site and associate with the product page that is being created. How would I go about doing this? Would I need to use the Media Service and a specific datatype or would this structure fit easily into a multiple media picker?

You may find it easiest to loop through the images once you've retrieved them and create a new Media item for each of them, and then associate them with the product using a property based on something like the Multiple Media Picker datatype as you noted.

Because this data type stores it's values as a comma separated list of id's you could use something like the following:

        // Setup
        IContentService cs = ApplicationContext.Current.Services.ContentService;
        var mediaService = ApplicationContext.Current.Services.MediaService;
        int mediaRootId = 1111; // Replace with the id of your media parent folder


        // Replace the image looping logic with the following:

        // MultiMediaPicker stores it's values as a commma separated string of ids.
        string imageIds = string.Empty;

        foreach (var image in product.Images)
        {
            var fileName = image.FileName; // Assumes no path information, just the file name
            var ext = fileName.Substring(fileName.LastIndexOf('.') + 1).ToLower();

            if (!UmbracoConfig.For.UmbracoSettings().Content.DisallowedUploadFiles.Contains(ext))
            {
                var mediaType = Constants.Conventions.MediaTypes.File;

                if (UmbracoConfig.For.UmbracoSettings().Content.ImageFileTypes.Contains(ext))
                    mediaType = Constants.Conventions.MediaTypes.Image;

                var f = mediaService.CreateMedia(fileName, mediaRootId, mediaType);
                // Assumes the image._Image is a Stream - you may have to do some extra work here...
                f.SetValue(Constants.Conventions.Media.File, fileName, (Stream)image._Image); // Real magic happens here.

                mediaService.Save(f);

                imageIds += (imageIds.Length > 0 ? "," : "") + f.Id;
            }
        }

        // Get the relevant property on the new item and save the image list to it.
        var imagesProp = item.Properties.Where(p => p.Alias == "productImages").FirstOrDefault();
        imagesProp.Value = imageIds;

Note - I haven't tested this code out, so ymmv.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM