ABP VNext BLOB存储文件到本地文件
1.Application层
1.1 引用NuGet包
安装 Volo.Abp.BlobStoring
安装 Volo.Abp.BlobStoring.FileSystem
1.2 Module添加依赖
typeof(AbpBlobStoringModule)
typeof(AbpBlobStoringFileSystemModule)
[DependsOn(
typeof(BLobDemoDomainModule),
typeof(AbpAccountApplicationModule),
typeof(BLobDemoApplicationContractsModule),
typeof(AbpIdentityApplicationModule),
typeof(AbpPermissionManagementApplicationModule),
typeof(AbpTenantManagementApplicationModule),
typeof(AbpFeatureManagementApplicationModule),
typeof(AbpSettingManagementApplicationModule)
,typeof(AbpBlobStoringModule)
,typeof(AbpBlobStoringFileSystemModule)
)]
1.3 ConfigureServices添加AbpBlobStoringOptions
public override void ConfigureServices(ServiceConfigurationContext context)
{
......
Configure<AbpBlobStoringOptions>(options =>
{
options.Containers.ConfigureDefault(container =>
{
container.UseFileSystem(fileSystem =>
{
fileSystem.BasePath = "D:\\my-files";
});
});
});
}
1.4 Blob服务层
public class BlobService:ApplicationService, IBlobService
{
private readonly IBlobContainer<ProfilePictureContainer> _blobContainer;
public BlobService(IBlobContainer<ProfilePictureContainer> blobContainer)
{
_blobContainer = blobContainer;
}
[RemoteService(false)]
public async Task SaveBytesAsync(byte[] bytes)
{
await _blobContainer.SaveAsync("my-blob-1", bytes,true);
}
[RemoteService(false)]
public async Task<byte[]> GetBytesAsync()
{
return await _blobContainer.GetAllBytesOrNullAsync("my-blob-1");
}
}
2.Application.Contracts层
2.1 引用NuGet包
安装 Volo.Abp.BlobStoring
2.2 Blob服务层接口
public interface IBlobService : IApplicationService
{
Task SaveBytesAsync(byte[] bytes);
Task<byte[]> GetBytesAsync();
}
2.3 类型化 IBlobContainer
[BlobContainerName("profile-pictures")]
public class ProfilePictureContainer
{
}
3.HttpApi层
[RemoteService(Name = "upload")]
[Route("api/upload")]
public class UpLoadController : AbpControllerBase
{
private readonly IBlobService _blobService;
public UpLoadController(IBlobService blobService)
{
_blobService = blobService;
}
[HttpPost]
public async Task SaveBytesAsync(IFormFile formFile)
{
using (var memoryStream = new MemoryStream())
{
await formFile.CopyToAsync(memoryStream);
await _blobService.SaveBytesAsync(memoryStream.ToArray());
}
}
[HttpGet]
[Route("downLoad")]
public async Task<IActionResult> GetDownloadAsync(string fileName)
{
var fileDto = await _blobService.GetBytesAsync();
return File(fileDto, "video/mp4", "dd");
}
}
4.Web层
设置文件上传大小限制
Configure<KestrelServerOptions>(options =>
{
options.Limits.MaxRequestBodySize = int.MaxValue; // if don't set default value is: 30 MB
});
Configure<FormOptions>(x =>
{
x.ValueLengthLimit = int.MaxValue;
x.MultipartBodyLengthLimit = int.MaxValue; // if don't set default value is: 128 MB
x.MultipartHeadersLengthLimit = int.MaxValue;
});