Can anybody tell me how I upload a picture (the actual data) via the media API. I've been searching for a while and can't seem to find anything for the v6 API.
It looks like in the legacy v4 you can use the mediaServiceSoapClients
writeContents (haven't tried it though) but how do you get the raw data of an Image up in the media library?
Cheers for the link Søren, but I have looked the documentation through and can't figure out from it where the raw data of the image goes.
Let say I make a method that downloads that umb logo from the top:
using (WebClient client = new WebClient())
{
Stream s = client.OpenRead("http://our.umbraco.org/css/img/top_logo.png");
using (MemoryStream ms = new MemoryStream())
{
s.CopyTo(ms);
}
}
The ms.ToArray() now has the raw data of the img as a byte[] but where do you put that on the media element?
Thanks for the reply.
It seems that the link you have posted is using a HttpPostedFileBase.
This i guess comes from an upload control and in my case I don't work with an upload control.
What i have is the raw physical data of a file.
How would you programmatically upload images from your hard drive to umbraco via mediaservice?
Haven't replied but here is how i solved the problem, but it is a bit hacky imo think it uses something I found in another forum posts that i can't find now:
I used this to wrap up the image data as byte[]
public class HttpPostedFileWrapper : HttpPostedFileBase
{
private Stream stream;
private string contentType;
private string fileName;
public HttpPostedFileWrapper(string fileName, string contentType, byte[] imgData)
{
this.fileName = fileName;
this.stream = new MemoryStream(imgData);
this.contentType = contentType;
}
public override int ContentLength
{
get { return (int)stream.Length; }
}
public override string ContentType
{
get { return contentType; }
}
public override string FileName
{
get { return fileName; }
}
public override Stream InputStream
{
get { return stream; }
}
public override void SaveAs(string filename)
{
using (var file = File.Open(filename, FileMode.CreateNew))
stream.CopyTo(file);
}
}
I then created an UmbracoApiController to get access to MediaService with the following method:
[HttpPost]
public int CreateImage(Image image)
{
var newImage = mediaService.CreateMedia(image.Name, image.ParentID, image.MediaType);
HttpPostedFileWrapper httpPostedWrapper = new HttpPostedFileWrapper(image.ImageName, image.ContentType, image.ImageData);
newImage.SetValue("umbracoFile", httpPostedWrapper);
mediaService.Save(newImage);
return newImage.Id;
}
The Image obj contains the data needed to create the image (ContentType, Name, Data etc.)
For ~4mb + images u need to fiddle with your web.config.
Umbraco 6 Media API upload image
Hi all
Can anybody tell me how I upload a picture (the actual data) via the media API. I've been searching for a while and can't seem to find anything for the v6 API.
It looks like in the legacy v4 you can use the mediaServiceSoapClients writeContents (haven't tried it though) but how do you get the raw data of an Image up in the media library?
Hi Niels,
you can find the documentation here:
http://our.umbraco.org/documentation/Reference/Management-v6/Models/Media
I hope this helps.
Best regards
Sören
Cheers for the link Søren, but I have looked the documentation through and can't figure out from it where the raw data of the image goes.
Let say I make a method that downloads that umb logo from the top:
The ms.ToArray() now has the raw data of the img as a byte[] but where do you put that on the media element?
Or do you have to create the file via IO?
Here is an example: https://gist.github.com/nul800sebastiaan/8008892
Jeroen
Hi Jeroen
Thanks for the reply. It seems that the link you have posted is using a HttpPostedFileBase. This i guess comes from an upload control and in my case I don't work with an upload control.
What i have is the raw physical data of a file. How would you programmatically upload images from your hard drive to umbraco via mediaservice?
Haven't replied but here is how i solved the problem, but it is a bit hacky imo think it uses something I found in another forum posts that i can't find now:
I used this to wrap up the image data as byte[]
I then created an UmbracoApiController to get access to MediaService with the following method:
The Image obj contains the data needed to create the image (ContentType, Name, Data etc.)
For ~4mb + images u need to fiddle with your web.config.
is working on a reply...