Saturday, March 19, 2011

File ActionResult in ASP.net MVC

          You can return a file using File ActionResult Type in ASP.net  MVC. The url will download the file which specified in the Action. Lets check some samples.
    
       MIME Types:

       Word (.doc)

      application/msword
     Word (.docx)
      application/vnd.openxmlformats-officedocument.wordprocessingml.document
        PDF (.pdf)
      application/pdf
    more

For Pdf file format It will render it on browser. In the case of a word file (.doc,.docx) it will be downloaded


         public ActionResult Help()
        {
            return File("/Content/Help.pdf""application/pdf");
        }



In Chrome


IE 9 will render the pdf file in default pdf viewer.here is the result 




Result in firefox




When we want to download a doc file. use the following action


      public ActionResult Help()
        {
            return File("/Content/sample.doc""application/msword");
        }
The .doc file sample.doc will be downloaded.
In Chrome

In IE 9







0 comments:

Post a Comment