This action is similar to the one in which we are using byte with the exception that, now, we are calling the GetImageAsStream( ) method from the FileService class. We are going to keep the Get HTTP Verb, but this time, we set our route to images-stream. Var image = _fileService.GetImageAsStream() Now, let’s create a ReturnStream action: You can help the X-Files Wiki by expanding it. This article is a stub and is incomplete. The File method lives under the ControllerBase abstract class and returns a FileContentResult, which is responsible to provide the file to download. But The X-Files franchise wouldn't return until 2008 with the second feature-length film The X-Files: I Want to Believe, which eight-years later would be followed by a tenth season - 14 years since the show last broadcast on television. Then, we return a call to the File method passing the image file as a byte array, the mimeType we set as a class field, and the file name we want to return, as parameters. So, first, we use GetImageAsByteArray() from the FileService class to retrieve the image we want to return as an array of bytes. Once we are using the same file result for every request, we don’t need any input parameter. Note that this is an images-byte route with the Get HTTP Verb. Var image = _fileService.GetImageAsByteArray() Now, let’s create our first ReturnByteArray action to return a file using Byte Array: Learning Web API? Get our eBook ASP.NET Core Web API Best Practices and become an expert for FREE! > GET THE BOOK << Return a File in ASP.NET Core Web API As a ByteArray The MimeType field represents the nature of the file we want to return and the FileName represents a suggestion file name to download. For this article, we are not going into detail about how to do it, however, you can check the FileService class implementation in our source code. The _fileService contains the logic to transform the file into a byte array or a stream. Note that we have a _fileService, a MimeType, and a FileName field. Public DownloadsController(IFileService fileService) Private const string FileName = "CM-Logo.png" Private const string MimeType = "image/png" Private readonly IFileService _fileService In the time between the end of The X-Files ‘ original run on the small screen and its mini event series rebirth in 2016 (the 2008 movie, The X-Files: I Want. Public class DownloadsController : ControllerBase Once we have an ASP.NET Core Web API ready, let’s create an empty API Controller: ")] You can check the Convert Byte Array to File in C# article to learn more about byte arrays and the C# Back to Basics – Files, StreamWriter and StreamReader to learn more about Stream. The most common class that derives from Stream is FileStream and MemoryStream. Stream is an abstract class to represent a sequence of bytes.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |