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.

Updated November 2026: a section on how File() chooses inline or attachment, a troubleshooting checklist and an FAQ.

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-downloading,C#. NET - Open file in browser instead of downloading

Why File() Downloads or Displays: fileDownloadName and Content-Disposition

The Content-Disposition response header tells the browser to show the file (inline) or save it (attachment). In ASP.NET Core, the fileDownloadName argument of File() is what switches that header to attachment.

Note: Think of Content-Disposition as a sticker on a parcel. “Open now” means the browser unwraps it in the tab. “Keep” means it goes straight to the downloads folder. The browser can still ignore the sticker if its own settings say otherwise.

Why does my File() action always download?

You most likely passed a file name. File(bytes, contentType, "report.pdf") adds Content-Disposition: attachment; filename="report.pdf". File(bytes, contentType) and File(stream, contentType) send no attachment header, so the browser renders the file inline.

How do you keep a file name and stay inline?

Build the header yourself and add it to Response.Headers before returning File() without a name. Use ContentDispositionHeaderValue from Microsoft.Net.Http.Headers, set the disposition type to inline, and set the file name with SetHttpFileName. That encodes non-ASCII names correctly.

Older MVC samples use System.Net.Mime.ContentDisposition with Inline = true. It still compiles, but prefer the ASP.NET Core type on current .NET.

Does the Content-Type really matter?

Yes. Send the real type, such as application/pdf, image/png or image/jpeg. With application/octet-stream the browser has nothing to render and will usually save the file.

Which result type does each File() overload return?

The argument you pass decides the result class and how much memory the response needs:

SourceResult typeNote
byte[]FileContentResultWhole file sits in memory
StreamFileStreamResultStream is disposed after the response
PhysicalFilePhysicalFileResultAbsolute path on disk
Web-root virtual pathVirtualFileResultPath relative to wwwroot

For large files, stream from disk or storage instead of loading a byte[]. Otherwise every concurrent request holds a full copy of the file in memory.

When should you enable range processing?

Set enableRangeProcessing to true for large PDFs, video or audio. It defaults to false. When enabled, clients can request byte ranges and get 206 Partial Content, so a viewer can show the first pages or seek in a video without downloading everything.

Does the same rule apply to minimal APIs?

Yes. Results.File and Results.Stream follow the same fileDownloadName rule. Leave the name out for inline display, and pass it only when you want a download.

Tip: If the header looks right and the file still downloads, check the browser. Chrome and Edge both have a PDF setting that forces downloads.

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 (2026) best practices and guidelines for software design and development.



Frequently asked questions

Why does my File() result always download the file in ASP.NET Core?

Usually because you passed the fileDownloadName argument, which makes ASP.NET Core add Content-Disposition: attachment to the response. Drop the name and call File(bytes, "application/pdf") to get an inline response. If it still downloads, check that the Content-Type isn’t application/octet-stream, and that no middleware or reverse proxy adds its own attachment header.

What is the difference between Content-Disposition inline and attachment?

Inline tells the browser to try to render the file in the window or tab with a built-in viewer, such as the PDF viewer. Attachment tells it to save the file or show a save dialog. Both values can carry a filename parameter, so the filename alone doesn’t decide the behaviour.

Why does my PDF still download even though I set Content-Disposition inline?

The browser has the final say. Chrome and Edge both have a setting that downloads PDFs instead of opening them, and the user may have turned it on. Also check that the Content-Type is application/pdf, that an HTML download attribute on a same-origin link isn’t overriding your header, and that a proxy or middleware isn’t rewriting the header on the way out.

How do I set a filename for an inline file without forcing a download?

Don’t pass fileDownloadName to File(). Build the header yourself with ContentDispositionHeaderValue from Microsoft.Net.Http.Headers: create it with "inline", call SetHttpFileName so non-ASCII names are encoded correctly, and assign ToString() to Response.Headers.ContentDisposition. Then return File(bytes, contentType) or File(stream, contentType) as usual.

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 *