62,046
社区成员
发帖
与我相关
我的任务
分享
<a href="xxx.jpeg" download="下载时的文件名" >下载</a>
不过很可惜,目前只有chrome和firefox支持
图片文件,a标签再怎样,也时会被浏览器打开的,除非服务端向浏览器响应下载的指令/// <summary>
/// 输出流文件
/// </summary>
/// <param name="filePath"></param>
public static void ReturnHTTPStream(string filePath)
{
FileInfo fi = new FileInfo(filePath);
HttpContext.Current.Response.ClearHeaders();
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpContext.Current.Server.UrlEncode(fi.Name));
HttpContext.Current.Response.AddHeader("Content-Length", fi.Length.ToString());
HttpContext.Current.Response.AppendHeader("Last-Modified", fi.LastWriteTime.ToFileTime().ToString());
HttpContext.Current.Response.AppendHeader("Location", HttpContext.Current.Request.Url.AbsoluteUri);
HttpContext.Current.Response.ContentType = GetResponseContentType(fi.Extension);
HttpContext.Current.Response.WriteFile(filePath);
HttpContext.Current.Response.End();
}
/// <summary>
/// Handle the Document
/// </summary>
/// <param name="fileType"></param>
/// <returns></returns>
private static string GetResponseContentType(string fileType)
{
string result;
switch (fileType.ToLower())
{
case ".doc": result = "application/msword"; break;
case ".xls": result = "application/msexcel"; break;
case ".txt": result = "text/plain"; break;
case ".pdf": result = "application/pdf"; break;
case ".psd": result = "application/pad"; break;
case ".ppt": result = "appication/powerpoint"; break;
default: result = "application/unknown"; break;
}
return result;
}