public class FileDownLoadController : BaseController { private string FileFolder = ConfigurationManager.AppSettings["DownFile"];//获取值 #region 下载 #region 下载文件服务器方法 /// <summary> /// 下载文件服务器方法 /// </summary> /// <param name="fileName"></param> /// <returns></returns> public HttpResponseMessage GetDownLoad(string fileName) { HttpResponseMessage result = null; try { DirectoryInfo directoryInfo = new DirectoryInfo(FileFolder); FileInfo foundFileInfo = directoryInfo.GetFiles().Where(x => x.Name == fileName).FirstOrDefault(); if (foundFileInfo != null) { FileStream fs = new FileStream(foundFileInfo.FullName, FileMode.Open); result = new HttpResponseMessage(HttpStatusCode.OK); result.Content = new StreamContent(fs); result.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/octet-stream"); result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment"); result.Content.Headers.ContentDisposition.FileName = foundFileInfo.Name; } else { result = new HttpResponseMessage(HttpStatusCode.NotFound); } } catch (Exception ex) { log.Error(ex.StackTrace); throw ex; } return result; } #endregion #endregion }