zoukankan      html  css  js  c++  java
  • C# asp.net webapi下支持文件下载输出接口

    /// <summary>
        /// 下载文件
        /// </summary>
        public class DownloadController : ApiController
        {
            /// <summary>
            /// 下载文件
            /// </summary>
            /// <returns></returns>      
            public async Task<HttpResponseMessage> Get()
            {
                try
                {
                    string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Export\list.txt");
                    if (!string.IsNullOrWhiteSpace(path) && File.Exists(path))
                    {
                        
                        string filename = Path.GetFileName(path);
                        var stream = new FileStream(path, FileMode.Open);
                        HttpResponseMessage resp = new HttpResponseMessage(HttpStatusCode.OK)
                        {
                            Content = new StreamContent(stream)
                        };
                        resp.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
                        {
                            FileName = filename
                        };
                        resp.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
                        resp.Content.Headers.ContentLength = stream.Length;
                      
                        return await Task.FromResult(resp);
                    }
                }
                catch (Exception ex)
                {
                }
                return new HttpResponseMessage(HttpStatusCode.NoContent);
            }
        }
  • 相关阅读:
    [UOJ UNR #2]积劳成疾
    [UOJ UNR#2 黎明前的巧克力]
    [UOJ UNR#2 UOJ拯救计划]
    [Codeforces Round #431]简要题解
    【UOJ UNR #1】争夺圣杯
    【UOJ UNR #1】火车管理
    [UOJ UNR#1]奇怪的线段树
    [暑假的bzoj刷水记录]
    项目(一)--python3--爬虫实战
    接收端--服务器详细阐述
  • 原文地址:https://www.cnblogs.com/94cool/p/9203506.html
Copyright © 2011-2022 走看看