Note that there are some explanatory texts on larger screens.

plurals
  1. POReturning a file to View/Download in ASP.NET MVC
    primarykey
    data
    text
    <p>I'm encountering a problem sending files stored in a database back to the user in ASP.NET MVC. What I want is a view listing two links, one to view the file and let the mimetype sent to the browser determine how it should be handled, and the other to force a download.</p> <p>If I choose to view a file called <code>SomeRandomFile.bak</code> and the browser doesn't have an associated program to open files of this type, then I have no problem with it defaulting to the download behavior. However, if I choose to view a file called <code>SomeRandomFile.pdf</code> or <code>SomeRandomFile.jpg</code> I want the file to simply open. But I also want to keep a download link off to the side so that I can force a download prompt regardless of the file type. Does this make sense?</p> <p>I have tried <code>FileStreamResult</code> and it works for most files, it's constructor doesn't accept a filename by default, so unknown files are assigned the a file name based on the url (which does not know the extension to give based on content type). If I force the file name by specifying it, I lose the ability for the browser to open the file directly and I get a download prompt. Has anyone else encountered this.</p> <p>These are the examples of what I've tried so far.</p> <pre><code>//Gives me a download prompt. return File(document.Data, document.ContentType, document.Name); </code></pre> <p></p> <pre><code>//Opens if it is a known extension type, downloads otherwise (download has bogus name and missing extension) return new FileStreamResult(new MemoryStream(document.Data), document.ContentType); </code></pre> <p></p> <pre><code>//Gives me a download prompt (lose the ability to open by default if known type) return new FileStreamResult(new MemoryStream(document.Data), document.ContentType) {FileDownloadName = document.Name}; </code></pre> <p>Any suggestions?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload