`

Response.ContentType 详解

阅读更多
不同的ContentType 会影响客户端所看到的效果.默认的ContentType为 text/html 也就是网页格式.
代码如:

<% response.ContentType ="text/html" %>
<!--#i nclude virtual="/ContentType.html" -->

显示的为网页,而

<% response.ContentType ="text/plain" %>
<!--#i nclude virtual="/sscript/ContentType.html" -->

则会显示html原代码.

以下为一些常用的 ContentType
GIF images

<% response.ContentType ="image/gif" %>
<!--#i nclude virtual="/myimage.gif" -->

JPEG images

<% response.ContentType ="image/jpeg" %>
<!--#i nclude virtual="/myimage.jpeg" -->

TIFF images

<% response.ContentType ="image/tiff" %>
<!--#i nclude virtual="/myimage.tiff" -->

MICROSOFT WORD document

<% response.ContentType ="application/msword" %>
<!--#i nclude virtual="/myfile.doc" -->

RTF document

<% response.ContentType ="application/rtf" %>
<!--#i nclude virtual="/myfile.rtf" -->

MICROSOFT EXCEL document

<% response.ContentType ="application/x-excel" %>
<!--#i nclude virtual="/myfile.xls" -->

MICROSOFT POWERPOINT document

<% response.ContentType ="application/ms-powerpoint" %>
<!--#i nclude virtual="/myfile.pff" -->

PDF document

<% response.ContentType ="application/pdf" %>
<!--#i nclude virtual="/myfile.pdf" -->

ZIP document

<% response.ContentType ="application/zip" %>
<!--#i nclude virtual="/myfile.zip" -->



下面是更详细的ContentType
'ez' => 'application/andrew-inset',
'hqx' => 'application/mac-binhex40',
'cpt' => 'application/mac-compactpro',
'doc' => 'application/msword',
'bin' => 'application/octet-stream',
'dms' => 'application/octet-stream',
'lha' => 'application/octet-stream',
'lzh' => 'application/octet-stream',
'exe' => 'application/octet-stream',
'class' => 'application/octet-stream',
'so' => 'application/octet-stream',
'dll' => 'application/octet-stream',
'oda' => 'application/oda',
'pdf' => 'application/pdf',
'ai' => 'application/postscript',
'eps' => 'application/postscript',
'ps' => 'application/postscript',
'smi' => 'application/smil',
'smil' => 'application/smil',
'mif' => 'application/vnd.mif',
'xls' => 'application/vnd.ms-excel',
'ppt' => 'application/vnd.ms-powerpoint',
'wbxml' => 'application/vnd.wap.wbxml',
'wmlc' => 'application/vnd.wap.wmlc',
'wmlsc' => 'application/vnd.wap.wmlscriptc',
'bcpio' => 'application/x-bcpio',
'vcd' => 'application/x-cdlink',
'pgn' => 'application/x-chess-pgn',
'cpio' => 'application/x-cpio',
'csh' => 'application/x-csh',
'dcr' => 'application/x-director',
'dir' => 'application/x-director',
'dxr' => 'application/x-director',
'dvi' => 'application/x-dvi',
'spl' => 'application/x-futuresplash',
'gtar' => 'application/x-gtar',
'hdf' => 'application/x-hdf',
'js' => 'application/x-javascript',
'skp' => 'application/x-koan',
'skd' => 'application/x-koan',
'skt' => 'application/x-koan',
'skm' => 'application/x-koan',
'latex' => 'application/x-latex',
'nc' => 'application/x-netcdf',
'cdf' => 'application/x-netcdf',
'sh' => 'application/x-sh',
'shar' => 'application/x-shar',
'swf' => 'application/x-shockwave-flash',
'sit' => 'application/x-stuffit',
'sv4cpio' => 'application/x-sv4cpio',
'sv4crc' => 'application/x-sv4crc',
'tar' => 'application/x-tar',
'tcl' => 'application/x-tcl',
'tex' => 'application/x-tex',
'texinfo' => 'application/x-texinfo',
'texi' => 'application/x-texinfo',
't' => 'application/x-troff',
'tr' => 'application/x-troff',
'roff' => 'application/x-troff',
'man' => 'application/x-troff-man',
'me' => 'application/x-troff-me',
'ms' => 'application/x-troff-ms',
'ustar' => 'application/x-ustar',
'src' => 'application/x-wais-source',
'xhtml' => 'application/xhtml+xml',
'xht' => 'application/xhtml+xml',
'zip' => 'application/zip',
'au' => 'audio/basic',
'snd' => 'audio/basic',
'mid' => 'audio/midi',
'midi' => 'audio/midi',
'kar' => 'audio/midi',
'mpga' => 'audio/mpeg',
'mp2' => 'audio/mpeg',
'mp3' => 'audio/mpeg',
'aif' => 'audio/x-aiff',
'aiff' => 'audio/x-aiff',
'aifc' => 'audio/x-aiff',
'm3u' => 'audio/x-mpegurl',
'ram' => 'audio/x-pn-realaudio',
'rm' => 'audio/x-pn-realaudio',
'rpm' => 'audio/x-pn-realaudio-plugin',
'ra' => 'audio/x-realaudio',
'wav' => 'audio/x-wav',
'pdb' => 'chemical/x-pdb',
'xyz' => 'chemical/x-xyz',
'bmp' => 'image/bmp',
'gif' => 'image/gif',
'ief' => 'image/ief',
'jpeg' => 'image/jpeg',
'jpg' => 'image/jpeg',
'jpe' => 'image/jpeg',
'png' => 'image/png',
'tiff' => 'image/tiff',
'tif' => 'image/tiff',
'djvu' => 'image/vnd.djvu',
'djv' => 'image/vnd.djvu',
'wbmp' => 'image/vnd.wap.wbmp',
'ras' => 'image/x-cmu-raster',
'pnm' => 'image/x-portable-anymap',
'pbm' => 'image/x-portable-bitmap',
'pgm' => 'image/x-portable-graymap',
'ppm' => 'image/x-portable-pixmap',
'rgb' => 'image/x-rgb',
'xbm' => 'image/x-xbitmap',
'xpm' => 'image/x-xpixmap',
'xwd' => 'image/x-xwindowdump',
'igs' => 'model/iges',
'iges' => 'model/iges',
'msh' => 'model/mesh',
'mesh' => 'model/mesh',
'silo' => 'model/mesh',
'wrl' => 'model/vrml',
'vrml' => 'model/vrml',
'css' => 'text/css',
'html' => 'text/html',
'htm' => 'text/html',
'asc' => 'text/plain',
'txt' => 'text/plain',
'rtx' => 'text/richtext',
'rtf' => 'text/rtf',
'sgml' => 'text/sgml',
'sgm' => 'text/sgml',
'tsv' => 'text/tab-separated-values',
'wml' => 'text/vnd.wap.wml',
'wmls' => 'text/vnd.wap.wmlscript',
'etx' => 'text/x-setext',
'xsl' => 'text/xml',
'xml' => 'text/xml',
'mpeg' => 'video/mpeg',
'mpg' => 'video/mpeg',
'mpe' => 'video/mpeg',
'qt' => 'video/quicktime',
'mov' => 'video/quicktime',
'mxu' => 'video/vnd.mpegurl',
'avi' => 'video/x-msvideo',
'movie' => 'video/x-sgi-movie',
'ice' => 'x-conference/x-cooltalk'

//---------------------下面是从资源文件获取图片。并显示
public class GetImage
    {
        public static System.Drawing.Image GetSrc(string name)
        {
            System.Resources.ResourceManager rm = new ResourceManager("ClassLibrary.ResourceTestImg",
       System.Reflection.Assembly.GetExecutingAssembly());//ResourceTestImg为资源文件名称,ClassLibrary//为命名空间
            return rm.GetObject(name) as System.Drawing.Image;
        }
      
    }

显示代码
protected void Page_Load(object sender, EventArgs e)
{

            System.Drawing.Image img = ClassLibrary.GetImage.GetSrc("_15958260");
           //_15958260为在ResourceTestImg.resx资源管理窗口中看到资源图片名称
                    
            img.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
}
=====================================================

struts.xml:

<action name="formTemplate_*" class="formTemplateAction" method="{1}">
      <result name="download" type="stream">
      <!-- 指定下载文件的类型 -->
      <param name="contentType">application/msword</param>
      <!-- 指定getTargetFile()方法返回被下载的InputStream -->
      <param name="inputName">targetFile</param>
      <!-- 指定下载的文件名 -->
      <param name="contentDisposition">filename="${downloadFileName}.doc"</param>
      <!-- 指定下载文件时的缓冲大小 -->
      <param name="bufferSize">4096</param>
     </result>
  </action>

contentType类型配置:

'ez' => 'application/andrew-inset',
'hqx' => 'application/mac-binhex40',
'cpt' => 'application/mac-compactpro',
'doc' => 'application/msword',
'bin' => 'application/octet-stream',
'dms' => 'application/octet-stream',
'lha' => 'application/octet-stream',
'lzh' => 'application/octet-stream',
'exe' => 'application/octet-stream',
'class' => 'application/octet-stream',
'so' => 'application/octet-stream',
'dll' => 'application/octet-stream',
'oda' => 'application/oda',
'pdf' => 'application/pdf',
'ai' => 'application/postscript',
'eps' => 'application/postscript',
'ps' => 'application/postscript',
'smi' => 'application/smil',
'smil' => 'application/smil',
'mif' => 'application/vnd.mif',
'xls' => 'application/vnd.ms-excel',
'ppt' => 'application/vnd.ms-powerpoint',
'wbxml' => 'application/vnd.wap.wbxml',
'wmlc' => 'application/vnd.wap.wmlc',
'wmlsc' => 'application/vnd.wap.wmlscriptc',
'bcpio' => 'application/x-bcpio',
'vcd' => 'application/x-cdlink',
'pgn' => 'application/x-chess-pgn',
'cpio' => 'application/x-cpio',
'csh' => 'application/x-csh',
'dcr' => 'application/x-director',
'dir' => 'application/x-director',
'dxr' => 'application/x-director',
'dvi' => 'application/x-dvi',
'spl' => 'application/x-futuresplash',
'gtar' => 'application/x-gtar',
'hdf' => 'application/x-hdf',
'js' => 'application/x-javascript',
'skp' => 'application/x-koan',
'skd' => 'application/x-koan',
'skt' => 'application/x-koan',
'skm' => 'application/x-koan',
'latex' => 'application/x-latex',
'nc' => 'application/x-netcdf',
'cdf' => 'application/x-netcdf',
'sh' => 'application/x-sh',
'shar' => 'application/x-shar',
'swf' => 'application/x-shockwave-flash',
'sit' => 'application/x-stuffit',
'sv4cpio' => 'application/x-sv4cpio',
'sv4crc' => 'application/x-sv4crc',
'tar' => 'application/x-tar',
'tcl' => 'application/x-tcl',
'tex' => 'application/x-tex',
'texinfo' => 'application/x-texinfo',
'texi' => 'application/x-texinfo',
't' => 'application/x-troff',
'tr' => 'application/x-troff',
'roff' => 'application/x-troff',
'man' => 'application/x-troff-man',
'me' => 'application/x-troff-me',
'ms' => 'application/x-troff-ms',
'ustar' => 'application/x-ustar',
'src' => 'application/x-wais-source',
'xhtml' => 'application/xhtml+xml',
'xht' => 'application/xhtml+xml',
'zip' => 'application/x-zip',
'au' => 'audio/basic',
'snd' => 'audio/basic',
'mid' => 'audio/midi',
'midi' => 'audio/midi',
'kar' => 'audio/midi',
'mpga' => 'audio/mpeg',
'mp2' => 'audio/mpeg',
'mp3' => 'audio/mpeg',
'aif' => 'audio/x-aiff',
'aiff' => 'audio/x-aiff',
'aifc' => 'audio/x-aiff',
'm3u' => 'audio/x-mpegurl',
'ram' => 'audio/x-pn-realaudio',
'rm' => 'audio/x-pn-realaudio',
'rpm' => 'audio/x-pn-realaudio-plugin',
'ra' => 'audio/x-realaudio',
'wav' => 'audio/x-wav',
'pdb' => 'chemical/x-pdb',
'xyz' => 'chemical/x-xyz',
'bmp' => 'image/bmp',
'gif' => 'image/gif',
'ief' => 'image/ief',
'jpeg' => 'image/jpeg',
'jpg' => 'image/jpeg',
'jpe' => 'image/jpeg',
'png' => 'image/png',
'tiff' => 'image/tiff',
'tif' => 'image/tiff',
'djvu' => 'image/vnd.djvu',
'djv' => 'image/vnd.djvu',
'wbmp' => 'image/vnd.wap.wbmp',
'ras' => 'image/x-cmu-raster',
'pnm' => 'image/x-portable-anymap',
'pbm' => 'image/x-portable-bitmap',
'pgm' => 'image/x-portable-graymap',
'ppm' => 'image/x-portable-pixmap',
'rgb' => 'image/x-rgb',
'xbm' => 'image/x-xbitmap',
'xpm' => 'image/x-xpixmap',
'xwd' => 'image/x-xwindowdump',
'igs' => 'model/iges',
'iges' => 'model/iges',
'msh' => 'model/mesh',
'mesh' => 'model/mesh',
'silo' => 'model/mesh',
'wrl' => 'model/vrml',
'vrml' => 'model/vrml',
'css' => 'text/css',
'html' => 'text/html',
'htm' => 'text/html',
'asc' => 'text/plain',
'txt' => 'text/plain',
'rtx' => 'text/richtext',
'rtf' => 'text/rtf',
'sgml' => 'text/sgml',
'sgm' => 'text/sgml',
'tsv' => 'text/tab-separated-values',
'wml' => 'text/vnd.wap.wml',
'wmls' => 'text/vnd.wap.wmlscript',
'etx' => 'text/x-setext',
'xsl' => 'text/xml',
'xml' => 'text/xml',
'mpeg' => 'video/mpeg',
'mpg' => 'video/mpeg',
'mpe' => 'video/mpeg',
'qt' => 'video/quicktime',
'mov' => 'video/quicktime',
'mxu' => 'video/vnd.mpegurl',
'avi' => 'video/x-msvideo',
'movie' => 'video/x-sgi-movie',
'ice' => 'x-conference/x-cooltalk'



=====================================
response.setContentType("application/vnd.ms-excel");
  response.addHeader("content-disposition",
    "inline;  filename=abc.xls");//inline attachment有什么区别?

-------------

Response.AddHeader("content-type","application/x-msdownload");
Response.AddHeader("Content-Disposition","attachment;filename=要下载的文件名.rar");

刷新页面
Response.AddHeader “REFRESH”, ”60;URL=newpath/newpage.asp”
这等同于客户机端<META>元素:
<META HTTP-EQUIV=”REFRESH”, “60;URL=newpath/newpage.asp”

页面转向
Response.Status = “302 Object Moved”
Response.Addheader “Location”, “newpath/newpage.asp”
这等同于使用Response.Redirect方法:
Response.Redirect “newpath/newpage.asp”

强制浏览器显示一个用户名/口令对话框

Response.Status= “401 Unauthorized”
Response.Addheader “WWW-Authenticate”, “BASIC”
强制浏览器显示一个用户名/口令对话框,然后使用BASIC验证把它们发送回服务器(将在本书后续部分看到验证方法)。

如何让网页不缓冲
Response.Expires = 0
Response.ExpiresAbsolute = Now() - 1
Response.Addheader "pragma","no-cache"
Response.Addheader "cache-control","private"
Response.CacheControl = "no-cache

分享到:
评论

相关推荐

    关于asp.net的ContentType输出格式

    ### ASP.NET中的ContentType详解 #### 一、ContentType概念与作用 在ASP.NET应用程序中,`ContentType`是一个非常重要的属性,它决定了服务器响应时所发送的数据类型。浏览器通过这个信息来解析响应的内容,从而...

    NET四种文件下载方式

    Response.ContentType = "application/x-zip-compressed"; Response.AddHeader("Content-Disposition", "attachment;filename=keji.rar"); string filename = Server.MapPath("keji.rar"); Response....

    DataTable导出为word,excel,html,csv,pdf

    HttpContext.Current.Response.ContentType = "application/ms-word"; control.EnableViewState = false; System.IO.StringWriter swOut = new System.IO.StringWriter(); HtmlTextWriter hTw = new ...

    asp.net上传、下载常用方法

    ### ASP.NET中的文件上传与下载方法详解 在ASP.NET应用开发过程中,文件的上传与下载是常见的需求之一。本文将详细介绍几种常用的文件上传与下载的方法,并通过具体的代码示例进行说明。 #### 一、文件上传的基本...

    下载功能代码

    #### Response.TransmitFile() 方法详解 `Response.TransmitFile`是ASP.NET框架提供的一个高效传输文件到客户端的方法。它通过直接将文件发送到HTTP响应流中,避免了在服务器内存中的缓存操作,从而显著减少了对...

    C# 将数据导出到Execl汇总

    HttpContext.Current.Response.ContentType = "application/ms-excel"; ctl.Page.EnableViewState = false; System.IO.StringWriter tw = new System.IO.StringWriter(); System.Web.UI.HtmlTextWriter hw = new...

    asp.net中导出Execl的方法

    ### ASP.NET中导出Excel的方法详解 #### 一、前言 在Web应用程序开发中,经常需要将数据导出到Excel文件中供用户下载或离线查看。ASP.NET提供了多种方式来实现这一功能,其中一种常见的方式是通过DataGrid控件来...

    node.js中的http.response.getHeader方法使用说明

    ### `http.response.getHeader`方法详解 `response.getHeader(name)`方法的语法非常简洁,它接受一个参数`name`,这个参数代表了要查询的响应头字段的名称。由于HTTP协议规定,头字段名称是不区分大小写的,因此在...

    jQuery post数据至ashx实例详解

    context.Response.ContentType = "application/json"; context.Response.ContentEncoding = Encoding.UTF8; Stream inputStream = context.Request.InputStream; Encoding encoding = context.Request.Content...

    C#数据导出

    ### C# 数据导出知识点详解 #### 一、概述 在C#开发中,数据导出是一项非常实用的功能,尤其当需要将数据以特定格式(如Excel)分享或备份时。本文将详细介绍如何使用C#实现数据导出,并提供一个实际的例子——基于...

    asp.net动态生成txt文件并下载

    ### ASP.NET 动态生成TXT文件并下载的知识点详解 #### 一、ASP.NET环境介绍与准备工作 在深入了解如何在ASP.NET中动态生成并下载TXT文件之前,我们首先需要了解ASP.NET的基本概念及其运行环境。 - **ASP.NET简介*...

    ASP内置对象Response详解.doc

    ASP内置对象Response是ASP(Active Server Pages)编程中用于向客户端发送信息的关键组件。它提供了多种功能,包括设置HTTP响应头、控制缓存、发送文本、重定向等。以下是Response对象的详细介绍: **Response对象...

    实现服务器文件下载,使你一看就明白

    - `Response.ContentType`指定文件的MIME类型,这里设为"application/ms-excel",表示文件通常会被Excel打开,但也可以根据实际文件类型进行调整。 - 使用`Response.WriteFile(file.FullName)`将文件内容写入HTTP...

    FileDown类

    HttpContext.Current.Response.ContentType = "application/octet-stream"; HttpContext.Current.Response.WriteFile(destFileName); HttpContext.Current.Response.Flush(); HttpContext.Current.Response.End...

    asp.net高级指南

    Response.ContentType = "image/vnd.wap.wbmp" Response.BinaryWrite StoB(img) %&gt; ``` **解释:** - `StoB` 函数用于将字符串转换为对应的二进制表示。 - `img` 变量定义了 WBMP 图像的头部信息。 - `Response....

    C#数据表中的数据导出到excel(web,WinFrom)

    ### C# 数据表中的数据导出到 Excel 的方法详解 #### 一、Web 环境下导出数据到 Excel 在 Web 开发中,经常需要将数据表中的数据导出为 Excel 文件以便用户下载。这里介绍一种使用 C# 在 ASP.NET Web 应用程序中...

    页面导出EXCEL

    curContext.Response.ContentType = "application/vnd.ms-excel"; // 设置编码方式为 GB2312 curContext.Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312"); // 清空字符集设置 ...

    导入导出excel C#

    ### 导入导出Excel使用C#的知识点详解 #### 一、背景介绍 在实际工作中,Excel作为数据处理的一种常见格式,与之相关的数据交换需求频繁出现。使用C#进行Excel的导入导出操作是一种高效的方式。本文将详细介绍如何...

Global site tag (gtag.js) - Google Analytics