Returning a file to View/Download in ASP.NET MVC -


i'm encountering problem sending files stored in database user in asp.net mvc. want view listing 2 links, 1 view file , let mimetype sent browser determine how should handled, , other force download.

if choose view file called somerandomfile.bak , browser doesn't have associated program open files of type, have no problem defaulting download behavior. however, if choose view file called somerandomfile.pdf or somerandomfile.jpg want file open. want keep download link off side can force download prompt regardless of file type. make sense?

i have tried filestreamresult , works files, it's constructor doesn't accept filename default, unknown files assigned file name based on url (which not know extension give based on content type). if force file name specifying it, lose ability browser open file directly , download prompt. has else encountered this.

these examples of i've tried far.

//gives me download prompt. return file(document.data, document.contenttype, document.name); 

//opens if known extension type, downloads otherwise (download has bogus name , missing extension) return new filestreamresult(new memorystream(document.data), document.contenttype); 

//gives me download prompt (lose ability open default if known type) return new filestreamresult(new memorystream(document.data), document.contenttype) {filedownloadname = document.name}; 

any suggestions?

public actionresult download() {     var document = ...     var cd = new system.net.mime.contentdisposition     {         // example foo.bak         filename = document.filename,           // prompt user downloading, set true if want          // browser try show file inline         inline = false,      };     response.appendheader("content-disposition", cd.tostring());     return file(document.data, document.contenttype); } 

note: example code above fails account international characters in filename. see rfc6266 relevant standardization. believe recent versions of asp.net mvc's file() method , contentdispositionheadervalue class accounts this. - oskar 2016-02-25


Comments