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);
            }
        }
  • 相关阅读:
    vc++ 错误 CreateWindow
    设置某个页面的编码方式
    部署Struts2框架
    oracle之约束
    java web开发(三)、Ajax技术
    vs2008 安装出错 Microsoft Visual Studio Web 组件安装失败
    java web开发(二)、Servlet技术
    WLS Zip Distribution for Oracle WebLogic Server 12.1.1.0安装
    MyEclipse配置Weblogic服务器
    js 未结束的字符串常量错误解决方法
  • 原文地址:https://www.cnblogs.com/94cool/p/9203506.html
Copyright © 2011-2022 走看看