I'm recently working on a ReactJS app that's calling an API (developed with .NET Core 2.0).
My question is how to use HttpPostedFileBase
in an .NET Core 2.0 API in order to get file content and save it in database.
I'm recently working on a ReactJS app that's calling an API (developed with .NET Core 2.0).
My question is how to use HttpPostedFileBase
in an .NET Core 2.0 API in order to get file content and save it in database.
You don't have
HttpPostedFileBase
in ASP.NET Core 2.0, but you can useIFormFile
.More here: https://docs.microsoft.com/en-us/aspnet/core/mvc/models/file-uploads?view=aspnetcore-2.1
IFormFile
is in the following namespaceMicrosoft.AspNetCore.Http
.You should also be able to get the files like this :
HttpPostedFileBase
doesn't exist in ASP.NET Core. You should useIFormFile
now, instead. However, that only works when you send the request asmultipart/form-data
, which you're likely not doing if you're working with a client-side framework like React. If you're posting JSON, you should set the JSON member that corresponds with your file property with the file encoded as a Base64 string. Server-side, you should then bind tobyte[]
.If anyone finds this by searching for
HttpPostedFileBase
it's possibly you're familiar with writing ASP.NET controller methods something like this:If you're wanting to write an equivalent in ASP.NET Core then you can write it like this:
i.e. the only change needed to the method signature is replacing
HttpPostedFileBase
withIFormFile
. You will then need to modify your method to work with the new parameter type (e.g.HttpPostedFileBase
has anInputStream
property, whereasIFormFile
has aOpenReadStream()
method) but I think the details of those differences are beyond the scope of this question.