webapi下载文件

复制代码
[HttpGet]
        public IHttpActionResult ExportData()
        {
            
            ...
            var dt = ExcelHelper.ListToDataTable(list);
            var fileName = SysConfig.WebTempPath + Guid.NewGuid().ToString() + ".xls";
            ExcelHelper.ExportToFile(dt,fileName);
            
        //http://www.cnblogs.com/enternal/p/5417519.html HttpResponseMessage httpResponseMessage
= new HttpResponseMessage(HttpStatusCode.OK); FileStream fileStream = File.OpenRead(fileName); httpResponseMessage.Content = new StreamContent(fileStream); httpResponseMessage.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream"); httpResponseMessage.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") { FileName =HttpUtility.UrlEncode(Path.GetFileName(fileName)) }; return ResponseMessage(httpResponseMessage); }
复制代码

 

DF1E31DE-768E-43D7-B1DB-DE10BD031E13 From:http://www.cnblogs.com/xuejianxiyang/p/7802365.html

posted @   Ace001  阅读(4903)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示