/// basic/download
/// <summary>
/// 压缩文件下载
/// </summary>
/// <param name="filePath "></param>
/// <returns></returns>
[HttpGet]
[ActionName("download")]
public HttpResponseMessage DownLoad(string filePath )
{
string customFileName = DateTime.Now.ToString("yyyyMMddHHmmss.rar");//客户端保存的文件名
FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);
HttpResponseMessage response = new HttpResponseMessage();
response.Content = new StreamContent(fileStream);
response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
response.Content.Headers.ContentDisposition.FileName = customFileName;
response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream"); // 这句话要告诉浏览器要下载文件
response.Content.Headers.ContentLength = new FileInfo(filePath).Length;
return response;
}WebApi 文件下载功能实例
最新推荐文章于 2022-08-24 21:43:08 发布
本文介绍了一种使用HTTP GET方法实现压缩文件下载的功能,通过指定文件路径生成客户端可保存的文件名,设置文件流和相应头信息来完成文件下载。
2271

被折叠的 条评论
为什么被折叠?



