mvc-在线弹出新页面查看word文档

这篇博客介绍了如何在MVC应用中实现在线预览Word文档的功能。通过页面按钮触发ActionLink,调用HomeController的ViewManual方法。该方法首先检查文件是否存在,然后根据文件扩展名转换Word为HTML,最后重定向到生成的HTML页面进行预览。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.页面

 <div class="cont">
            <span class="label">操作手册</span>
            @Html.ActionLink(查看, "ViewManual", "Home", new { target = "_blank", @class = "btn main_btn" })
            <a href="/home/path" class="btn">下载</a>
        </div>

2.HomeController

引用dll

  public ActionResult ViewManual()
        {
            try
            {
                string virtualURL = ConfigurationManager.AppSettings["ManualPath"].ToString();
                string physicalPath = Server.MapPath(Server.UrlDecode(virtualURL));
                if (string.IsNullOrEmpty(virtualURL) || !System.IO.File.Exists(physicalPath))
                {
                    return Json("请先上传系统手册至站点目录下,并在配置文件(appSettings.config)中配置路径信息!", JsonRequestBehavior.AllowGet);
                }
                string extension = Path.GetExtension(physicalPath);
                string htmlUrl = "";
                switch (extension.ToLower())
                {

                    case ".doc":
                    case ".docx":
                        htmlUrl = PreviewWord(physicalPath, virtualURL);
                        break;
                }
                return Redirect(Url.Content(htmlUrl));
            }
            catch (Exception ex)
            {
                return Json(ex.Message, JsonRequestBehavior.AllowGet);
            }
        }

========================

   public static string PreviewWord(string physicalPath, string virtualURL)
        {
            string htmlName = Path.GetFileNameWithoutExtension(physicalPath) + ".html";
            if (File.Exists(Path.GetDirectoryName(virtualURL)+"/"+htmlName))
                File.Delete(Path.GetDirectoryName(virtualURL) + "/" + htmlName);
            Aspose.Words.Document d = new Aspose.Words.Document(physicalPath);
            d.Save(Path.GetDirectoryName(physicalPath) + "\\" + htmlName, SaveFormat.Html);
            return Path.GetDirectoryName(virtualURL) + "\\" + htmlName; ;

        }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值