asp.net 实现word在线阅读

5 篇文章 0 订阅

最近接到一个案子需要实现把上传的word档案,在网页里面进行阅读,类似于百度文库。

1.最初的思路是用FlashPaper在线把word直接专程swf格式,结果放到iis就不能实现了,网上找了好多种解决方案都不能实现最终无赖只好放弃。

2.后来就用了折中的办法,把word文件上传的时候先转成pdf格式,然后再把pdf格式专程swf格式,最终用户看到的是swf格式。就实现了在线阅读的功能

现在把过程和代码给大家献上:

   1.先下载安装swftools工具:swftools下载

   2.然后下载FlexPaper:FlexPaper下载

   3.下载转word转pdf的aspose.word此dll可以不用下,我已经加到例子里面了,可以在例子里面找到

代码例子在:完成代码例子

html代码

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="FLexpaper.aspx.cs" Inherits="onlineRead_FLexpaper" %>

<!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">
     <input type="hidden" id="file" value='<%=FileURL %>' />
    <div style="position:absolute;left:10px;top:10px; height: 89px;">
               
           
        </div>
    <br />
    <asp:FileUpload ID="FileUpload1" runat="server" />
    <asp:Button ID="Button1" runat="server" οnclick="Button1_Click" Text="上传并转换" />
     <br />
    <br />
     <br />
                                            <asp:TextBox ID="txtwjnum" 
        runat="server"></asp:TextBox>
                                            <asp:TextBox ID="txtoldver" 
        runat="server"></asp:TextBox>
                                            <asp:HyperLink ID="HyperLink1" 
        runat="server" Target="_blank">na</asp:HyperLink>
    </form>
</body>
</html>
C#代码

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Aspose.Words;
using Aspose.Words.Reporting;
using Aspose.Words.Saving;
using System.IO;
using System.Text;
using System.Collections;

using System.Diagnostics;


public partial class onlineRead_FLexpaper : System.Web.UI.Page
{ 
    public string sql;
    public DataSet ds;
    public string constr = PublicFun.PublicFunction.GetDBconstr("ce_manage_db");
    public string type;
    static string newnum;
    static string strbody;
    static string UploadURL, UploadURL2;
    public string FileURL
    {
        get;
        set;
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["id"] != null)
        {
            FileURL = Request.QueryString["id"].ToString();
        }
    }
   
    private static void ConvertCmd(string fileName,string oldsource)
    {
       
        using (Process p = new Process())
        {
            string cmdStr = HttpContext.Current.Server.MapPath("~/onlineRead/SWFTOOLS/pdf2swf.exe");
            string savePath = HttpContext.Current.Server.MapPath("~/swfroot/");
            // @"""" 相当于一个双引号,之所以要加@"""" 就是为了防止要转换的过程中,文件夹名字带有空格,导致失败
            string sourcePath = @"""" + oldsource + @"""";
            string targetPath = @"""" + savePath + fileName.Substring(0, fileName.LastIndexOf(".")) + ".swf" + @"""";
            string argsStr = "  -t " + sourcePath + " -s flashversion=9 -o " + targetPath;
         
            //调用新进程 进行转换
            ProcessStartInfo psi = new ProcessStartInfo(cmdStr, argsStr);
            p.StartInfo = psi;
            p.Start();
            p.WaitForExit();
           
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        uploadfile();     
    }
    private void uploadfile()
    {

        string FilePath = "";
        if (FileUpload1.FileName != "")
        {
            if (FileUpload1.PostedFile.ContentLength <= 0)
            {
                PublicFun.PublicFunction.showMsg(this, "上傳文件為空,請重新選擇");
            }
            else
            {
            }

            if (FileUpload1.HasFile)
            {
                if (FileUpload1.PostedFile.ContentLength > 4196304)
                {
                }
                else
                {
                }

                FilePath = Server.MapPath("~/onlineRead/officeroot");//服務器文件路徑

                string hzfname = FileUpload1.FileName.Substring(FileUpload1.FileName.LastIndexOf(".") + 1, FileUpload1.FileName.Length - FileUpload1.FileName.LastIndexOf(".") - 1);//獲得文件後綴名;
                string oldfilename = txtwjnum.Text.Trim() + "_" + txtoldver.Text.Trim() + "." + hzfname;
                string newfilename = txtwjnum.Text.Trim() + "_" + txtoldver.Text.Trim() + ".html";
                string newpdffilename = txtwjnum.Text.Trim() + "_" + txtoldver.Text.Trim() + ".pdf";
                string newswffilename = txtwjnum.Text.Trim() + "_" + txtoldver.Text.Trim() + ".swf";
                string input = Server.MapPath("~/onlineRead/swfroot/" + oldfilename + "");//上傳的文件路徑
                if (File.Exists(input))
                {
                    File.Delete(input);
                }
                FileUpload1.SaveAs(FilePath + "\\" + txtwjnum.Text.Trim() + "_" + txtoldver.Text.Trim() + "." + hzfname);
                PublicFun.PublicFunction.showMsg(this, "附件上傳成功");

                //  SaveAshtmlpage(oldfilename, newfilename);//原來檔案轉化為html
                SaveAspdf(oldfilename, newpdffilename);//原來檔案轉化為pdf
                string output = Server.MapPath("~/onlineRead/swfroot/" + newswffilename + "");
                // UploadURL = "~/onlineRead/swfroot/" + newfilename + "";//轉化後格式for html
                UploadURL = "~/onlineRead/swfroot/" + newswffilename + "";//轉化後格式for swf
                UploadURL2 = "~/onlineRead/officeroot/" + oldfilename + "";//原來格式要存到DB的url2

                string swfname = UploadURL.Substring(UploadURL.LastIndexOf("/") + 1);
                HyperLink1.NavigateUrl = "~/onlineRead/viewer2.aspx?id=" + HttpUtility.HtmlEncode(swfname);
                //HyperLink1.NavigateUrl = "~/onlineRead/swfroot/" + newswffilename + "";//for swf
                HyperLink1.Text = output.Substring(output.LastIndexOf("\\") + 1);


               



            }
            else
            {


            }
        }
    }
    private void SaveAshtmlpage(string oldfilename, string newfilename)
    {
        Document tocDoc = new Document(Server.MapPath("~/onlineRead/officeroot/" + oldfilename + ""));
        MemoryStream stream = new MemoryStream();
        tocDoc.Save(Server.MapPath("~/onlineRead/swfroot/" + newfilename + ""));
    }
    private void SaveAspdf(string oldfilename, string newfilename)//word =>pdf => swf
    {
        Document tocDoc = new Document(Server.MapPath("~/onlineRead/officeroot/" + oldfilename + ""));
        MemoryStream stream = new MemoryStream();
        tocDoc.Save(Server.MapPath("~/onlineRead/pdfroot/" + newfilename + "")); //保存为pdf

        //切记,使用pdf2swf.exe 打开的文件名之间不能有空格,否则会失败
        string cmdStr = HttpContext.Current.Server.MapPath("~/onlineRead/SWFTOOLS/pdf2swf.exe");
        string pdfPath = HttpContext.Current.Server.MapPath("~/onlineRead/pdfroot/");
        string swfPath = HttpContext.Current.Server.MapPath("~/onlineRead/swfroot/");
        string sourcePath = @"""" + pdfPath + newfilename + @"""";//pdf文件路径
        string swfname = txtwjnum.Text.Trim() + "_" + txtoldver.Text.Trim() + ".swf";
        string targetPath = @"""" + swfPath + swfname + @"""";//swf文件路径
        //@"""" 四个双引号得到一个双引号,如果你所存放的文件所在文件夹名有空格的话,要在文件名的路径前后加上双引号,才能够成功
        // -t 源文件的路径
        // -s 参数化(也就是为pdf2swf.exe 执行添加一些窗外的参数(可省略))
        string argsStr = "  -t " + sourcePath + " -s flashversion=9 -o " + targetPath;
        ExcutedCmd(cmdStr, argsStr);
    }
    private static void ExcutedCmd(string cmd, string args)
    {
        using (Process p = new Process())
        {
            ProcessStartInfo psi = new ProcessStartInfo(cmd, args);
            p.StartInfo = psi;
            p.Start();
            p.WaitForExit();
        }
    }

    

    
}

