#region 文件下载
/// <summary>
/// 下载
/// </summary>
[HttpGet]
public HttpResponseMessage GetDownLoad(string filePath) //文件路径
{
try
{
HttpResponseMessage response = new HttpResponseMessage();
FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);
response.Content = new StreamContent(fileStream);
response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
HttpBrowserCapabilities browser = HttpContext.Current.Request.Browser;
if (browser.Browser.Contains("Safari")) //Safari branch
HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=" + fileInfo.FILE_NAME);
else //IE、Chrome
HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=" + HttpUtility.UrlEncode(fileInfo.FILE_NAME));
response.Content.Headers.ContentLength = filePath.Length;
}
catch (Exception ex)
{
return new HttpResponseMessage(HttpStatusCode.NoContent);
}
}
#endregion
/// <summary>
/// 下载
/// </summary>
[HttpGet]
public HttpResponseMessage GetDownLoad(string filePath) //文件路径
{
try
{
HttpResponseMessage response = new HttpResponseMessage();
FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);
response.Content = new StreamContent(fileStream);
response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
HttpBrowserCapabilities browser = HttpContext.Current.Request.Browser;
if (browser.Browser.Contains("Safari")) //Safari branch
HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=" + fileInfo.FILE_NAME);
else //IE、Chrome
HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=" + HttpUtility.UrlEncode(fileInfo.FILE_NAME));
response.Content.Headers.ContentLength = filePath.Length;
}
catch (Exception ex)
{
return new HttpResponseMessage(HttpStatusCode.NoContent);
}
}
#endregion