本文主要是介绍c# 解决safari浏览器导出文件后缀为.html的问题,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
/// <summary>
/// 输出文件到浏览器
/// </summary>
/// <param name="ms">Excel文档流</param>
/// <param name="context">HTTP上下文</param>
/// <param name="fileName">文件名</param>
public static void RenderToBrowser(MemoryStream ms, HttpContext context, string fileName)
{
if (context.Request.Browser.Browser == "IE")
fileName = HttpUtility.UrlEncode(fileName);
context.Response.AddHeader("Content-Disposition", "attachment;fileName=" + fileName);
//
context.Response.AddHeader("Pragma", "public");
context.Response.AddHeader("Expires","0");
context.Response.AddHeader("Cache-Control","must-revalidate, post-check=0, pre-check=0");
context.Response.AddHeader("Content-Type", "application/force-download");
context.Response.AddHeader("Content-Type", "application/vnd.ms-excel");
context.Response.AddHeader("Content-Type", "application/octet-stream");
context.Response.AddHeader("Content-Type", "application/download"); ;
context.Response.AddHeader("Content-Transfer-Encoding", "binary");
//
context.Response.BinaryWrite(ms.ToArray());
}
原因,未告知浏览器回传的数据流是什么类型.我们需要做的就是标记header表头的MIME类型详细信息.
参考常见MIME类型:http://www.w3school.com.cn/media/media_mimeref.asp
类似图片/pdf/rar如下:
context.Response.AddHeader("Pragma", "public");
context.Response.AddHeader("Expires","0");
context.Response.AddHeader("Cache-Control","must-revalidate, post-check=0, pre-check=0");
context.Response.AddHeader("Content-Type", "application/force-download");
context.Response.AddHeader("Content-Type", "application/vnd.ms-word");//.doc
context.Response.AddHeader("Content-Type", "application/octet-stream");
context.Response.AddHeader("Content-Type", "application/download"); ;
context.Response.AddHeader("Content-Transfer-Encoding", "binary");-----------------------------------------------
context.Response.AddHeader("Pragma", "public");
context.Response.AddHeader("Expires","0");
context.Response.AddHeader("Cache-Control","must-revalidate, post-check=0, pre-check=0");
context.Response.AddHeader("Content-Type", "application/force-download");
context.Response.AddHeader("Content-Type", "application/vnd.ms-excel");//.pdf
context.Response.AddHeader("Content-Type", "application/octet-stream");
context.Response.AddHeader("Content-Type", "application/download"); ;
context.Response.AddHeader("Content-Transfer-Encoding", "binary");-----------------------------------------------
Response.AddHeader("Pragma", "public");
Response.AddHeader("Expires", "0");
Response.AddHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
Response.AddHeader("Content-Type", "application/force-download");
Response.AddHeader("Content-Type", "application/pdf");//.pdf
Response.AddHeader("Content-Type", "application/octet-stream");
Response.AddHeader("Content-Type", "application/download"); ;
Response.AddHeader("Content-Transfer-Encoding", "binary");-----------------------------------------------
Response.AddHeader("Pragma", "public");
Response.AddHeader("Expires", "0");
Response.AddHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
Response.AddHeader("Content-Type", "application/force-download");
Response.AddHeader("Content-Type", "image/jpeg");//.jpg
Response.AddHeader("Content-Type", "application/octet-stream");
Response.AddHeader("Content-Type", "application/download"); ;
Response.AddHeader("Content-Transfer-Encoding", "binary");-----------------------------------------------
Response.AddHeader("Pragma", "public");
Response.AddHeader("Expires", "0");
Response.AddHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
Response.AddHeader("Content-Type", "application/force-download");
Response.AddHeader("Content-Type", "application/x-rar-compressed");//.rar
Response.AddHeader("Content-Type", "application/octet-stream");
Response.AddHeader("Content-Type", "application/download"); ;
Response.AddHeader("Content-Transfer-Encoding", "binary");
-----------------------------------------------
这篇关于c# 解决safari浏览器导出文件后缀为.html的问题的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!