我是靠谱客的博主 激情香水,最近开发中收集的这篇文章主要介绍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的问题的全部内容,希望文章能够帮你解决c# 解决safari浏览器导出文件后缀为.html的问题所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(62)

评论列表共有 0 条评论

立即
投稿
返回
顶部