I am trying to upload files programmatically using a html file upload control. The file is getting uploaded and the respective media item is getting created but when I open the media item it's actually showing as file stream not the actual file . Please find my code below
public void CreateMedia(string mediaName, Guid folderGuid, FileStream file, string extension, string size, object mediaFile)
{
var media = _mediaService.CreateMedia(mediaName, folderGuid, "File");
media.SetValue("umbracoFile", file);
media.SetValue("umbracoExtension", extension);
media.SetValue("umbracoBytes", size);
_mediaService.Save(media);
}
To use it include the Umbraco.Core namespace in your class. You don't need to set any other values like bytes or size. Just something like:
using (var file = new FileStream(Server.MapPath("~/images/image.png"), FileMode.Open))
{
var media = Services.MediaService.CreateMediaWithIdentity(mediaName, folderId, "File");
media.SetValue(Services.ContentTypeBaseServices, "umbracoFile", "myfile.png", file);
Services.MediaService.Save(media);
}
Also notice that you should use CreateMediaWithIdentity instead.
Upload Any File Pro-grammatically To Media
I am trying to upload files programmatically using a html file upload control. The file is getting uploaded and the respective media item is getting created but when I open the media item it's actually showing as file stream not the actual file . Please find my code below
There's an extension method that allows you to set a value using a
Stream
with the signature:To use it include the
Umbraco.Core
namespace in your class. You don't need to set any other values like bytes or size. Just something like:Also notice that you should use
CreateMediaWithIdentity
instead.Thanks for your suggestion. It's working now after making your suggested changes. Thanks again.
is working on a reply...