Azure Blob How to open a file in browser without downloading it

0 votes

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?

Jun 20, 2018 in Azure by null_void
• 3,220 points
24,289 views

1 answer to this question.

0 votes

First, because I was using a byte[] the controller action needed to be FileContentResult not just FileResult. Found this thanks to: What's 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);
} 

Hope this helps!!

To know more about Azure, enroll today with our Azure certification course.

Thanks!!

answered Jun 20, 2018 by club_seesharp
• 3,450 points
Azure Blob  How to open a file in browser without downloading it  | Edureka Community
[url=http://www.gixv5831lc9r9n02406cd36bbij26xm0s.org/]uwotjmqov[/url]
wotjmqov http://www.gixv5831lc9r9n02406cd36bbij26xm0s.org/
<a href="http://www.gixv5831lc9r9n02406cd36bbij26xm0s.org/">awotjmqov</a>

Related Questions In Azure

0 votes
1 answer

How to get blob-URL after file upload in azure?

Assuming you're uploading the blobs into blob ...READ MORE

answered Sep 27, 2018 in Azure by null_void
• 3,220 points
15,781 views
0 votes
1 answer

How to download a file from Blob Storage to the browser?

If you use ASP.NET (core), you can ...READ MORE

answered Mar 26, 2019 in Azure by Prerna
• 1,960 points
7,090 views
0 votes
1 answer

Want to upload a blob file with huge size to azure in as little time as possible.

Microsoft Azure Storage Explorer is one of the ...READ MORE

answered Mar 27, 2019 in Azure by Prerna
• 1,960 points
1,301 views
0 votes
1 answer
0 votes
1 answer

How to create a deployment from a remote template file in Azure?

Hi@akhtar, You can create a deployment from a ...READ MORE

answered Nov 15, 2020 in Azure by MD
• 95,460 points
498 views
0 votes
1 answer

How can I remove/hide/disable excessive HTTP response headers in Azure/IIS7 without having to use UrlScan?

MSDN published an article on how to ...READ MORE

answered May 22, 2018 in Azure by club_seesharp
• 3,450 points
3,669 views
0 votes
2 answers

How to process a file using Azure Function?

You don't really need a http trigger ...READ MORE

answered Jun 18, 2018 in Azure by club_seesharp
• 3,450 points
7,189 views
0 votes
1 answer

How to identify a deadlock in SQL Azure?

Monitoring of SQL Azure is more limited ...READ MORE

answered Jun 19, 2018 in Azure by club_seesharp
• 3,450 points
2,532 views
0 votes
1 answer

How to upload a file on to Azure Blob storage without writing a code?

You can find the below tools useful ...READ MORE

answered Apr 13, 2018 in Azure by club_seesharp
• 3,450 points
1,508 views
0 votes
1 answer

How to get the list of names of Azure blob files in a container?

We can get some additional info like Size, ...READ MORE

answered Jun 27, 2018 in Azure by club_seesharp
• 3,450 points
21,512 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP