Web页面常用文件格式文件流的输出
今天,我的同事、同学、朋友老曲过生日,玩的有点晚了,以后在和朋友一起出去玩,尽量多吃饭少喝酒。前段时间,碰到一个如何进行用流文件的形式输出文件的问题,没有找到好方法。我的同事、同学、朋友小朝,给我提供了很不错的建议,使用一般处理程序处理文件以文件流的形式输出,并将其指向对应的地址(URL或Src)。简单的测试程序:
1.VS2010建立一个空白 Web项目,新建一个ASPX页面(WebPlayer.aspx)输入如下代码:
(该文件包含一个图片显示标签、Windows Media Player播放器插件、BS Contact Control插件调用)
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebPlayer.aspx.cs" Inherits="PPTWebTest.WebPlayer" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div style="height: 300px; width: 300px; float: left"> <img src="WebPlayerDecoding.ashx?type=picture" alt="pp" /> </div> <div style="height: 300px; width: 300px; float: left; margin-left:10px"> <object id="player" height="100%" width="100%" classid="CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6"> <param name="AutoStart" value="-1"> <!--是否自动播放--> <param name="Balance" value="0"> <!--调整左右声道平衡,同上面旧播放器代码--> <param name="enabled" value="-1"> <!--播放器是否可人为控制--> <param name="EnableContextMenu" value="-1"> <!--是否启用上下文菜单--> <param name="url" value="WebPlayerDecoding.ashx?type=media"> <!--<param name="Filename" value="/blog/01.wma" valuetype="ref">--> <!--播放的文件地址--> <param name="PlayCount" value="1"> <!--播放次数控制,为整数--> <param name="rate" value="1"> <!--播放速率控制,1为正常,允许小数,1.0-2.0--> <param name="currentPosition" value="0"> <!--控件设置:当前位置--> <param name="currentMarker" value="0"> <!--控件设置:当前标记--> <param name="defaultFrame" value=""> <!--显示默认框架--> <param name="invokeURLs" value="0"> <!--脚本命令设置:是否调用URL--> <param name="baseURL" value=""> <!--脚本命令设置:被调用的URL--> <param name="stretchToFit" value="0"> <!--是否按比例伸展--> <param name="volume" value="50"> <!--默认声音大小0%-100%,50则为50%--> <param name="mute" value="0"> <!--是否静音--> <param name="uiMode" value="mini"> <!--播放器显示模式:Full显示全部;mini最简化;None不显示播放控制,只显示视频窗口;invisible全部不显示--> <param name="windowlessVideo" value="0"> <!--如果是0可以允许全屏,否则只能在窗口中查看--> <param name="fullScreen" value="0"> <!--开始播放是否自动全屏--> <param name="enableErrorDialogs" value="-1"> <!--是否启用错误提示报告--> <param name="SAMIStyle" value> <!--SAMI样式--> <param name="SAMILang" value> <!--SAMI语言--> <param name="SAMIFilename" value> <!--字幕ID--> </object> </div> <div style="height: 300px; width: 300px; float: left; margin-left: 10px"> <object classid="CLSID:4B6E3013-6E45-11D0-9309-0020AFE05CC8" id="bsContact" width="100%" height="100%"> <param name="SRC" value="WebPlayerDecoding.ashx?type=3DMax"> </object> </div> </form> </body> </html>
2.其请求的一般处理程序(WebPlayerDecoding.ashx)实现文件流的形式输出:
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Web; namespace PPTWebTest { /// <summary> /// WebPlayerDecoding 的摘要说明 /// </summary> public class WebPlayerDecoding : IHttpHandler { public void ProcessRequest(HttpContext context) { string type = context.Request["type"].ToString(); MemoryStream ms = null; string fileName = string.Empty; switch (type) { case "picture": fileName = @"E:\Pictures\images.jpg"; context.Response.ContentType = "image/jpeg"; break; case "media": fileName = @"E\教程1(上).avi"; context.Response.ContentType = "video/x-msvideo"; break; case "3DMax": fileName = @"E:\BS Contact\avatar.wrl"; context.Response.ContentType = "x-world/x-vrml"; break; } //简单流文件读取(或复杂业务操作) FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read); ms = new MemoryStream(); Byte[] bytes = new Byte[fs.Length]; fs.Read(bytes, 0, bytes.Length); ms.Write(bytes, 0, bytes.Length); ms.WriteTo(context.Response.OutputStream); ms.Close(); fs.Close();//也可以使用using进行文件流处理,注意流的关闭、释放
context.Response.End(); } public bool IsReusable { get { return false; } } } }
将本地的文件以文件流的形式读取,存入内存流然后在输出,可以很好的实现一些很特别的业务需求,以前没有很特别的使用这种方法,不过发现真的很不错。你可能会说直接在WebPlayer.aspx页面,直接指定标签或插件的URL或SRC不是很好吗,但对于一些特殊的业务需求可能不能很好的满足。
可以在一般处理程序中指定文件的输出格式,并需要在其调用的前台页面有对应的标签或插件来接受这种格式的文件流,后台通过文件流的形式输出,前台有对应的展示该文件格式的标签或插件,前后保持一致就可。BS Contact插件支持的文件格式常用的图片、音频、视频、3D模型文件等等比较多,感兴趣的朋友可以测试一些。
像导出Excel的操作《[转]DataSet导出Excel,比以往的方法导出的Excel外观更加好看》也是很不错的。
通过文件流形式输出文件的方式实现文件的展示,也同样支持其他常用格式文件的输出,如Flash文件、3D模型文件、PDF文件、Excel文件等等,常用有如下格式:
按照内容类型排列的 Mime 类型列表:参见《MIME 类型(HttpContext.Response.ContentType)列表[转]》
按照内容类型排列的 Mime 类型列表:
类型/子类型 | 扩展名 |
---|---|
application/envoy | evy |
application/fractals | fif |
application/futuresplash | spl |
application/hta | hta |
application/internet-property-stream | acx |
application/mac-binhex40 | hqx |
application/msword | doc |
application/msword | dot |
application/octet-stream | * |
application/octet-stream | bin |
application/octet-stream | class |
application/octet-stream | dms |
application/octet-stream | exe |
application/octet-stream | lha |
application/octet-stream | lzh |
application/oda | oda |
application/olescript | axs |
application/pdf | |
application/pics-rules | prf |
application/pkcs10 | p10 |
application/pkix-crl | crl |
application/postscript | ai |
application/postscript | eps |
application/postscript | ps |
application/rtf | rtf |
application/set-payment-initiation | setpay |
application/set-registration-initiation | setreg |
application/vnd.ms-excel | xla |
application/vnd.ms-excel | xlc |
application/vnd.ms-excel | xlm |
application/vnd.ms-excel | xls |
application/vnd.ms-excel | xlt |
application/vnd.ms-excel | xlw |
application/vnd.ms-outlook | msg |
application/vnd.ms-pkicertstore | sst |
application/vnd.ms-pkiseccat | cat |
application/vnd.ms-pkistl | stl |
application/vnd.ms-powerpoint | pot |
application/vnd.ms-powerpoint | pps |
application/vnd.ms-powerpoint | ppt |
application/vnd.ms-project | mpp |
application/vnd.ms-works | wcm |
application/vnd.ms-works | wdb |
application/vnd.ms-works | wks |
application/vnd.ms-works | wps |
application/winhlp | hlp |
application/x-bcpio | bcpio |
application/x-cdf | cdf |
application/x-compress | z |
application/x-compressed | tgz |
application/x-cpio | cpio |
application/x-csh | csh |
application/x-director | dcr |
application/x-director | dir |
application/x-director | dxr |
application/x-dvi | dvi |
application/x-gtar | gtar |
application/x-gzip | gz |
application/x-hdf | hdf |
application/x-internet-signup | ins |
application/x-internet-signup | isp |
application/x-iphone | iii |
application/x-javascript | js |
application/x-latex | latex |
application/x-msaccess | mdb |
application/x-mscardfile | crd |
application/x-msclip | clp |
application/x-msdownload | dll |
application/x-msmediaview | m13 |
application/x-msmediaview | m14 |
application/x-msmediaview | mvb |
application/x-msmetafile | wmf |
application/x-msmoney | mny |
application/x-mspublisher | pub |
application/x-msschedule | scd |
application/x-msterminal | trm |
application/x-mswrite | wri |
application/x-netcdf | cdf |
application/x-netcdf | nc |
application/x-perfmon | pma |
application/x-perfmon | pmc |
application/x-perfmon | pml |
application/x-perfmon | pmr |
application/x-perfmon | pmw |
application/x-pkcs12 | p12 |
application/x-pkcs12 | pfx |
application/x-pkcs7-certificates | p7b |
application/x-pkcs7-certificates | spc |
application/x-pkcs7-certreqresp | p7r |
application/x-pkcs7-mime | p7c |
application/x-pkcs7-mime | p7m |
application/x-pkcs7-signature | p7s |
application/x-sh | sh |
application/x-shar | shar |
application/x-shockwave-flash | swf |
application/x-stuffit | sit |
application/x-sv4cpio | sv4cpio |
application/x-sv4crc | sv4crc |
application/x-tar | tar |
application/x-tcl | tcl |
application/x-tex | tex |
application/x-texinfo | texi |
application/x-texinfo | texinfo |
application/x-troff | roff |
application/x-troff | t |
application/x-troff | tr |
application/x-troff-man | man |
application/x-troff-me | me |
application/x-troff-ms | ms |
application/x-ustar | ustar |
application/x-wais-source | src |
application/x-x509-ca-cert | cer |
application/x-x509-ca-cert | crt |
application/x-x509-ca-cert | der |
application/ynd.ms-pkipko | pko |
application/zip | zip |
audio/basic | au |
audio/basic | snd |
audio/mid | mid |
audio/mid | rmi |
audio/mpeg | mp3 |
audio/x-aiff | aif |
audio/x-aiff | aifc |
audio/x-aiff | aiff |
audio/x-mpegurl | m3u |
audio/x-pn-realaudio | ra |
audio/x-pn-realaudio | ram |
audio/x-wav | wav |
image/bmp | bmp |
image/cis-cod | cod |
image/gif | gif |
image/ief | ief |
image/jpeg | jpe |
image/jpeg | jpeg |
image/jpeg | jpg |
image/pipeg | jfif |
image/svg+xml | svg |
image/tiff | tif |
image/tiff | tiff |
image/x-cmu-raster | ras |
image/x-cmx | cmx |
image/x-icon | ico |
image/x-portable-anymap | pnm |
image/x-portable-bitmap | pbm |
image/x-portable-graymap | pgm |
image/x-portable-pixmap | ppm |
image/x-rgb | rgb |
image/x-xbitmap | xbm |
image/x-xpixmap | xpm |
image/x-xwindowdump | xwd |
message/rfc822 | mht |
message/rfc822 | mhtml |
message/rfc822 | nws |
text/css | css |
text/h323 | 323 |
text/html | htm |
text/html | html |
text/html | stm |
text/iuls | uls |
text/plain | bas |
text/plain | c |
text/plain | h |
text/plain | txt |
text/richtext | rtx |
text/scriptlet | sct |
text/tab-separated-values | tsv |
text/webviewhtml | htt |
text/x-component | htc |
text/x-setext | etx |
text/x-vcard | vcf |
video/mpeg | mp2 |
video/mpeg | mpa |
video/mpeg | mpe |
video/mpeg | mpeg |
video/mpeg | mpg |
video/mpeg | mpv2 |
video/quicktime | mov |
video/quicktime | qt |
video/x-la-asf | lsf |
video/x-la-asf | lsx |
video/x-ms-asf | asf |
video/x-ms-asf | asr |
video/x-ms-asf | asx |
video/x-msvideo | avi |
video/x-sgi-movie | movie |
x-world/x-vrml | flr |
x-world/x-vrml | vrml |
x-world/x-vrml | wrl |
x-world/x-vrml | wrz |
x-world/x-vrml | xaf |
x-world/x-vrml | xof |
当然,上面的代码还可以优化。根据这个表做一个文件类型与输出类型的对照字典集合,给一个文件路径,字符串截取取得其扩展名,然后根据其扩展名查找对应的输出类型。这样就根据文件自动查找输出的文件类型格式了,少些很多代码。