原因:应客户需求,在系统中浏览附件内容,需要先下载到本地然后打开,对使用造成了不便,要求可以不需下载直接在浏览器中打开减少操作步骤。

领导给了3天时间,最后查找方法,写测试项目,往正式项目添加,测试,修bug,优化下来总共花费了大概两天多时间。下面给出解决经验,主要把遇到的坑

给说一下。

1.研究方案

参考:http://www.cnblogs.com/xuse/p/3710647.html

使用FlexPaper实现office文件的预览(C#版)
http://www.cnblogs.com/zzPrince/p/3378336.html

flexpaper使用:

http://www.cnblogs.com/Gnepner/archive/2011/08/19/2145493.html

通过研究发现,网上流传很多方法可以实现该需求,排除第三方控件的话,有两种比较流行,一种是把文档转化为swf格式,还有一种是转化为html实现在线预览。

但是按照网上的原话,转化为html方法很不科学,转换的文件格式丢失,仅限于IIS服务器,利用asp.net。配置麻烦,正如微软所说,读取office不是这么干的,鉴于此

先尝试另一种方案。

原理:先将office转换为PDF,再转换为SWF,最后通过网页加载Flash预览。

2.搭建测试项目

2.1工具:

a.安装Microsoft Office 2007以上版本,主要是需要用到里面四个类库,稍后列出。

b.Swftools

下载地址:http://www.swftools.org/download.html

这有个小坑,看好要下载window版。

c.flexpaper

下载地址:http://flexpaper.devaldi.com/download.htm

2.2搭建测试项目

由于本系统用的是Mvc开发,所以先建了一个MvcTest项目。

添加引用,此处有小坑,注意在引用的类库上右键,把嵌入互操作类型改为False,

引用版本最好12.0.0.0,14.0.0.0经测试也可用,网上说存在权限认证问题。

添加工具,项目文件

a.Common>Utils里面写Word,Excel,PPT等转化为PDF的方法,网上有很多,主要是Excel的转化经测试有一些有问题,贴一个靠谱的

  public static bool ExcelToPDF(string sourcePath, string targetPath)
{
bool result = false;
Excel.XlFixedFormatType targetType = Excel.XlFixedFormatType.xlTypePDF;
object missing = Type.Missing;
Excel.ApplicationClass application = null;
Excel.Workbook workBook = null;
try
{
application = new Excel.ApplicationClass();
object target = targetPath;
object type = targetType;
workBook = application.Workbooks.Open(sourcePath, missing, missing, missing, missing, missing,
missing, missing, missing, missing, missing, missing, missing, missing, missing); workBook.ExportAsFixedFormat(targetType, target, Excel.XlFixedFormatQuality.xlQualityStandard, true, false, missing, missing, missing, missing);
result = true;
}
catch
{
result = false;
}
finally
{
if (workBook != null)
{
workBook.Close(true, missing, missing);
workBook = null;
}
if (application != null)
{
application.Quit();
application = null;
}
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
}
return result; }

ExcelToPDF

b.修改控制器

先在HomeController控制器的视图里里添加一个链接

<div>
<input id="fileName" name="fileName" type="text" style="width:100px;"/>
<a href="#" onclick="preview();" >预览</a>
</div> <script type="text/javascript">
function preview() {
var name=$("#fileName").val();
window.open("/Home/Preview?name=" + name);
}
</script>

再在控制器里添加一个方法:

        public ActionResult Preview(string name="")
{
string url = "/FlexPaper/Index?filePath=~/UploadFiles/" + name;
return Redirect(url);
}

测试用RedirectToAction的方法走不到FlexPaper控制器里,貌似是路由的问题,为节省时间直接使用Redirect方法,成功进去了。

然后新建一个FlexPaperController控制器,里面写的有自己从网上down的代码,加上自己的优化,判断文件是否存在,删除旧文件,是否可转化等等提示。

 public class FlexPaperController : Controller
{ string pdf2swfToolPath = System.Web.HttpContext.Current.Server.MapPath("~/FlexPaper/pdf2swf.exe");
string png2swfToolPath = System.Web.HttpContext.Current.Server.MapPath("~/FlexPaper/png2swf.exe");
string jpeg2swfToolPath = System.Web.HttpContext.Current.Server.MapPath("~/FlexPaper/jpeg2swf.exe");
string gif2swfToolPath = System.Web.HttpContext.Current.Server.MapPath("~/FlexPaper/gif2swf.exe"); public ActionResult Index(string filePath = "")
{ string msg = CreateSWF(filePath);
if (msg == "生成成功")
{
string FilePath = Server.MapPath(filePath);
string fileNameWithoutEx = System.IO.Path.GetFileNameWithoutExtension(FilePath);//不包含路径,不包含扩展名
string extendName = System.IO.Path.GetExtension(FilePath).ToLower();//文件扩展名
var sign = extendName.Replace(".", ""); string url = "/FlexPaper/SWF/" + fileNameWithoutEx +sign+ ".swf";
try
{
return Redirect(string.Format("/FlexPaper/FlexPaperViewer.html?emotion={0}", url));
}
catch
{
return Content("转化失败,不支持该类型转化!");
}
}
else
return Content(msg);
} /// <summary>
/// 0:转化成功
/// 1:不支持的该Office文件类型到pdf的转化
/// 2:不支持的该文件类型到swf的转化
/// </summary>
/// <returns></returns>
public string CreateSWF(string filePath)
{
string FilePath = Server.MapPath(filePath);
int index = FilePath.LastIndexOf("\\");
string officePath = FilePath.Substring(, index)+"\\";
string officeName = Server.UrlDecode(FilePath.Substring(index + ));
string fileNameWithoutEx = System.IO.Path.GetFileNameWithoutExtension(FilePath);//不包含路径,不包含扩展名
string extendName = System.IO.Path.GetExtension(FilePath).ToLower();//文件扩展名
string sign = extendName.Replace(".", "");
string PdfFilePath = Server.MapPath("~/FlexPaper/PDF/");
string SWFFilePath = Server.MapPath("~/FlexPaper/SWF/"); #region delete old file
string oldPdf = PdfFilePath + fileNameWithoutEx + sign + ".pdf";
if (System.IO.File.Exists(oldPdf))
{
System.IO.File.Delete(oldPdf);
}
string oldSwf = SWFFilePath + fileNameWithoutEx + sign + ".swf";
if (System.IO.File.Exists(oldSwf))
{
System.IO.File.Delete(oldSwf);
}
#endregion if (!System.IO.File.Exists(FilePath))
{
return "找不到该文件!" ;
} if (extendName == ".doc" ||
extendName == ".docx" ||
extendName == ".xls" ||
extendName == ".ppt")
{ //string pdf2swfToolPath = string.Format("{0}\\pdf2swf.exe", Server.MapPath("~/FlexPaper")); string PdfName = Utils.OfficeToPdf(officePath, officeName, PdfFilePath);
if (PdfName == "")
{
return "不支持该Office文件类型到pdf的转化!";
}
string SwfName = Utils.PdfToSwf(pdf2swfToolPath, PdfFilePath, PdfName, SWFFilePath);
if (SwfName == "")
{
return "不支持该文件类型到swf的转化!";
}
return "生成成功";
}
else if (extendName == ".jpg" || extendName == ".jpeg" || extendName == ".png" || extendName == ".gif")
{
string toolpath = String.Empty;
switch (extendName)
{
case ".jpg":
toolpath = jpeg2swfToolPath;
break;
case ".jpeg":
toolpath = jpeg2swfToolPath;
break;
case ".png":
toolpath = png2swfToolPath;
break;
case ".gif":
toolpath = gif2swfToolPath;
break;
default:
break;
}
string SwfFileName = Utils.PictureToSwf(toolpath, officePath, officeName, SWFFilePath);
return "生成成功";
}
else
return "不支持该文件类型的转化!";
}

FlexPaperController

c.新建FlexPaper文件夹,在该文件夹下再另外创建PDF,SWF子文件夹,此处原创便于区分,另把从网上下载demo里的js,FlexPaperViewer.html,FlexPaperViewer.swf

playerProductInstall.swf文件都拷过去。

把Swftools安装路径下的工具pdf2swf.exe,也拷过去,如有需要,其他类型也可拷过去。

修改FlexPaperViewer.html便于传递文件名

<script type="text/javascript">
//获得参数的方法
var request =
{
QueryString: function (val) {
var uri = window.location.search;
var re = new RegExp("" + val + "=([^&?]*)", "ig");
return ((uri.match(re)) ? (uri.match(re)[0].substr(val.length + 1)) : null);
}
}
</script> var emotion = request.QueryString('emotion'); ; //这填写文档转成的flash文件路径
var swfFile = emotion; var flashvars = {
SwfFile: escape(swfFile),//这里用到swf路径
Scale : 0.6,
ZoomTransition : "easeOut",

UploadFiles里添加需要在线预览的office文件。

3.最后一个坑

在本地调试没有问题,发布到IIS上后,一直加载中,无法正常显示,经查找测试发现是引用office组件的权限问题。

需要在Web.config里加一段代码:

 <system.web>
<identity impersonate="true" userName="你的用户名" password="密码"/>

4.预览效果

MVC 附件在线预览的更多相关文章

  1. 如何实现QQ附件在线预览功能

    方法一:使用 openoffice 的接口把文档转换成html (linux主机或者windows主机): 方法二:使用 一个叫 jacob.jar 的工具,在安装了 office 的windows主 ...

  2. 基于开源方案构建统一的文件在线预览与office协同编辑平台的架构与实现历程

    大家好,又见面了. 在构建业务系统的时候,经常会涉及到对附件的支持,继而又会引申出对附件在线预览.在线编辑.多人协同编辑等种种能力的诉求. 对于人力不是特别充裕.或者项目投入预期规划不是特别大的公司或 ...

  3. 文件批量上传-统一附件管理器-在线预览文件(有互联网和没有两种)--SNF快速开发平台3.0

    实际上在SNF里使用附件管理是非常简单的事情,一句代码就可以搞定.但我也要在这里记录一下统一附件管理器能满足的需求. 通用的附件管理,不要重复开发,调用尽量简洁. 批量文件上传,并对每个文件大小限制, ...

  4. ASP.NET MVC在线预览Excel、Word、TXT、PDF文件

    代码: using System; using System.Collections.Generic; using System.Linq; using System.Web; using Syste ...

  5. Asp.net MVC 利用(aspose+pdfobject.js) 实现在线预览word、excel、ppt、pdf文件

    在线预览word.excel.ppt利用aspose动态生成html 主要代码 private bool OfficeDocumentToHtml(string sourceDoc, string s ...

  6. .net mvc使用FlexPaper插件实现在线预览PDF,EXCEL,WORD的方法

    FlexPaper插件可以实现在浏览器中在线预览pdf,word,excel等. 在网上看到很多关于这个插件实现预览的技术,但是很难做到word和excel在线预览. pdf很好实现. 首先下载相关的 ...

  7. 实现在线预览PDF的几种解决方案

    因客户需要实现PDF的预览处理,在网上找了一些PDF在线预览的解决方案,有的用PDFJS的在线预览方式,有的使用PDFObject的嵌入式显示,有的通过转换JPG/PNG方式实现间接显示的方式,开始是 ...

  8. Word/Excel 在线预览

    前言 近日项目中做到一个功能,需要上传附件后能够在线预览.之前也没做过这类似的,于是乎就查找了相关资料,.net实现Office文件预览大概有这几种方式: ① 使用Microsoft的Office组件 ...

  9. asp.net word ecxel类型文件在线预览

    asp.net word ecxel类型文件在线预览 首先得引用COM: Microsoft Excel 10 Object Library Microsoft Word 10 Object Libr ...

随机推荐

  1. Semantic Parsing(语义分析) Knowledge base(知识图谱) 对用户的问题进行语义理解 信息检索方法

    简单说一下所谓Knowledge base(知识图谱)有两条路走,一条是对用户的问题进行语义理解,一般用Semantic Parsing(语义分析),语义分析有很多种,比如有用CCG.DCS,也有用机 ...

  2. Struts2逻辑视图与视图资源

  3. UVA10600 ACM Contest and Blackout —— 次小生成树

    题目链接:https://vjudge.net/problem/UVA-10600 In order to prepare the “The First National ACM School Con ...

  4. js用法2

    1,网站cookie document.cookie 2, Web Storage相当于cookie,当存储量大于cookie localStorage 存储格式都是字符串 有效期,清空缓存前,永远存 ...

  5. 【Android进度条】三种方式实现自定义圆形进度条ProgressBar

    一.通过动画实现 定义res/anim/loading.xml如下: <?xml version="1.0" encoding="UTF-8"?> ...

  6. NSString字符串截取方法

    1.字符串 1> 字符串比较 NSString *a = @“hello”; NSString *b = [NSString stringWithFormat:@hello”]; if (a = ...

  7. Docker实现CentOS容器SSH远程登录

    Docker实现CentOS容器SSH远程登录 https://blog.csdn.net/A632189007/article/details/78625378 这里根据Dockerfile方式构建 ...

  8. Python+页面元素高亮源码实例

    简单写了一个页面元素高亮的方法,原理就是在python中调用js实现元素高亮,分享一下源码如下: 1.元素高亮源码 Js调用 js = "var q=document.getElementB ...

  9. OC:基础总结

    OC面向对象的编程语言思想 类与对象.继承与实例化.属性点语法.内存管理.字符串.可见度. 类是一组具有相同特征和行为的事物的抽象 OC的与C相比所具有的新的特点: 定义新的类.类的实例和方法.方法的 ...

  10. 02_电话拨号器intent说明

    怎么在第一个Activity打开第二个Activity?在一个Activity中打开另外一个Activity,实际上之前已经做过,就是电话拨号器. package com.itheima.callne ...