how to save image using media picker through web api

Topics: Writing modules
Apr 30, 2013 at 7:17 AM
Hi All,

I have a module that is more or less a slideshow module which accepts images as items for the slideshow. Now, I have another module which is an extension of the first module but now as a service using web api. From my first module, I call the slideshow items as banner items which has a media picker for selecting an image but in the service module(2nd module), I am confused on how to save the images since the banner item record uses a media picker for the image. I noticed that the record itself has no field for image path but rather the media picker is the one saving the image details. Is there a way that I can manually let the media picker save an image from the post method of the service module?

Apr 30, 2013 at 9:31 AM
Yes, the MediaPickerField stores the relative path to the file. So what you need to do is get your hands on the content item to which the mediapicker field is attached. Once you have this content item, you can access its fields. In your second module, store the uploaded image to a folder in Media/[Somewhere], and store the relative path to that image in the Url property of the media picker field.

Tip: Orchard 1.x has a new Media Library module which exposes a MediaLibraryPickerField that enables the user to select multiple media items (where all media is now content). This enables a scenario where you can have a SlideShow content type with a single MediaLibraryPickerField, which you render as a slideshow.
This module comes with useful services to create and store media as well.
Disclaimer: the module is still under development, so there may be unresolved issues with it.