Make a file open in browser instead of downloading it

C#asp.net MvcAzure

C# Problem Overview


I have an MVC project that will display some documents to users. The files are currently stored in Azure blob storage.

Currently, the documents are retrieved from the following controller action:

[GET("{zipCode}/{loanNumber}/{classification}/{fileName}")]
public ActionResult GetDocument(string zipCode, string loanNumber, string classification, string fileName)
{
    // get byte array from blob storage
    byte[] doc = _docService.GetDocument(zipCode, loanNumber, classification, fileName);
    string mimeType = "application/octet-stream";
    return File(doc, mimeType, fileName);
}

Right now, when a user clicks on a link like the following:

<a target="_blank" href="http://...controller//GetDocument?zipCode=84016&loanNumber=12345678classification=document&fileName=importantfile.pdf

Then, the file downloads to their browser's downloads folder. What I would like to happen (and I thought was default behavior) is for the file to simply be displayed in the browser.

I have tried changing the mimetype and changing the return type to FileResult instead of ActionResult, both to no avail.

How can I make the file display in the browser instead of downloading?

C# Solutions


Solution 1 - C#

Thanks to all the answers, the solution was a combination of all of them.

First, because I was using a byte[] the controller action needed to be FileContentResult not just FileResult. Found this thanks to: https://stackoverflow.com/questions/1187261/whats-the-difference-between-the-four-file-results-in-asp-net-mvc

Second, the mime type needed to NOT be a octet-stream. Supposedly, using the stream causes the browser to just download the file. I had to change the type application/pdf. I will need to explore a more robust solution to handle other file/mime types though.

Third, I had to add a header that changed the content-disposition to inline. Using this post I figured out I had to modify my code to prevent duplicate headers, since the content-disposition was already being set to attachment.

The successful code:

public FileContentResult GetDocument(string zipCode, string loanNumber, string classification, string fileName)
{
    byte[] doc = _docService.GetDocument(zipCode, loanNumber, classification, fileName);
    string mimeType = "application/pdf"
    Response.AppendHeader("Content-Disposition", "inline; filename=" + fileName);
    return File(doc, mimeType);
} 

Solution 2 - C#

It looks like someone else asked a similar question a while ago:

how to force pdf files to open in a browser

With an answer saying that you should use the header:

Content-Disposition: inline; filename.pdf

Solution 3 - C#

Browsers should decide on downloading or displaying based on mime-type.

Try this:

string mimeType = "application/pdf";

Solution 4 - C#

Just return PhysicalFileResult and use HttpGet method ,url will open pdf file

public ActionResult GetPublicLink()
{
     path = @"D:\Read\x.pdf";
    return new PhysicalFileResult(path, "application/pdf");
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTrevorView Question on Stackoverflow
Solution 1 - C#TrevorView Answer on Stackoverflow
Solution 2 - C#weleganView Answer on Stackoverflow
Solution 3 - C#Andre PenaView Answer on Stackoverflow
Solution 4 - C#DivyaView Answer on Stackoverflow