Save images/files to media folder programmatically
Hi!
I've recently started developing with Umbraco and I'm currently using the latest version (version 8). The experience has been good so far but I've stumbled across a problem and I just can't seem to find any working solution for it using version 8...
I'm simply trying to programmatically save a file (image) uploaded through a regular asp form (Html.BeginUmbracoForm(...)). The file information (filename) seem to get stored in the database but when I look in the media folder it's not there and I don't get any error messages saving it (in the backoffice I see the reference/filename is stored both on page and under the media folder but the actual image or information about width etc. is not).
Here's a part of my code for storing the file:
//myFile is an object of HttpPostedFileBase
IMediaService mediaService = Services.MediaService; //creating service
int mediaRootId = 4120; //pointing to sub folder under media
var filename = myFile.FileName;
var mediaType = Constants.Conventions.MediaTypes.Image;
var media = mediaService.CreateMedia(filename, mediaRootId, mediaType);
media.SetValue("umbracoFile", myFile.InputStream);
mediaService.Save(media);
And later I save the media object to a page property together with other form data...
content.SetValue("myUploadedFile", media);
var result = contentService.SaveAndPublish(content);
I've been scratching my head and I'm thankful for any help regarding this...
Try creating the media with CreateMediaWithIdentity instead:
var media = Services.MediaService.CreateMediaWithIdentity(filename, mediaRootId, mediaType);
media.SetValue("umbracoFile", myFile);
Services.MediaService.Save(media);
var contentTypeBaseServiceProvider = Current.Services.ContentTypeBaseServices;
IMedia image = _umbMediaService.InsertImage(model.Logo, model.MediaFolderId, contentTypeBaseServiceProvider);
Here are the using statements. I am sure not all are necessary. I am sure your visual studios intellisesne will tell you if one of them is not required.
using Umbraco.Core.Composing;
using Website.Core.Helpers;
using Website.Core.Models;
using Website.Core.Models.EntityModels;
using Website.Core.Services;
I can confirm this code works. I literally just re-tested this on a project before posting.
Thanks you are my hero !! :)
i can add something here if you want to save this media on a media picker you can do it like this
var media = InsertImage(myHttpPostedFileBase);
var member = GetCurrentIMember();
var udi = Udi.Create(DefinedConstants.UdiEntityType.Media, media.Key);
member.SetValue("mycustomimage",udi.ToString());
_memberService.Save(member);
Save images/files to media folder programmatically
Hi! I've recently started developing with Umbraco and I'm currently using the latest version (version 8). The experience has been good so far but I've stumbled across a problem and I just can't seem to find any working solution for it using version 8...
I'm simply trying to programmatically save a file (image) uploaded through a regular asp form (Html.BeginUmbracoForm(...)). The file information (filename) seem to get stored in the database but when I look in the media folder it's not there and I don't get any error messages saving it (in the backoffice I see the reference/filename is stored both on page and under the media folder but the actual image or information about width etc. is not). Here's a part of my code for storing the file:
And later I save the media object to a page property together with other form data...
I've been scratching my head and I'm thankful for any help regarding this...
Anyone here with some insight about this?
Hi Henning,
There's an extension method in the
Umbraco.Core
namespace that accepts an HttpPostedFileBase directly, so you can do this to save your uploaded file:The
ContentTypeBaseServices
is available in theServices
object of an Umbraco controller.Great! Works like a charm... Many thanks Mario!
Hi Mario, do you know how to make it visible in Umbraco backoffice?
Incase you're still struggling with this Josip... Saving your page/content and file like this should work for you and display it in the backoffice:
Try creating the media with
CreateMediaWithIdentity
instead:Hi,
This is how I do it.
And this is how I call the method.
Here are the using statements. I am sure not all are necessary. I am sure your visual studios intellisesne will tell you if one of them is not required.
I can confirm this code works. I literally just re-tested this on a project before posting.
Regards
David
Thanks you are my hero !! :) i can add something here if you want to save this media on a media picker you can do it like this
Hi, I am new in Umbraco (I am using v9). What is Services here? IMediaService mediaService = Services.MediaService; //creating service
Thanks.
Hi Fernando
Yes, you are correct.
In v9 the
Services.MediaService
is now Dependency Injected asIMediaService
// Fred
Yes, I solved this using DI, thanks.
is working on a reply...