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);
            }
        }
  • 相关阅读:
    Qt安装
    Windows下查看进程的工具
    編譯 Boost 1.35.0 (Visual Studio 2005 (VC 8.0) + Windows XP
    boost1.35.0编译日志
    Linux
    Tool
    word cup
    IIS Study
    Oracle PL/SQL语言基础1 [初级] (http://www.cnmpa.com/edu/a1/8/892f4a44496ef382.asp)
    Psychology
  • 原文地址:https://www.cnblogs.com/94cool/p/9203506.html
Copyright © 2011-2022 走看看