Open file in browser instead of downloading .NET example

force files to open in the browser instead of downloading pdf

Today in this article, we will learn example with C#. NET – Open file in browser instead of downloading simple technique in ASP or MVC application or API application.

You may want to force files to open in the client’s browser before a client can actually download them directly. We shall see how to achieve this for the .PDF or Image file like .png or .jpeg file.

Please note that ASP.NET Core now supports file download. You can let User open the file directly or download it directly without opening it in the browser.

Today in this article, we will cover below aspects,

If you are already sure about the file extension and you want the user to open the file as a preview then we can use the File or FileContentResult class to decide the behavior.

Using File class to Open PDF file in browser

File class can be used directly in the controller method once you obtain the stream or byte array of the required file.

         [HttpGet("{id}")]
         [Route("Stream")]
         public async Task DownloadInBrowserPdfFile(string fileId)
         {

            byte [] stream = await _fileInterafces.ReadAsByteStream(fileId);         
            string mimeType = "application/pdf";         
            return File(stream, mimeType);  
          }

Using FileContentResult class to Open file in browser

FileContentResult class can be used directly in the controller method once you obtain the stream or byte array of the required file.

         [HttpGet("{id}")]
         [Route("Stream")]
         public async Task DownloadInBrowserPdfFile(string fileId)
         {

            byte [] stream = await _fileInterafces.ReadAsByteStream(fileId);         
            string mimeType = "application/pdf";         
            return new FileContentResult(stream, mimeType);     
          }       

Using File class to Open Image/png file in browser

Similarly depending on the file type, if you are available, you can specify file type,

         [HttpGet("{id}")]
         [Route("Stream")]
         public async Task DownloadInBrowserPdfFile(string fileId)
         {

            byte [] stream = await _fileInterafces.ReadAsByteStream(fileId);         
            string mimeType = "image/png";         
            return new FileContentResult(stream, mimeType);     
          }    

make a file open in browser instead of downloadingC NET Open file in browser instead of downloading

Difference between FileResult, FileContentResult FileStreamResult

As we know FileResult is an abstract base class. Both FileContentResult FileStreamResult classes are derived class from Abstract FileResult class.

FileStreamResult – Sends binary content to the response by using a Stream instance. Here you have a stream and want to return stream content as a file.

FileContentResult – Sends the contents of a binary file to the response. Here you have a byte array and want to return byte content as a file.

References:

That’s all! Happy coding!

Does this help you fix your issue?

Do you have any better solutions or suggestions? Please sound off your comments below.



Please bookmark this page and share it with your friends. Please Subscribe to the blog to receive notifications on freshly published(2024) best practices and guidelines for software design and development.



3 thoughts on “C#. NET – Open file in browser instead of downloading

Leave a Reply

Your email address will not be published. Required fields are marked *