c# 解决safari浏览器导出文件后缀为.html的问题

本文主要是介绍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的问题的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/732623

相关文章

IDEA编译报错“java: 常量字符串过长”的原因及解决方法

《IDEA编译报错“java:常量字符串过长”的原因及解决方法》今天在开发过程中,由于尝试将一个文件的Base64字符串设置为常量,结果导致IDEA编译的时候出现了如下报错java:常量字符串过长,... 目录一、问题描述二、问题原因2.1 理论角度2.2 源码角度三、解决方案解决方案①:StringBui

部署Vue项目到服务器后404错误的原因及解决方案

《部署Vue项目到服务器后404错误的原因及解决方案》文章介绍了Vue项目部署步骤以及404错误的解决方案,部署步骤包括构建项目、上传文件、配置Web服务器、重启Nginx和访问域名,404错误通常是... 目录一、vue项目部署步骤二、404错误原因及解决方案错误场景原因分析解决方案一、Vue项目部署步骤

C#中读取XML文件的四种常用方法

《C#中读取XML文件的四种常用方法》Xml是Internet环境中跨平台的,依赖于内容的技术,是当前处理结构化文档信息的有力工具,下面我们就来看看C#中读取XML文件的方法都有哪些吧... 目录XML简介格式C#读取XML文件方法使用XmlDocument使用XmlTextReader/XmlTextWr

mybatis和mybatis-plus设置值为null不起作用问题及解决

《mybatis和mybatis-plus设置值为null不起作用问题及解决》Mybatis-Plus的FieldStrategy主要用于控制新增、更新和查询时对空值的处理策略,通过配置不同的策略类型... 目录MyBATis-plusFieldStrategy作用FieldStrategy类型每种策略的作

linux下多个硬盘划分到同一挂载点问题

《linux下多个硬盘划分到同一挂载点问题》在Linux系统中,将多个硬盘划分到同一挂载点需要通过逻辑卷管理(LVM)来实现,首先,需要将物理存储设备(如硬盘分区)创建为物理卷,然后,将这些物理卷组成... 目录linux下多个硬盘划分到同一挂载点需要明确的几个概念硬盘插上默认的是非lvm总结Linux下多

前端原生js实现拖拽排课效果实例

《前端原生js实现拖拽排课效果实例》:本文主要介绍如何实现一个简单的课程表拖拽功能,通过HTML、CSS和JavaScript的配合,我们实现了课程项的拖拽、放置和显示功能,文中通过实例代码介绍的... 目录1. 效果展示2. 效果分析2.1 关键点2.2 实现方法3. 代码实现3.1 html部分3.2

Python Jupyter Notebook导包报错问题及解决

《PythonJupyterNotebook导包报错问题及解决》在conda环境中安装包后,JupyterNotebook导入时出现ImportError,可能是由于包版本不对应或版本太高,解决方... 目录问题解决方法重新安装Jupyter NoteBook 更改Kernel总结问题在conda上安装了

pip install jupyterlab失败的原因问题及探索

《pipinstalljupyterlab失败的原因问题及探索》在学习Yolo模型时,尝试安装JupyterLab但遇到错误,错误提示缺少Rust和Cargo编译环境,因为pywinpty包需要它... 目录背景问题解决方案总结背景最近在学习Yolo模型,然后其中要下载jupyter(有点LSVmu像一个

Goland debug失效详细解决步骤(合集)

《Golanddebug失效详细解决步骤(合集)》今天用Goland开发时,打断点,以debug方式运行,发现程序并没有断住,程序跳过了断点,直接运行结束,网上搜寻了大量文章,最后得以解决,特此在这... 目录Bug:Goland debug失效详细解决步骤【合集】情况一:Go或Goland架构不对情况二:

解决jupyterLab打开后出现Config option `template_path`not recognized by `ExporterCollapsibleHeadings`问题

《解决jupyterLab打开后出现Configoption`template_path`notrecognizedby`ExporterCollapsibleHeadings`问题》在Ju... 目录jupyterLab打开后出现“templandroidate_path”相关问题这是 tensorflo