Asp.net MVC FileContentResult - prevent opening in browser

One of my actions with the controller returns the file to the user. I would like the user to be presented with the download (open / save) dialog box, regardless of file type. This works fine when the file type is .doc, .docx, .xlsx, etc., but when the file is .txt, .xps, .pdf (sometimes) or .html, it opens in a browser.

Is there a way to prevent the file from opening in the browser and allow the user to open it in a separate window without going from the current page?

A file request is created using jQuery $.ajax({}).

Related: if the browser processes the request and displays a pop-up window, unlike an AJAX call that receives a file connection as an answer string, this is explained by this ansher , but this question is about how to make the browser process the file in a certain way after receiving it.

+5
source share
1 answer

I do not think that you can do this with a client command or configuration. You will need to do something on the server so that it returns the content type application/octet-streamfor each file. Otherwise, the browser will look at the incoming file and decide what to do with it, based on its own rules and capabilities.

, "Content-Disposition" "attachment; filename=whatever.xyz"

+5

All Articles