现在网上大多数文档预览都是文件转换成swf之后flash预览。其实如果用心找还是能够找到蛮多相关资料的。这里写的是关于使用print2flash实现在线文档预览功能的。
思路:
1.传入文档url,下载文档,获取文件md5值(获取md5值之后,查找是否有md5.xml,如果有,就直接返回转换后的swf路径,这样就不用下面两步了),由于文件md5是唯一的,所以生成md5之后我就把服务器的文件删除了。美之名曰:过河拆桥。
2.将文档转换成swf,保存到服务器。
3.将文件名,md5值,转换后的swf路径存入xml文件,保存为md5.xml。
过程(基于.net开发):
1.服务器部署print2flash 下载
2.iis环境安装 print2flash 需要.net2.0以上环境
3.web项目实现,下面附代码
protected void unint(string url) {
try
{
//获取文件名称
string fileName = url.Split('/')[url.Split('/').Length - 1];
//文件格式
string[] fileNameStr = fileName.Split('.');
if (!checkFileType.checkFile(fileNameStr[fileNameStr.Length - 1]))
{
errorMsg = "文件格式不支持";
return;
}
string saveFileName = Server.MapPath("uploadedfiles/" + fileName);
//保存文件
FileStream fs = new FileStream(saveFileName, System.IO.FileMode.Create);
try
{
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
long filesize = request.GetResponse().ContentLength;//取得目标文件的长度
if (filesize > 0)
{
byte[] nbytes = new byte[512];
System.IO.Stream ns = request.GetResponse().GetResponseStream();//获得接收流
int nreadsize = ns.Read(nbytes, 0, 512);
while (nreadsize > 0)
{
fs.Write(nbytes, 0, nreadsize);
nreadsize = ns.Read(nbytes, 0, 512);
//listBox1.Items.Add("文件正在接收进度");
//listBox1.SelectedIndex = listBox1.Items.Count - 1;
}
fs.Close();
ns.Close();
}
request.Abort();
}
catch (Exception er)
{
fs.Close();
throw er;
}
//获取文件MD5值
string fileMd5 = MD5FromFile.GetMD5HashFromFile(saveFileName);
string convertFileName = Server.MapPath("convertedfiles/" + fileName + "-" + fileMd5 + ".swf");
//xml5文件
string xmlFileName = Server.MapPath("xml/" + fileMd5 + ".xml");
if (File.Exists(xmlFileName))
{
swfUrl = XmlCtr.getXml(xmlFileName, "convertFileName");
//删除doc文件
File.Delete(saveFileName);
return;
}
//转换文件
try
{
int interfaceOptions = 0;
//interfaceOptions = 120/*Zooming controls*/ | 8192/*Print button*/| 896/*Navigation controls*/| 3072/*Search controls*/| 4182022/*Other controls*/;
interfaceOptions = 120/*Zooming controls*/ | 896/*Navigation controls*/;
//a|=b等价于a=a|b;
Print2Flash3.Server2 p2fServer = new Print2Flash3.Server2();
p2fServer.DefaultProfile.InterfaceOptions = interfaceOptions;
p2fServer.DefaultProfile.ProtectionOptions = (int)Print2Flash3.PROTECTION_OPTION.PROTENAPI;
p2fServer.ConvertFile(saveFileName, convertFileName, null, null, null);
}
catch (Exception ex)
{
throw ex;
}
FileMsg filemsg = new FileMsg();
filemsg.fileName = "uploadedfiles/" + fileName;
filemsg.convertFileName = "convertedfiles/" + fileName + "-" + fileMd5 + ".swf";
filemsg.MD5 = fileMd5;
XmlCtr.CreatXml(xmlFileName, filemsg);
//删除doc文件
File.Delete(saveFileName);
swfUrl = filemsg.convertFileName;
}
catch {
errorMsg = "系统或者文件出错,请刷新";
}
}
public class MD5FromFile
{
public static string GetMD5HashFromFile(string fileName)
{
try
{
FileStream file = new FileStream(fileName, FileMode.Open);
System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
byte[] retVal = md5.ComputeHash(file);
file.Close();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < retVal.Length; i++)
{
sb.Append(retVal[i].ToString("x2"));
}
return sb.ToString();
}
catch (Exception ex)
{
throw new Exception("GetMD5HashFromFile() fail,error:" + ex.Message);
}
}
}
public class XmlCtr
{
public static void CreatXml(string fileName,FileMsg filemsg) {
System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();//
XmlDeclaration dec = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", "yes");
xmlDoc.AppendChild(dec);
//创建根节点
XmlNode rootNode = xmlDoc.CreateElement("file");
XmlNode fileNode = xmlDoc.CreateElement("fileMsg");
XmlAttribute nameAttribute = xmlDoc.CreateAttribute("fileName");
nameAttribute.Value = filemsg.fileName;
XmlAttribute nameAttribute1 = xmlDoc.CreateAttribute("MD5");
nameAttribute1.Value = filemsg.MD5;
XmlAttribute nameAttribute2 = xmlDoc.CreateAttribute("convertFileName");
nameAttribute2.Value = filemsg.convertFileName;
//xml节点附件属性
fileNode.Attributes.Append(nameAttribute);
fileNode.Attributes.Append(nameAttribute1);
fileNode.Attributes.Append(nameAttribute2);
//保存Xml文档
rootNode.AppendChild(fileNode);//添加到<bookstore>节点中
xmlDoc.AppendChild(rootNode);
xmlDoc.Save(fileName);
}
public static string getXml(string fileName, string col) {
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(fileName);
XmlElement xmlel = (XmlElement)xmlDoc.SelectSingleNode("file").FirstChild;
return xmlel.GetAttribute(col);
}
}
public class FileMsg {
public string fileName { set; get; }
public string MD5 { set; get; }
public string convertFileName { set; get; }
}
public class checkFileType {
public static bool checkFile(string filetype) {
string filetypeStr = "doc, docx, ppt, pptx, xls, xlsx, rtf, htm, html, pdf, jpg, jpeg, tif, tiff, bmp, gif, png, txt, odt, odg, odp, odf";
if (filetypeStr.IndexOf(filetype) > -1)
{
return true;
}
else {
return false;
}
}
}
代码写到这里就结束了,希望给大家带来帮助。