How to provide a fileDownloadName only if the user requests to save the file in ASP.NET MVC?

Posted by davekaro on Stack Overflow See other posts from Stack Overflow or by davekaro
Published on 2010-05-19T15:53:47Z Indexed on 2010/05/19 16:40 UTC
Read the original article Hit count: 607

Filed under:
|

I've got a controller action that returns a FileResult like this

return this.File("file.pdf", "application/pdf");

for the URL "/Download/322" - where 322 is the id of the file.

This works great, so that if a user clicks on a link to the PDF - it will open in their web browser as long as they have a PDF plugin installed.

But, what if they right-click the link and choose "Save as..."? The browser pops up with the filename as "322." I'd like to have a better filename at this point, by doing something like this:

return this.File("file.pdf", "application/pdf", "file.pdf");

But if I change the controller to return like that, then it will always pop up the download box, since MVC is setting the Content-Disposition header to attachment (so I can't embed the file).

In summary, can I somehow detect that the user is trying to download the file vs. the file is just being embedded in something on the page?

© Stack Overflow or respective owner

Related posts about asp.net-mvc

Related posts about download