我正在尝试将图像上传到我们从 ASP.NET Core Web API 前端接收文件的文件夹下的媒体文件夹。我使用的是 Umbraco 3.2.2 版本。
当我像下面一样尝试时,它抛出该文件容器不存在:
[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");
}
}
是否无法将图像上传到 Umbraco 后台媒体文件夹?
这是因为本地azure模拟器没有数据。当我添加本地 Azure 存储中的所有文件夹时,问题已得到解决。