I am trying to upload the image to media folder under a folder which we are receiving the file from frontend in .net core api. I am using Umbraco 3.2.2 version.
When I am trying like below its throwing that file container is not exists:
[HttpPost("/agents/logo-upload")]
public async Task<IActionResult> UploadImage([FromForm]IFormFile file)
{
if (file == null || file.Length == 0)
{
return BadRequest("No file uploaded.");
}
try
{
// 1. Generate a unique file name and save it to the media folder
var folderName = _authenticationService.GetAgent().Agency.Name;
folderName = folderName.Trim().Replace(" ","").ToLower();
var fileName = Path.GetFileName(file.FileName);
var filePath = Path.Combine("media", folderName, fileName); // Replace with your media folder path
var existingFolder = _mediaService.GetRootMedia().FirstOrDefault(m => m.Name.InvariantEquals(folderName));
if (existingFolder == null)
{
var newFolder = _mediaService.CreateMedia(folderName, Constants.System.Root, "Folder");
_mediaService.Save(newFolder);
existingFolder = newFolder;
}
var directoryPath = Path.GetDirectoryName(filePath);
if (!Directory.Exists(directoryPath))
{
Directory.CreateDirectory(directoryPath);
}
using (var fileStream = new FileStream(filePath, FileMode.Create))
{
await file.CopyToAsync(fileStream);
}
IMedia media = null;
using (var stream = System.IO.File.OpenRead(filePath))
{
media = _mediaService.CreateMedia(fileName, existingFolder.Id, Constants.Conventions.MediaTypes.Image);
media.SetValue(_mediaFileManager, _mediaUrlGeneratorCollection, _shortStringHelper, _contentTypeBaseServiceProvider, Constants.Conventions.Media.File, fileName, stream);
_mediaService.Save(media); // throwing the error here
}
return Ok(new { MediaId = media.Id, FilePath = filePath });
}
catch (Exception ex)
{
_logger.LogError(ex, "Error while uploading image.");
return StatusCode(500, "Internal server error");
}
}
}
Is it not possible to upload the image to Umrbaco backoffice media folder?
Are you hosted on Azure? If so, you don't need to save the image to a directory unless it's absolutely necessary. You can directly use the file stream to create the media item in Umbraco like this:
using (var stream = file.OpenReadStream())
{
var media = _mediaService.CreateMedia(file.FileName, folderName, Constants.Conventions.MediaTypes.Image);
media.SetValue(_mediaFileManager, _mediaUrlGeneratorCollection, _shortStringHelper, _contentTypeBaseServiceProvider, Constants.Conventions.Media.File, file.FileName, stream);
_mediaService.Save(media);
}
If not, you might want to check into storage account configuration
oh okay. is there any setting that I have to change?
I am trying to upload locally in back office media folder manually, still its throws below error:
Azure.RequestFailedException: The specified container does not exist.
RequestId:e1f6c584-3bc6-4cea-bbe5-c6718ea13d8f
Time:2024-11-27T10:47:05.212Z
Status: 404 (The specified container does not exist.)
ErrorCode: ContainerNotFound
could you removing the blob storage config and try again? (Umbraco is attempting to access an Azure Blob Storage container to upload or retrieve media files).
Image upload to Media folder in .net core api
Hello Team,
I am trying to upload the image to media folder under a folder which we are receiving the file from frontend in .net core api. I am using Umbraco 3.2.2 version.
When I am trying like below its throwing that file container is not exists:
Is it not possible to upload the image to Umrbaco backoffice media folder?
Hi Sanjay,
Are you hosted on Azure? If so, you don't need to save the image to a directory unless it's absolutely necessary. You can directly use the file stream to create the media item in Umbraco like this:
If not, you might want to check into storage account configuration
I have tried like above without storing the file anywhere. But still it throws same error.
It's working fine for me maybe you got something wrong with the controller
oh okay. is there any setting that I have to change?
I am trying to upload locally in back office media folder manually, still its throws below error:
could you removing the blob storage config and try again? (Umbraco is attempting to access an Azure Blob Storage container to upload or retrieve media files).
It is resolved by creating media folder in local azure blob storage. Thought it will automatically created, but its actually not.
is working on a reply...