/// <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);
        }
    }

相关文章:

  • 2021-12-22
  • 2021-12-18
  • 2021-06-25
  • 2022-02-06
  • 2022-01-15
  • 2021-08-13
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-10-24
  • 2022-01-10
  • 2022-12-23
  • 2021-06-25
  • 2022-12-23
相关资源
相似解决方案