I got a C# .NET Core Web API (REST). I want to implement an endpoint for upload files and one for download files (any file type, as binary). What is best practice for design and code? What I find so far by a search engine is kind of week. Do you have a recommendation for me? I guess I have to save metadata like the title (type,..?) somewhere else. Could make it easier to search for files.
At this link from microsoft I get the problem, that it seems to be only for MVC API, what I don't use.
After
[HttpPost("UploadFiles")]
public async Task<IActionResult> Post(List<IFormFile> files)
files
is empty.
And this from the c-sharpcorner does not work neater. My object file from Type IFormFile
doesn't know the method.GetFilename()
.
C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...
Full form of C is “COMPILE”. One thing which was missing in C language was further added to C++ that is 'the concept of CLASSES'.
What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.
C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.
If you take a look at the IFormFile
docs, you'll notice, that it has a property FileName
, which you can use:
https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.http.iformfile?view=aspnetcore-2.2
So, as an example, you could take that and save it whereever you want with that property. In my case all the low level stuff of writing that file to the storage is hidden in a repository, but you get the idea:
public async Task<IActionResult> Upload(IFormFile file)
{
if (file == null || file.Length == 0) return BadRequest();
using (var ms = new MemoryStream())
{
file.CopyTo(ms);
var img = new Entities.Image
{
Name = file.FileName,
ContentType = file.ContentType,
Data = ms.ToArray()
};
await _repo.CreateImage(img);
return Ok();
}
}
There is very nice article about how to upload / download files using .net core at this location. The same tutorial should work for web API also.
I checked in your question that it is not working for you, you many want to check the request through postman / fiddler and check if data is being sent properly. This blog has example of file upload web API
For getting the filename, the definition of IFormFile as as below:
public interface IFormFile
{
string ContentType { get; }
string ContentDisposition { get; }
IHeaderDictionary Headers { get; }
long Length { get; }
string Name { get; }
string FileName { get; }
Stream OpenReadStream();
void CopyTo(Stream target);
Task CopyToAsync(Stream target, CancellationToken cancellationToken = null);
}
Hence you should be able to use property FileName
to get the file name.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With