您的位置:首页 > 其它

WebApi 文件下载功能实例

2014-01-02 08:48 369 查看
/// 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;
}




阅读更多
                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: