We’re building a function where partners (to our client) can submit an Umbraco form and an editor will then look at the form submission values and either accept or deny it (via a custom page that we’ve built). If the editor accept it, a new page will be created.
Our problem is that the uploaded image (from UB Form fileupload-element) doesn’t seem to be reachable in the code. We want to move that image from /media/forms/upload/ to a folder of our choosing plus set an image value on the new page that’s being created.
The uploaded image has a url that looks like this: “/media/forms/upload/form_e4ce59d4-e91f-41e8-b0ae-18b2820c1d02/595e3d4f-67f3-4360-b60e-2aecb8aec1ba/test-image.jpg” and if I try to access it via mediaService.GetMediaByPath(); it returns null. I’ve also tried mediaService.GetById and pass the guid (595e3d4f-67f3-4360-b60e-2aecb8aec1ba in my example) and it returns null as well.
There has to be some way of accessing these images like with any other Umbraco image, right?
mediaService.GetMediaByPath() will return null as this isn’t a media item (eg listed in the media area of the backoffice). It’s just been stored on disk by Umbraco Forms, similar to the File Upload property .
So you’ll have to do the heavy lifting…
Also as it’s not always the case that it’s a local filessytem, could be blob storage etc.. I’d recommend using the MediaFileManager service.
@using Microsoft.AspNetCore.Hosting;
@using Umbraco.Cms.Core.IO
@inject MediaFileManager _mediaFileManager
@inject IWebHostEnvironment _webHostEnvironment
var url = "/media/forms/upload/form_e4ce59d4-e91f-41e8-b0ae-18b2820c1d02/595e3d4f-67f3-4360-b60e-2aecb8aec1ba/test-image.jpg";
string webRootPath = _webHostEnvironment.WebRootPath;
var path = System.IO.Path.Combine(webRootPath, System.IO.Path.Combine(url.Split('/', StringSplitOptions.RemoveEmptyEntries)));
if (_mediaFileManager.FileSystem.FileExists(path))
{
_mediaFileManager.FileSystem.AddFile("{newpath...}", path, overrideIfExists: true, copy: true);
// copy: false if you aren't bothered about broken links in the uForms backoffice/emails you've sent
// create your node and assign the new path to your property.
// might want to use FileUpload or MediaPicker property??
....
}
Awesome, thank you! Locally we have the media-folder on an external disk and test/production is on Umbraco cloud. Does it work with MediaFileManager then also (I haven’t had the time to test your snippet yet)?
var url = “/media/forms/upload/form_e4ce59d4-e91f-41e8-b0ae-18b2820c1d02/595e3d4f-67f3-4360-b60e-2aecb8aec1ba/test-image.jpg”;
if (_mediaFileManager.FileSystem.FileExists(url))
{
var fileName = url.Split('/').LastOrDefault();
var stream = _mediaFileManager.FileSystem.OpenFile(url);
var media = _mediaService.CreateMedia(fileName, Constants.System.Root, Constants.Conventions.MediaTypes.Image);
media.SetValue(_mediaFileManager, _mediaUrlGeneratorCollection, _shortStringHelper, contentTypeBaseServiceProvider, Constants.Conventions.Media.File, fileName, stream);
var result = _mediaService.Save(media);
}
you might want to add using (stream) for disposal purposes..
Also I’ve never been a fan of allowing anything but folders in the media section root, since the versions where Umbraco had issues with large lists of images/assets in root causing slowdowns… probably addressed in more modern versions..
In the finished code I’ll move the files to a folder that we chose in our sites settings page , I just wanted to show a little snippet in case someone else googled this problem. I took the code from docs actually