asp.net mvc 2 - How to force download in MVC2? -


i have pdf , want offer user simple "download" link. how can made?

my idea is
- compute url pdf document on server side , store in "viewmodel.pdfurl",
- add <a href=...> view calls function.
- function use

$.post("forcepdfdownload", { pdfurl: <%: model.pdfurl %> } ); 

to call serverside method:

[httppost] public jsonresult forcepdfdownload(string pdfurl) {     string path = path.getfullpath(pdfurl);     string filename = path.getfilename(pdfurl);     response.appendheader("content-disposition", "attachment; filename=" + filename);     response.contenttype = "application/pdf";     response.writefile(path);     response.end();      return null; } 

but return null; makes no sense me, methode must return something, otherwise wont visual studio compile...

any idea?

thanks in advance!

no need use json, ajax, jquery or whatever. simply:

public actionresult forcepdfdownload(string pdfurl) {     string path = path.getfullpath(pdfurl);     string filename = path.getfilename(pdfurl);     response.appendheader("content-disposition", "attachment; filename=" + filename);     return file(path, "application/pdf"); } 

and construct link:

<%: html.actionlink("download pdf", "forcepdfdownload", new { pdfurl = model.pdfurl }) %> 

be extremely careful when exposing such action on server hacker type following address in favorite browser:

http://foo.com/somecontroller/forcepdfdownload/?pdfurl=c%3a%5cmycreditcardnumbers.txt 

and live happily rest of life :-)


Comments

Popular posts from this blog

android - Spacing between the stars of a rating bar? -

aspxgridview - Devexpress grid - header filter does not work if column is initially hidden -

c# - How to execute a particular part of code asynchronously in a class -