Mvc4+ef5+oracle Project notes (002) Download the Excel generated by the export

Source: Internet
Author: User
Tags httpcontext

1. First write the download () method under Citycontroller

1   Publicactionresult DownLoad ()2         {3             /*the virtualpath here indicates which file you want to set up for download. 4 In the project, the system always defaults to the DownloadFile folder in the UI layer to find city information. xls. 5 then set filedownloadname for what you want to download when the name of the file is saved,6 I set this here to download the default Save as Downfilename.rar*/7             return NewDownloadresult {virtualpath ="~/downloadfile/city information. xls", Filedownloadname ="city information. xls" };8}

2. Then write the following method

1   Public classDownloadresult:actionresult2         {3              PublicDownloadresult ()4             {5             }6 7              PublicDownloadresult (stringvirtualpath)8             {9                  This. VirtualPath =virtualpath;Ten             } One  A              Public stringvirtualpath -             { -                 Get; the                 Set; -             } -  -              Public stringFiledownloadname +             { -                 Get; +                 Set; A             } at             /* - This class is primarily for VirtualPath and filedownloadname to start, - It is important to AddHeader ("content-disposition", "attachment; Filename=" + this.) Filedownloadname); - This . Filedownloadname is the new Downloadresult class in action, - Pass in the parameters, the following server.mappath is to save the download file where, depending on the user's choice.  -              */ in              Public Override voidExecuteresult (controllercontext context) -             { to                 if(!String.IsNullOrEmpty (filedownloadname)) +                 { -Context. HttpContext.Response.AddHeader ("content-disposition", the                       "attachment; Filename="+ This. Filedownloadname); *                 } $ Panax Notoginseng                 stringFilePath = context. HttpContext.Server.MapPath ( This. virtualpath); - context. HttpContext.Response.TransmitFile (FilePath); the             } +}

3.View Layer Code

1  <a [email protected] ("/city/download") iconcls="icon-save "> Download </a>

4. Complete the user at the designated address to download file files to keep the user's own choice

Mvc4+ef5+oracle Project notes (002) Download the Excel generated by the export

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.