WSM.Media.Management
1.0.1
See the version list below for details.
dotnet add package WSM.Media.Management --version 1.0.1
NuGet\Install-Package WSM.Media.Management -Version 1.0.1
<PackageReference Include="WSM.Media.Management" Version="1.0.1" />
paket add WSM.Media.Management --version 1.0.1
#r "nuget: WSM.Media.Management, 1.0.1"
// Install WSM.Media.Management as a Cake Addin #addin nuget:?package=WSM.Media.Management&version=1.0.1 // Install WSM.Media.Management as a Cake Tool #tool nuget:?package=WSM.Media.Management&version=1.0.1
WSM Media Management Library
A simple library to manage images or media files in ASP.NET Core with EF Core integration.
Features
- Upload and store media files using EF Core.
- Generate URLs for uploaded media files.
- Easy integration with ASP.NET Core through dependency injection.
Installation
- Install the necessary NuGet packages:
dotnet add package WSM.Media.Management
dotnet add package Microsoft.EntityFrameworkCore.InMemory
- Add the necessary dependencies to your project:
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using WSM.Media.Management;
using WSM.Media.Management.Media;
using WSM.Media.Management.Models;
using WSMMediaSample1;
Setup
1. Create AppDbContext
Your AppDbContext
must inherit from IMediaDbContext
and include the MediaFiles
property:
using Microsoft.EntityFrameworkCore;
using WSM.Media.Management.Db;
using WSM.Media.Management.Models;
public class AppDbContext : DbContext, IMediaDbContext
{
public AppDbContext(DbContextOptions options) : base(options) { }
public DbSet<MediaFile> MediaFiles { get; set; }
}
2. Register Services in Program.cs
To enable media management, add the AddMediaFile
and UseMediaFile
services in the Program.cs
:
var builder = WebApplication.CreateBuilder(args);
// Register DbContext
builder.Services.AddDbContext<AppDbContext>(options =>
options.UseInMemoryDatabase(nameof(AppDbContext)));
// Register MediaFile management
builder.Services.AddMediaFile<AppDbContext>();
var app = builder.Build();
// Enable MediaFile middleware
app.UseMediaFile();
app.UseHttpsRedirection();
3. Upload and Manage Media Files
To upload media files, use the FileManager
class in your API. Here’s an example of how to add media in a controller or endpoint:
var mediaFileId = await fileManager.UploadFileAsync<WeatherForecast>(new()
{
Bytes = Convert.FromBase64String(fileRequest.Base64),
Extension = fileRequest.Extension,
});
var mediaFileUrl = await fileManager.GetUrlAsync(mediaFileId);
return new
{
ResponseUrl = mediaFileUrl,
MediaFileId = mediaFileId,
};
4. Example File Request Model
Create a simple request model for handling incoming files:
public class ExampleFileRequest
{
public string Extension { get; set; }
public string Base64 { get; set; }
}
5. Don't forgot to create App_Data folder and follow the sample project for more details
mkdir App_Data
This setup allows you to upload media files, generate URLs, and store them using EF Core.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
-
net8.0
- Microsoft.EntityFrameworkCore (>= 8.0.8)
- SixLabors.ImageSharp (>= 3.1.5)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.