Press Ctrl / CMD + C to copy this to your clipboard.
This post will be reported to the moderators as potential spam to be looked at
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");
There's an extension method that allows you to set a value using a Stream with the signature:
SetValue(this IContentBase content, IContentTypeBaseServiceProvider contentTypeBaseServiceProvider, string propertyTypeAlias, string filename, Stream filestream, string culture = null, string segment = null)
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);
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...
Write your reply to:
Image will be uploaded when post is submitted