• API下载文件


       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
        }
    

      

  • 相关阅读:
    Spock
    Spock
    Spock
    Spock
    Spock
    Spock
    Python3 与 Python2 的不同
    python 文件处理
    Django 数据迁移
    Python 特殊方法
  • 原文地址:https://www.cnblogs.com/lhlong/p/14758035.html
Copyright © 2020-2023  润新知