Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# .NET Web API What is best practice for file up- and download?

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().

enter image description here

like image 846
Frank Mehlhop Avatar asked Apr 26 '19 06:04

Frank Mehlhop


People also ask

What C is used for?

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 ...

What is C full form?

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 language basics?

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.

Is C language easy?

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.


2 Answers

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();
    }
}
like image 131
Marco Avatar answered Sep 22 '22 02:09

Marco


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.

like image 34
Manoj Choudhari Avatar answered Sep 23 '22 02:09

Manoj Choudhari