显示swf的页面html

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="viewer2.aspx.cs" Inherits="onlineRead_viewer2" %>

<!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>
    <script src="js/jquery.js" type="text/javascript"></script>
     <script src="js/flexpaper_flash_debug.js" type="text/javascript"></script>
    <script src="js/flexpaper_flash.js" type="text/javascript"></script>
</head>
<body>
    <form id="form1" runat="server">
    <div style="position: absolute; left: 20px; top: 20px;">
        <a id="viewerPlaceHolder" 
            style="width: 949px; height: 688px; display: block; background-color: #CCFFFF;"></a>
        <!--设置一个隐蔽控件来得到要显示的文件的名字-->
        <input type="hidden" id="file" value='<%=FileURL %>' />
        <script type="text/javascript">
        
        var fileURL=$("#file").val();
            var fp = new FlexPaperViewer(
        'FlexPaperViewer',
        'viewerPlaceHolder',
        { config: {
            SwfFile : escape('../onlineRead/swfroot/'+fileURL),  <!--这句是关键: SwfFile: 指示导入的.swf的路径-->
            Scale: 1.0,
            ZoomTransition: 'easeOut',
            ZoomTime: 0.5,
            ZoomInterval: 0.2,
            FitPageOnLoad: false,
            FitWidthOnLoad: false,
            PrintEnabled: false,<!-- 打印 true为可以,false为不可以-->
            FullScreenAsMaxWindow: false,
            ProgressiveLoading: false,
            MinZoomSize: 0.2,
            MaxZoomSize: 5,
            SearchMatchAll: false,
            InitViewMode: 'Portrait',
            ViewModeToolsVisible: true,
            ZoomToolsVisible: true,
            NavToolsVisible: true,
            CursorToolsVisible: true,
            SearchToolsVisible: true,
            localeChain: 'en_US'
        }
        }
        );
        </script>
    </div>
    </form>
</body>
</html>

显示swf的c#代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class onlineRead_viewer2 : System.Web.UI.Page
{
    public string FileURL
    {
        get;
        set;
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["id"] != null)
        {
            FileURL = Request.QueryString["id"].ToString();
        }
    }
   
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

cheug

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值