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