https://github.com/sitholewb/files.entityframeworkcore.extensions
This is a library for storing files in small chunks on sql using EntityFrameworkCore. Works well with Entity Framework as an extension.
https://github.com/sitholewb/files.entityframeworkcore.extensions
blob-storage bytes entity-framework-core extensions file-upload files images storage
Last synced: about 1 year ago
JSON representation
This is a library for storing files in small chunks on sql using EntityFrameworkCore. Works well with Entity Framework as an extension.
- Host: GitHub
- URL: https://github.com/sitholewb/files.entityframeworkcore.extensions
- Owner: SitholeWB
- License: mit
- Created: 2023-04-08T19:23:48.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2023-09-02T12:29:36.000Z (almost 3 years ago)
- Last Synced: 2025-07-07T08:55:05.969Z (about 1 year ago)
- Topics: blob-storage, bytes, entity-framework-core, extensions, file-upload, files, images, storage
- Language: C#
- Homepage:
- Size: 1.03 MB
- Stars: 5
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Files.EntityFrameworkCore.Extensions
This is a library for storing files in small chunks on sql using EntityFrameworkCore.
Works well with **Entity Framework** as an extension.
# Get Started
```nuget
Install-Package Files.EntityFrameworkCore.Extensions
```
# Example
### More examples found inside the repository
```C#
public class UserImage : IFileEntity
{
public Guid Id { get; set; }
public Guid FileId { get; set; }
public string Name { get; set; }
public string MimeType { get; set; }
public DateTimeOffset TimeStamp { get; set; }
public Guid? NextId { get; set; }
public int ChunkBytesLength { get; set; }
public long TotalBytesLength { get; set; }
public byte[] Data { get; set; }
}
public class UploadFileIdCommand
{
public Guid? FileId { get; set; }
}
public class UploadCommand
{
public IFormFile File { get; set; }
}
public class WebApiContext : DbContext
{
public WebApiContext(DbContextOptions options)
: base(options)
{
}
public DbSet UserImage { get; set; }
}
[Route("api/user-images")]
[ApiController]
public class UserImagesController : ControllerBase
{
private readonly WebApiContext _context;
public UserImagesController(WebApiContext context)
{
_context = context;
}
[HttpPost]
[DisableRequestSizeLimit]
public async Task> UploadFile([FromForm] UploadCommand uploadCommand)
{
var file = uploadCommand.File;
if (file.Length > 0)
{
var fileDetails = await _context.SaveFileAsync(file.OpenReadStream(), file.FileName, file.ContentType);
return Ok(fileDetails);
}
else
{
return BadRequest("File is required.");
}
}
[HttpPost("other-file")]
public async Task> UploadOtherFile([FromBody] UploadFileIdCommand command)
{
//The @"appsettings.json" is a path to any file you will like to save
var fileDetails = await _context.SaveFileAsync(@"appsettings.json", command?.FileId);
//Save will be auto called on every chunk addition so that memory usage remain low, i.e. await _context.SaveChangesAsync();
return Ok(fileDetails);
}
[HttpGet("{id}/download")]
public async Task DownLoadFile(Guid id)
{
var fileDetails = await _context.GetFileInfoAsync(id);
var stream = new MemoryStream();
await _context.DownloadFileToStreamAsync(id, stream);
return File(stream, fileDetails.MimeType, fileDetails.Name);
}
[HttpGet("{id}/view")]
public async Task DownloadView(Guid id)
{
var fileDetails = await _context.GetFileInfoAsync(id);
var stream = new MemoryStream();
await _context.DownloadFileToStreamAsync(id, stream);
return new FileStreamResult(stream, fileDetails.MimeType);
}
[HttpDelete("{id}")]
public async Task DeleteUserImage(Guid id)
{
if (_context.UserImage == null)
{
return NotFound();
}
var userImage = await _context.UserImage.FindAsync(id);
if (userImage == null)
{
return NotFound();
}
await _context.DeleteFileAsync(id);
//Save will be auto called on every chunk deletion so that memory usage remain low, i.e. await _context.SaveChangesAsync();
return NoContent();
}
}
```