ABP VNext BLOB存储文件到本地文件

文章详细介绍了如何在ABPVNext框架下将BLOB存储文件到本地文件系统。这包括在Application层引用NuGet包,配置AbpBlobStoringOptions,定义Blob服务层和接口,以及在HttpApi层处理文件上传和下载。同时,Web层设置了文件上传大小限制。
摘要由CSDN通过智能技术生成

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;
        });
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值