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/envoyevy
application/fractalsfif
application/futuresplashspl
application/htahta
application/internet-property-streamacx
application/mac-binhex40hqx
application/msworddoc
application/msworddot
application/octet-stream*
application/octet-streambin
application/octet-streamclass
application/octet-streamdms
application/octet-streamexe
application/octet-streamlha
application/octet-streamlzh
application/odaoda
application/olescriptaxs
application/pdfpdf
application/pics-rulesprf
application/pkcs10p10
application/pkix-crlcrl
application/postscriptai
application/postscripteps
application/postscriptps
application/rtfrtf
application/set-payment-initiationsetpay
application/set-registration-initiationsetreg
application/vnd.ms-excelxla
application/vnd.ms-excelxlc
application/vnd.ms-excelxlm
application/vnd.ms-excelxls
application/vnd.ms-excelxlt
application/vnd.ms-excelxlw
application/vnd.ms-outlookmsg
application/vnd.ms-pkicertstoresst
application/vnd.ms-pkiseccatcat
application/vnd.ms-pkistlstl
application/vnd.ms-powerpointpot
application/vnd.ms-powerpointpps
application/vnd.ms-powerpointppt
application/vnd.ms-projectmpp
application/vnd.ms-workswcm
application/vnd.ms-workswdb
application/vnd.ms-workswks
application/vnd.ms-workswps
application/winhlphlp
application/x-bcpiobcpio
application/x-cdfcdf
application/x-compressz
application/x-compressedtgz
application/x-cpiocpio
application/x-cshcsh
application/x-directordcr
application/x-directordir
application/x-directordxr
application/x-dvidvi
application/x-gtargtar
application/x-gzipgz
application/x-hdfhdf
application/x-internet-signupins
application/x-internet-signupisp
application/x-iphoneiii
application/x-javascriptjs
application/x-latexlatex
application/x-msaccessmdb
application/x-mscardfilecrd
application/x-msclipclp
application/x-msdownloaddll
application/x-msmediaviewm13
application/x-msmediaviewm14
application/x-msmediaviewmvb
application/x-msmetafilewmf
application/x-msmoneymny
application/x-mspublisherpub
application/x-msschedulescd
application/x-msterminaltrm
application/x-mswritewri
application/x-netcdfcdf
application/x-netcdfnc
application/x-perfmonpma
application/x-perfmonpmc
application/x-perfmonpml
application/x-perfmonpmr
application/x-perfmonpmw
application/x-pkcs12p12
application/x-pkcs12pfx
application/x-pkcs7-certificatesp7b
application/x-pkcs7-certificatesspc
application/x-pkcs7-certreqrespp7r
application/x-pkcs7-mimep7c
application/x-pkcs7-mimep7m
application/x-pkcs7-signaturep7s
application/x-shsh
application/x-sharshar
application/x-shockwave-flashswf
application/x-stuffitsit
application/x-sv4cpiosv4cpio
application/x-sv4crcsv4crc
application/x-tartar
application/x-tcltcl
application/x-textex
application/x-texinfotexi
application/x-texinfotexinfo
application/x-troffroff
application/x-trofft
application/x-trofftr
application/x-troff-manman
application/x-troff-meme
application/x-troff-msms
application/x-ustarustar
application/x-wais-sourcesrc
application/x-x509-ca-certcer
application/x-x509-ca-certcrt
application/x-x509-ca-certder
application/ynd.ms-pkipkopko
application/zipzip
audio/basicau
audio/basicsnd
audio/midmid
audio/midrmi
audio/mpegmp3
audio/x-aiffaif
audio/x-aiffaifc
audio/x-aiffaiff
audio/x-mpegurlm3u
audio/x-pn-realaudiora
audio/x-pn-realaudioram
audio/x-wavwav
image/bmpbmp
image/cis-codcod
image/gifgif
image/iefief
image/jpegjpe
image/jpegjpeg
image/jpegjpg
image/pipegjfif
image/svg+xmlsvg
image/tifftif
image/tifftiff
image/x-cmu-rasterras
image/x-cmxcmx
image/x-iconico
image/x-portable-anymappnm
image/x-portable-bitmappbm
image/x-portable-graymappgm
image/x-portable-pixmapppm
image/x-rgbrgb
image/x-xbitmapxbm
image/x-xpixmapxpm
image/x-xwindowdumpxwd
message/rfc822mht
message/rfc822mhtml
message/rfc822nws
text/csscss
text/h323323
text/htmlhtm
text/htmlhtml
text/htmlstm
text/iulsuls
text/plainbas
text/plainc
text/plainh
text/plaintxt
text/richtextrtx
text/scriptletsct
text/tab-separated-valuestsv
text/webviewhtmlhtt
text/x-componenthtc
text/x-setextetx
text/x-vcardvcf
video/mpegmp2
video/mpegmpa
video/mpegmpe
video/mpegmpeg
video/mpegmpg
video/mpegmpv2
video/quicktimemov
video/quicktimeqt
video/x-la-asflsf
video/x-la-asflsx
video/x-ms-asfasf
video/x-ms-asfasr
video/x-ms-asfasx
video/x-msvideoavi
video/x-sgi-moviemovie
x-world/x-vrmlflr
x-world/x-vrmlvrml
x-world/x-vrmlwrl
x-world/x-vrmlwrz
x-world/x-vrmlxaf
x-world/x-vrmlxof

当然,上面的代码还可以优化。根据这个表做一个文件类型与输出类型的对照字典集合,给一个文件路径,字符串截取取得其扩展名,然后根据其扩展名查找对应的输出类型。这样就根据文件自动查找输出的文件类型格式了,少些很多代码。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
>