最近接到一个案子需要实现把上传的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" %>
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();
}
}
}
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="viewer2.aspx.cs" Inherits="onlineRead_viewer2" %>
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();
}
}
}