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);
            }
        }
  • 相关阅读:
    读取exec返回值
    List
    面向对象设计原则
    CascadingDropDown省市县无刷新联动
    读写配置文件app.config
    变向实现动态水晶报表
    JS验证是否日期格式
    C#中调用API(转)
    (转自老赵Jeffrey Zhao)The status code returned from the server was: 12031”。(转)
    利用Javascript的“函数重载”实现自定义Alert样式
  • 原文地址:https://www.cnblogs.com/94cool/p/9203506.html
Copyright © 2011-2022 走看看