Introduction
This step-by-step article describes how to upload a file to a Web server and also download by client with using ASP.NET Core & ABP Framework. By following this article, you will create a web project and its related code to upload and download files.
Before the creating application, we need to know some fundamentals.
BLOB Storing
It is typical to store file contents in an application and read these file contents on need. Not only files, but you may also need to save various types of large binary objects, a.k.a. BLOBs, into a storage. For example, you may want to save user profile pictures.
A BLOB is a typically byte array. There are various places to store a BLOB item; storing in the local file system, in a shared database or on the Azure BLOB storage can be options.
The ABP Framework provides an abstraction to work with BLOBs and provides some pre-built storage providers that you can easily integrate to. Having such an abstraction has some benefits;
- You can easily integrate to your favorite BLOB storage provides with a few lines of configuration.
- You can then easily change your BLOB storage without changing your application code.
- If you want to create reusable application modules, you don't need to make assumption about how the BLOBs are stored.
ABP BLOB Storage system is also compatible to other ABP Framework features like multi-tenancy.
To get more information about ABP BLOB Storing system, please check this documentation.
Preparing the Project
Startup template and the initial run
Abp Framework offers startup templates to get into the business faster. We can download a new startup template using Abp CLI:
abp new FileActionsDemo -m none
After the download i
alexandru-bagu 5 years ago
When you say large binary objects you don't actually mean large files, do you? I mean do try uploading a 1gb file with your code and see if it will actually work without any tweaks. How would one go about uploading a file directly to the API without using a Dto (because loading unnecessarily a 1gb file, heck it could be any size, in memory is stupid)? In AppService I could read the body stream, but how does one manage the authentication without digging through volosoft's code to figure out the parts required to build a httpclient with the required headers to be able to actually make the requests?
cotur 5 years ago
Hi Alexandru, This post is just PoC for file upload and download processes, I never described that it the best-practise. For really big files, of course we should use streaming, not byte arrays. ABP Framework has one special object to solve this issue, that is the RemoteStreamContent. With RemoteStreamContent, you are able to send streams from controller to application service and to blob provider. But you should not use Database Provider if you plan to store really big files, because Entity Framework have no stream implementation. Some resources for you: https://github.com/dotnet/efcore/issues/6234 https://github.com/abpframework/abp/issues/7418
Anthony_Albutt 5 years ago
Great article. How do you get _fileContainer.SaveAsync(input.Name, input.Content, true); to add TenamatId in the data record. The read requires it by default. where to add container.IsMultiTenant = true;
cotur 5 years ago
Thanks for thoughts. :) The multi-tenacy is automatically managed by blob providers. You should not care about it.
Anthony_Albutt 5 years ago
Thanks Cotur. Unfortunately, when the record is saved into the data table using your example, no TenantId is inserted. If you read, no data is returned, unless I manually update the data record and add the correct TenantId into the data table
cotur 5 years ago
Oh I see, this is a known problem and in 3.1 version of ABP (currently is under development) we had fix that issue. You need to work with source-code for now, when the new version is released (probably in 4-5 days) you'll get that bug-fix. reference: https://github.com/abpframework/abp/pull/4944
hikalkan 5 years ago
Great article :)