• 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);
            }
        }
  • 相关阅读:
    mysql启动错误
    maven环境变量配置
    记一次服务器Tomcat优化经历
    自动定时备份删除脚本
    Tomcat网页加载速度过慢的解决方法
    tomcat运行war包报错,找不到context-root文件
    maven下配置pom.xml
    [LeetCode]题解(python):116-Populating Next Right Pointers in Each Node
    [LeetCode]题解(python):115-Distinct Subsequences
    [LeetCode]题解(python):114-Flatten Binary Tree to Linked List
  • 原文地址:https://www.cnblogs.com/94cool/p/9203506.html
Copyright © 2020-2023  润新知