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?
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:
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.
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);
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:
Now I want to write the content of the media element, which should be an image.
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!
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? RichHi 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:
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!
We are having this same issue, any resolution to this?
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:
For a complete example have a look at the CreateMedia method here: http://damp.codeplex.com/SourceControl/changeset/view/81602#1967018
Jeroen
Actually discovered a bug in the umbraco.webservices project under the MediaService.asmx.cs class.
Line 143 currently reads:
and should read:
Yes that seems to be the problem. Maybe you should report it on codeplex: http://umbraco.codeplex.com/WorkItem/Create
Jeroen
Submitted, feel free to vote up :-) http://umbraco.codeplex.com/workitem/30692
is working on a reply...