MVC 附件在线预览
原因:应客户需求,在系统中浏览附件内容,需要先下载到本地然后打开,对使用造成了不便,要求可以不需下载直接在浏览器中打开减少操作步骤。
领导给了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 附件在线预览的更多相关文章
- 如何实现QQ附件在线预览功能
方法一:使用 openoffice 的接口把文档转换成html (linux主机或者windows主机): 方法二:使用 一个叫 jacob.jar 的工具,在安装了 office 的windows主 ...
- 基于开源方案构建统一的文件在线预览与office协同编辑平台的架构与实现历程
大家好,又见面了. 在构建业务系统的时候,经常会涉及到对附件的支持,继而又会引申出对附件在线预览.在线编辑.多人协同编辑等种种能力的诉求. 对于人力不是特别充裕.或者项目投入预期规划不是特别大的公司或 ...
- 文件批量上传-统一附件管理器-在线预览文件(有互联网和没有两种)--SNF快速开发平台3.0
实际上在SNF里使用附件管理是非常简单的事情,一句代码就可以搞定.但我也要在这里记录一下统一附件管理器能满足的需求. 通用的附件管理,不要重复开发,调用尽量简洁. 批量文件上传,并对每个文件大小限制, ...
- ASP.NET MVC在线预览Excel、Word、TXT、PDF文件
代码: using System; using System.Collections.Generic; using System.Linq; using System.Web; using Syste ...
- Asp.net MVC 利用(aspose+pdfobject.js) 实现在线预览word、excel、ppt、pdf文件
在线预览word.excel.ppt利用aspose动态生成html 主要代码 private bool OfficeDocumentToHtml(string sourceDoc, string s ...
- .net mvc使用FlexPaper插件实现在线预览PDF,EXCEL,WORD的方法
FlexPaper插件可以实现在浏览器中在线预览pdf,word,excel等. 在网上看到很多关于这个插件实现预览的技术,但是很难做到word和excel在线预览. pdf很好实现. 首先下载相关的 ...
- 实现在线预览PDF的几种解决方案
因客户需要实现PDF的预览处理,在网上找了一些PDF在线预览的解决方案,有的用PDFJS的在线预览方式,有的使用PDFObject的嵌入式显示,有的通过转换JPG/PNG方式实现间接显示的方式,开始是 ...
- Word/Excel 在线预览
前言 近日项目中做到一个功能,需要上传附件后能够在线预览.之前也没做过这类似的,于是乎就查找了相关资料,.net实现Office文件预览大概有这几种方式: ① 使用Microsoft的Office组件 ...
- asp.net word ecxel类型文件在线预览
asp.net word ecxel类型文件在线预览 首先得引用COM: Microsoft Excel 10 Object Library Microsoft Word 10 Object Libr ...
随机推荐
- linux路由表解析
1 格式 Destination 这个和Genmask一起构成目标网络.路由是路由到目标网络,知道目标网络就可以到达目标路由器,然后在该网络中找到目标机器. Gateway 网关,数据包的下一跳.比如 ...
- 【java报错】CacheLoader returned null for key class
CacheLoader returned null for key class cmd mvn eclipse:clean eclipse:eclipse mvn install -Dmave ...
- 设计模式-(17)策略模式 (swift版)
一,概念: 策略模式定义了一系列的算法,并将每一个算法封装起来,而且使他们可以相互替换,让算法独立于使用它的客户而独立变化. 二,使用场景 1.针对同一类型问题的多种处理方式,仅仅是具体行为有差别时: ...
- ASP.NET Web Pages (Razor) API Quick Reference
ASP.NET Web Pages (Razor) API Quick Reference By Tom FitzMacken|February 10, 2014 Print This page co ...
- git unstage
https://stackoverflow.com/questions/6919121/why-are-there-2-ways-to-unstage-a-file-in-git git rm --c ...
- 【转】Android性能优化-过度绘制解决方案
转载请注明出处:http://blog.csdn.net/a740169405/article/details/53896497 过度绘制: 屏幕上某一像素点在一帧中被重复绘制多次,就是过度绘制. 下 ...
- Linux服务器性能问题
如何快速分析出现性能问题的Linux服务器 https://www.cnblogs.com/leixiaobai/category/1246164.html Brendan Gregg曾经分享过当遇到 ...
- BZOJ_1307_玩具_单调栈+双指针
BZOJ_1307_玩具_单调栈+双指针 Description 小球球是个可爱的孩子,他喜欢玩具,另外小球球有个大大的柜子,里面放满了玩具,由于柜子太高了,每天小球球都会让妈妈从柜子上拿一些玩具放在 ...
- ChartCtrl源码剖析之——CChartTitle类
CChartTitle类顾名思义,该类用来绘制波形控件的标题,它处于该控件的区域,如下图所示: CChartTitle类的头文件. #if !defined(AFX_CHARTTITLE_H__499 ...
- 三种实现AJAX的方法以及Vue和axios结合使用的坑
前言 之前曾经想自己写一个天气的App,并找到了一个免费的天气数据接口的服务商--和风天气,当时也不懂怎么发HTTP请求,而且也只会用Java语言,看到官方文档里面有一个Java代码示例,就复制了一下 ...